Neural Sync Active
May 2026 - Python - Week 3 - GrPA 2 - For Loop - GRADED
Registry Synced
May 2026 - Python - Week 3 - GrPA 2 - For Loop - GRADED
1504 words
8 min read
Reading compass
Now · Problem Statement
GrPA 2 - For Loop - GRADED
Course: May 2026 - Python
Week 3
Problem Statement
Change in eligibility criteria to write oppe1 exam:
A1>=40/100 AND A2>=40/100 AND A3>=40/100 AND A4>=40/100
Bit of Wisdom 📖
In context of general incremental definite loops the
structure of
while loop can be converted to a for loop using range. Refer this.Write a multi functional program that takes input task from standard input and does the corresponding taks accordingly. Note that the useage of for loop is not allowed in this exercise.
Part 1 - while loop to for loop
factorial - print factorial of a given non-negative integer n (Type: Accumulation)Input -
n:inteven_numbers - Print the even numbers from 0 (including) till the given input number n(including) in multiple lines (Type: Just Iterating)Input -
n:intpower_sequence - Print the sequence 1, 2, 4, 8, 16, ... n terms in same line in multiple lines, where n is taken from the input(Type: Mapping)Input -
n:intPart 2 - for loop With range
sum_not_divisible - Print the sum of positive less that the given number n and not divisible by 4 and 5. (Type: Filtered Accumulation)Input -
n:intfrom_k - Starting from 100 and going in the decreasing order, print the reverse(digits reversed) of first n numbers starting from k which do not have the digit 5 and 9 and is odd number in multiple lines.Input -
n:int, k:intpart 3 - for loop with iterables.
string_iter - Given a string s of digits print the numerical value of the digit multiplied by the previous digit. Assume the pevious digit for the first element to be 1.Input -
s:strlist_iter - Print the elements of a list l line by line in the format {element} - type: {type} where the element is the current element being iterated by the for loop and type is the type of the element. (Even though list are not covered in this week, this is included to demonstrate the similarity between iterating characters in a str and items in a list)Input -
l:listTemplate Code
plaintextif task == 'factorial': n = int(input()) result = 1 i = 1 while i <=n: result*=i i+=1 print(result) elif task == 'even_numbers': n = ... while i< n+1: print(i) i+=2 elif task == 'power_sequence': n = ... result = 1 while i<n: print(result) result*=2 i+=1 elif task == 'sum_not_divisible': ... elif task == 'from_k': ... elif task == 'string_iter': ... elif task == 'list_iter': lst = eval(input()) # this will load the list from input else: print("Invalid")
Test Cases
Public Test Cases
Case 1
Input:
textfactorial 5
Expected Output:
text120
Actual Output:
text120
Case 2
Input:
textfactorial 7
Expected Output:
text5040
Actual Output:
text5040
Case 3
Input:
texteven_numbers 10
Expected Output:
text0 2 4 6 8 10
Actual Output:
text0 2 4 6 8 10
Case 4
Input:
texteven_numbers 5
Expected Output:
text0 2 4
Actual Output:
text0 2 4
Case 5
Input:
textpower_sequence 5
Expected Output:
text1 2 4 8 16
Actual Output:
text1 2 4 8 16
Case 6
Input:
textpower_sequence 3
Expected Output:
text1 2 4
Actual Output:
text1 2 4
Case 7
Input:
textsum_not_divisible 15
Expected Output:
text66
Actual Output:
text66
Case 8
Input:
textsum_not_divisible 10
Expected Output:
text28
Actual Output:
text28
Case 9
Input:
textfrom_k 5 99
Expected Output:
text78 38 18 77 37
Actual Output:
text78 38 18 77 37
Case 10
Input:
textfrom_k 3 55
Expected Output:
text74 34 14
Actual Output:
text74 34 14
Case 11
Input:
textstring_iter 1234
Expected Output:
text1 2 6 12
Actual Output:
text1 2 6 12
Case 12
Input:
textstring_iter 567
Expected Output:
text5 30 42
Actual Output:
text5 30 42
Case 13
Input:
textlist_iter [1, 2.0, "three", True]
Expected Output:
text1 - type: <class 'int'> 2.0 - type: <class 'float'> three - type: <class 'str'> True - type: <class 'bool'>
Actual Output:
text1 - type: <class 'int'> 2.0 - type: <class 'float'> three - type: <class 'str'> True - type: <class 'bool'>
Case 14
Input:
textlist_iter ["apple", ... , [1,2], None]
Expected Output:
textapple - type: <class 'str'> Ellipsis - type: <class 'ellipsis'> [1, 2] - type: <class 'list'> None - type: <class 'NoneType'>
Actual Output:
textapple - type: <class 'str'> Ellipsis - type: <class 'ellipsis'> [1, 2] - type: <class 'list'> None - type: <class 'NoneType'>
Private Test Cases
Case 1
Input:
textfactorial 5
Expected Output:
text120
Actual Output:
text120
Case 2
Input:
textfactorial 0
Expected Output:
text1
Actual Output:
text1
Case 3
Input:
texteven_numbers 6
Expected Output:
text0 2 4 6
Actual Output:
text0 2 4 6
Case 4
Input:
texteven_numbers 0
Expected Output:
text0
Actual Output:
text0
Case 5
Input:
textpower_sequence 7
Expected Output:
text1 2 4 8 16 32 64
Actual Output:
text1 2 4 8 16 32 64
Case 6
Input:
textpower_sequence 2
Expected Output:
text1 2
Actual Output:
text1 2
Case 7
Input:
textsum_not_divisible 25
Expected Output:
text186
Actual Output:
text186
Case 8
Input:
textsum_not_divisible 6
Expected Output:
text6
Actual Output:
text6
Case 9
Input:
textfrom_k 4 100
Expected Output:
text78 38 18 77
Actual Output:
text78 38 18 77
Case 10
Input:
textfrom_k 7 70
Expected Output:
text76 36 16 74 34 14 73
Actual Output:
text76 36 16 74 34 14 73
Case 11
Input:
textstring_iter 124334
Expected Output:
text1 2 8 12 9 12
Actual Output:
text1 2 8 12 9 12
Case 12
Input:
textstring_iter 271828
Expected Output:
text2 14 7 8 16 16
Actual Output:
text2 14 7 8 16 16
Case 13
Input:
textlist_iter [10, 20.5, "hello", False, None, [1,2,3]]
Expected Output:
text10 - type: <class 'int'> 20.5 - type: <class 'float'> hello - type: <class 'str'> False - type: <class 'bool'> None - type: <class 'NoneType'> [1, 2, 3] - type: <class 'list'>
Actual Output:
text10 - type: <class 'int'> 20.5 - type: <class 'float'> hello - type: <class 'str'> False - type: <class 'bool'> None - type: <class 'NoneType'> [1, 2, 3] - type: <class 'list'>
Case 14
Input:
textlist_iter [24,"Python", 53.6, False]
Expected Output:
text24 - type: <class 'int'> Python - type: <class 'str'> 53.6 - type: <class 'float'> False - type: <class 'bool'>
Actual Output:
text24 - type: <class 'int'> Python - type: <class 'str'> 53.6 - type: <class 'float'> False - type: <class 'bool'>
Official Solution
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
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':
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX