Quiz 2
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:int
even_numbers - Print the even numbers from 0 (including) till the given input number n(including) in multiple lines (Type: Just Iterating)
Input - n:int
power_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:int
Part 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:int
from_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:int
part 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:str
list_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:list

Template Code

plaintext
if 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:
text
factorial
5
Expected Output:
text
120
Actual Output:
text
120

Case 2

Input:
text
factorial
7
Expected Output:
text
5040
Actual Output:
text
5040

Case 3

Input:
text
even_numbers
10
Expected Output:
text
0
2
4
6
8
10
Actual Output:
text
0
2
4
6
8
10

Case 4

Input:
text
even_numbers
5
Expected Output:
text
0
2
4
Actual Output:
text
0
2
4

Case 5

Input:
text
power_sequence
5
Expected Output:
text
1
2
4
8
16
Actual Output:
text
1
2
4
8
16

Case 6

Input:
text
power_sequence
3
Expected Output:
text
1
2
4
Actual Output:
text
1
2
4

Case 7

Input:
text
sum_not_divisible
15
Expected Output:
text
66
Actual Output:
text
66

Case 8

Input:
text
sum_not_divisible
10
Expected Output:
text
28
Actual Output:
text
28

Case 9

Input:
text
from_k
5
99
Expected Output:
text
78
38
18
77
37
Actual Output:
text
78
38
18
77
37

Case 10

Input:
text
from_k
3
55
Expected Output:
text
74
34
14
Actual Output:
text
74
34
14

Case 11

Input:
text
string_iter
1234
Expected Output:
text
1
2
6
12
Actual Output:
text
1
2
6
12

Case 12

Input:
text
string_iter
567
Expected Output:
text
5
30
42
Actual Output:
text
5
30
42

Case 13

Input:
text
list_iter
[1, 2.0, "three", True]
Expected Output:
text
1 - type: <class 'int'>
2.0 - type: <class 'float'>
three - type: <class 'str'>
True - type: <class 'bool'>
Actual Output:
text
1 - type: <class 'int'>
2.0 - type: <class 'float'>
three - type: <class 'str'>
True - type: <class 'bool'>

Case 14

Input:
text
list_iter
["apple", ... , [1,2], None]
Expected Output:
text
apple - type: <class 'str'>
Ellipsis - type: <class 'ellipsis'>
[1, 2] - type: <class 'list'>
None - type: <class 'NoneType'>
Actual Output:
text
apple - type: <class 'str'>
Ellipsis - type: <class 'ellipsis'>
[1, 2] - type: <class 'list'>
None - type: <class 'NoneType'>

Private Test Cases

Case 1

Input:
text
factorial
5
Expected Output:
text
120
Actual Output:
text
120

Case 2

Input:
text
factorial
0
Expected Output:
text
1
Actual Output:
text
1

Case 3

Input:
text
even_numbers
6
Expected Output:
text
0
2
4
6
Actual Output:
text
0
2
4
6

Case 4

Input:
text
even_numbers
0
Expected Output:
text
0
Actual Output:
text
0

Case 5

Input:
text
power_sequence
7
Expected Output:
text
1
2
4
8
16
32
64
Actual Output:
text
1
2
4
8
16
32
64

Case 6

Input:
text
power_sequence
2
Expected Output:
text
1
2
Actual Output:
text
1
2

Case 7

Input:
text
sum_not_divisible
25
Expected Output:
text
186
Actual Output:
text
186

Case 8

Input:
text
sum_not_divisible
6
Expected Output:
text
6
Actual Output:
text
6

Case 9

Input:
text
from_k
4
100
Expected Output:
text
78
38
18
77
Actual Output:
text
78
38
18
77

Case 10

Input:
text
from_k
7
70
Expected Output:
text
76
36
16
74
34
14
73
Actual Output:
text
76
36
16
74
34
14
73

Case 11

Input:
text
string_iter
124334
Expected Output:
text
1
2
8
12
9
12
Actual Output:
text
1
2
8
12
9
12

Case 12

Input:
text
string_iter
271828
Expected Output:
text
2
14
7
8
16
16
Actual Output:
text
2
14
7
8
16
16

Case 13

Input:
text
list_iter
[10, 20.5, "hello", False, None, [1,2,3]]
Expected Output:
text
10 - 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:
text
10 - 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:
text
list_iter
[24,"Python", 53.6, False]
Expected Output:
text
24 - type: <class 'int'>
Python - type: <class 'str'>
53.6 - type: <class 'float'>
False - type: <class 'bool'>
Actual Output:
text
24 - 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

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.