Homework: Write a while loop with these conditions:
num starts at 0
Increments by 3 for a multiple of 2
Increments by 5 for a multiple of 3
Increments by 7 for a multiple of 5
Else, increment by 1
(For numbers like 30, a multiple of 2, 3, and 5, just let it increment thrice.)
num ends at 50
Write a for loop that reverses a string Resources: Slides: https://docs.google.com/presentation/d/1CRXu6253UinHpiKEMy3jCEwyIZdv6KXOzjep6_acN3M/edit?usp=sharing
Code: https://replit.com/join/npmdzjjjhm-shrav816 Contact: shravyassathish@gmail.com (mentor)
mailtoarnavpandey@gmail.com (TA) Make sure to "Add a comment" with your code to the homework in order to submit your homework assignment!
Recordings for the class have been sent out through email :)
# 1. num = 0 while num < 50: if num % 2 == 0: num += 3 print(num) elif num % 3 == 0: num += 5 print(num) elif num % 5 == 0: num += 7 print(num) else: num += 1 print(num) # 2. string = input("Input a multicharacter string:\n") x = [i for i in string] for i in range(len(string)): for j in range(len(string)-(i+1)): x[j], x[j + 1] = x[j + 1], x[j] print(*x, sep="") # The below code also works, but uses a different method. # x = [i for i in string] # y = [i for i in string] # for i in range(len(string)): # y[i] = x[len(string)-i-1] # print(*y, sep="")
#2 uses code that has not been covered yet but can be easily understood (reader, you do not have to read all the below; only read if you do not understand, and you want to).
Firstly, we have the line:
x = [i for i in string]
This line of code does what the following does:
x = []for i in string: for j in range(len(i)) x[j] = i
Each value of x in the list turns into each character in the string. Doing the main code on the string itself would result in an error because a string is immutable (unchangeable).
I am not sure if Shravya went over the len() command, so I will explain it. The len() command simply returns the integer length of (usually) a string. For example, if the string input was "Hello There", it would output 11, not 10. Python normally uses 0-indexing, but it doesn't appear here. You can see 0-indexing when calling for items in a list; x[0] is the 1st term of the list x.
The line x[j], x[j + 1] = x[j + 1], x[j] simply swaps the first and second list item, then the second and third, and so on. The code does specify that it swaps one less time each iteration.
For example, 1234 would be reversed like this:
1234
2134
2314
2341
3241
3421
4321
To put it simply, the program moves the first character to the last place by swapping, then the second to the penultimate (second to last), etc.
Now, we have the last line of code. The * before x states to print without the brackets []. Then, we have the sep="". This says to put nothing in between each array item (if that wasn't there, it would put a space separator between each character).
Lastly, we have the commented code. I won't go over how it works because all of the commands in there Shravya or I have already explained. Put simply, the loop puts the first character of the string as the last character of another string. Then, it puts the second character of the original string as the second to last of that second string, and so on.
It would go something like this:
input string (doesn't change); output string (does change)
12345; 12345
12345; 12341
12345; 12321
12345; 12321
12345; 14321
12345; 54321
54321
x = 0 while (x < 51): print(x) y = x if x % 2 == 0: y += 3 if x % 3 == 0: y += 5 if x % 5 == 0: y += 7 if x == y: y += 1 x = y x = input("Put in a string: \n") def reverse_for_loop(s): string1 = '' for i in s: string1 = i + string1 return string1 input_str = x print(reverse_for_loop(input_str))
i = 0
listofis = []
while i < 51:
n = 0
print (i)
listofis.append(str(i))
if i % 2 == 0 and i != 0:
n += 3
if i % 3 == 0 and i != 0:
n += 5
if i % 5 == 0 and i != 0:
n += 7
if n == 0:
i += 1
else:
i += n
listofis.reverse()
for listofi in listofis:
print(listofi)