-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeginner - For loop (practice code series)
More file actions
74 lines (59 loc) · 1.7 KB
/
Beginner - For loop (practice code series)
File metadata and controls
74 lines (59 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Note this prefix code is to verify that you are not using any for loops in this exercise. This won't affect any other functionality of the program.
with open(__file__) as f:
content = f.read().split("# <eoi>")[2]
if "while " in content:
print("You should not use while loop or the word while anywhere in this exercise")
# your code should not use more than 7 for loops
# assuming one for loop per problem
if content.count("for ")>7:
print("You should not use more than 7 for loops")
# This is the first line of the exercise
task = input()
# <eoi>
if task == 'factorial':
n = int(input())
result = 1
for i in range(1,n+1):
result*=i
print(result)
elif task == 'even_numbers':
n = int(input())
for i in range(0, n+1,2):
print(i)
elif task == 'power_sequence':
n = int(input())
result = 1
for i in range(n):
print(result)
result *= 2
elif task == 'sum_not_divisible':
n = int(input())
total = 0
for i in range(1, n):
if i % 4 != 0 and i % 5 != 0:
total += i
print(total)
elif task == 'from_k':
n = int(input())
k = int(input())
count = 0
for i in range(k, 0, -1):
i_str = str(i)
if '5' not in i_str and '9' not in i_str and i % 2 != 0:
print(i_str[::-1])
count += 1
if count == n:
break
elif task == 'string_iter':
s = input()
prev = 1
for char in s:
num = int(char)
print(num * prev)
prev = num
elif task == 'list_iter':
lst = eval(input()) # this will load the list from input
for element in lst:
print(f'{element} - type: {type(element)}')
else:
print("Invalid")