Quiz 2
Registry Synced

May 2026 - Python - Week 3 - GrPA 1 - While Loop - GRADED

1643 words
8 min read

Reading compass

Now · Problem Statement

GrPA 1 - While 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
✅ Important Note on while loop🔁:
Use while only when the number of iterations is indefinite.
If you can term the steps as do n times, do once for each item, etc. use for loop instead.
If you can only term the steps as do until something happens. Like when user inputs 10.
A bit of wisdom 📖 There are maily two ways in which while loops are used in the context of taking inputs until a terminal word.
Problem Statement
Problem type - Standard Input - Standard Output
NOTE: None of this problem statements can be written using a for since the number of repetition is indefinite.
Implement different parts of a multi-functional program based on an initial input value. Each part of the program will handle various tasks related to accumulation, filtering, mapping, and combinations of these operations. None of the tasks should use explicit loops for definite repetitions, and the program should handle indefinite inputs gracefully.
Tasks
Accumulation - Accumulating a final result sum_until_0: Continuously read integers from standard input until you receive a zero. Print the sum of these integers.
total_price: Continuously read pairs of integers from standard input, representing the quantity and price of items, until you receive the string "END". Print the total price of all items.
Filtering - Selecting based on a criterion only_ed_or_ing: Continuously read strings from standard input until you encounter the word "STOP" (case insensitive and not included in the output). Print only those strings that end with "ed" or "ing" (case insensitive).
reverse_sum_palindrome: Continuously read positive integers from standard input until you encounter a "-1"(not included in the output). Print only those integers for which the sum of the number and its reverse is a palindrome.
Mapping - Applying the same operation to different items double_string: Continuously read lines from standard input until an empty line is encountered. Print each line repeated twice.
odd_char: Continuously read strings from standard input until you encounter a string ending with a "."(include that string with the "." in the output). Extract characters at odd positions (starting from 1) of each line, and print the results in a single line separated by spaces.
Filter and Map - Applying an operation to selected items only_even_squares: Continuously read numbers from standard input until "NAN" is encountered. Print the square of each number only if it is even.
Filter and Accumulate - Accumulating a result with selected items only_odd_lines: Continuously read lines from standard input until "END"(not included in the output) is encountered. Create a string by prepending only the odd lines (starting from 1) with a newline character in between, and print the result which will be the odd lines in reverse order.

Template Code

plaintext
if task == "sum_until_0":
    total = 0
    n = int(input())
    while ...: # the terminal condition
        ... # add n to the total
        ... # take the next n form the input
    print(total)

elif task == "total_price":
    total_price = 0
    while ...: # repeat forever since we are breaking inside
        line = input()
        if ...: # The terminal condition
            break
        quantity, price = line.split() # split uses space by default
        quantity, price = ... # convert to ints
        ... # accumulate the total price
    print(total_price)
elif task == "only_ed_or_ing":
    ...

elif task == "reverse_sum_palindrome":
    ...

elif task == "double_string":
    ...

elif task == "odd_char":
    ...

elif task == "only_even_squares":
    ...

elif task == "only_odd_lines":
    ...

Test Cases

Public Test Cases

Case 1

Input:
text
sum_until_0
5
3
2
0
Expected Output:
text
10
Actual Output:
text
10

Case 2

Input:
text
sum_until_0
10
-5
5
0
Expected Output:
text
10
Actual Output:
text
10

Case 3

Input:
text
total_price
2 50
1 100
3 30
END
Expected Output:
text
290
Actual Output:
text
290

Case 4

Input:
text
total_price
5 10
2 20
END
Expected Output:
text
90
Actual Output:
text
90

Case 5

Input:
text
only_ed_or_ing
Reading
completed
running
start
END
STOP
Expected Output:
text
Reading
completed
running
Actual Output:
text
Reading
completed
running

Case 6

Input:
text
only_ed_or_ing
opened
close
stopped
move
ingested
STOP
Expected Output:
text
opened
stopped
ingested
Actual Output:
text
opened
stopped
ingested

Case 7

Input:
text
reverse_sum_palindrome
56
99
32
-1
Expected Output:
text
56
32
Actual Output:
text
56
32

Case 8

Input:
text
reverse_sum_palindrome
12
19
23
34
87
-1
Expected Output:
text
12
23
34
Actual Output:
text
12
23
34

Case 9

Input:
text
double_string
hello
world
Expected Output:
text
hellohello
worldworld
Actual Output:
text
hellohello
worldworld

Case 10

Input:
text
double_string
foo
bar
baz
Expected Output:
text
foofoo
barbar
bazbaz
Actual Output:
text
foofoo
barbar
bazbaz

Case 11

Input:
text
odd_char
Hello
WORLD.
Expected Output:
text
Hlo WRD
Actual Output:
text
Hlo WRD

Case 12

Input:
text
odd_char
This
is
a 
sample
sentence.
Expected Output:
text
Ti i a sml snec.
Actual Output:
text
Ti i a sml snec.

Case 13

Input:
text
only_even_squares
3
4
5
NAN
Expected Output:
text
16
Actual Output:
text
16

Case 14

Input:
text
only_even_squares
2
7
8
NAN
Expected Output:
text
4
64
Actual Output:
text
4
64

Case 15

Input:
text
only_odd_lines
one
two
three
four
END
Expected Output:
text
three
one
Actual Output:
text
three
one

Case 16

Input:
text
only_odd_lines
line1
line2
line3
END
Expected Output:
text
line3
line1
Actual Output:
text
line3
line1

Private Test Cases

Case 1

Input:
text
sum_until_0
100
200
300
0
Expected Output:
text
600
Actual Output:
text
600

Case 2

Input:
text
sum_until_0
-1
-1
2
0
Expected Output:
text
0
Actual Output:
text
0

Case 3

Input:
text
total_price
1 1
2 1
3 1
END
Expected Output:
text
6
Actual Output:
text
6

Case 4

Input:
text
total_price
3 4
4 5
END
Expected Output:
text
32
Actual Output:
text
32

Case 5

Input:
text
only_ed_or_ing
tracked
jumped
book
coding
none
STOP
Expected Output:
text
tracked
jumped
coding
Actual Output:
text
tracked
jumped
coding

Case 6

Input:
text
only_ed_or_ing
raced
coded
running
STOP
Expected Output:
text
raced
coded
running
Actual Output:
text
raced
coded
running

Case 7

Input:
text
reverse_sum_palindrome
21
123
456
-1
Expected Output:
text
21
123
Actual Output:
text
21
123

Case 8

Input:
text
reverse_sum_palindrome
44
55
77
-1
Expected Output:
text
44
Actual Output:
text
44

Case 9

Input:
text
double_string
test
example
Expected Output:
text
testtest
exampleexample
Actual Output:
text
testtest
exampleexample

Case 10

Input:
text
double_string
python
programming
Expected Output:
text
pythonpython
programmingprogramming
Actual Output:
text
pythonpython
programmingprogramming

Case 11

Input:
text
odd_char
Programming
in
python
is
fun.
Expected Output:
text
Pormig i pto i fn
Actual Output:
text
Pormig i pto i fn

Case 12

Input:
text
odd_char
Make
it
work.
Expected Output:
text
Mk i wr.
Actual Output:
text
Mk i wr.

Case 13

Input:
text
only_even_squares
9
10
12
NAN
Expected Output:
text
100
144
Actual Output:
text
100
144

Case 14

Input:
text
only_even_squares
15
22
27
NAN
Expected Output:
text
484
Actual Output:
text
484

Case 15

Input:
text
only_odd_lines
A
B
C
D
END
Expected Output:
text
C
A
Actual Output:
text
C
A

Case 16

Input:
text
only_odd_lines
one
two
three
four
five
END
Expected Output:
text
five
three
one
Actual Output:
text
five
three
one

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

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 "for " in content:
print("You should not use for loop or the word for
anywhere in this exercise")

This is the first line of the exercise

task = input()

<eoi>

if task == "sum_until_0":
total = 0
n = int(input())
while n != 0: # the terminal condition
total += n # add n to the total
n = int(input()) # take the next n form the input
print(total)
elif task == "total_price":
total_price = 0
while True: # repeat forever since we are breaking
inside
line = input()
if line == "END": # The terminal condition
break
quantity, price = line.split() # split uses space by
default
quantity, price = int(quantity), int(price) #
convert to ints
total_price += quantity * price # accumulate the
total price
print(total_price)
elif task == "only_ed_or_ing":
word = input()
while word.lower() != "stop":
if word.lower().endswith("ed") or word.lower()[-3:]
=="ing": # both ways of doing it
print(word)
word = input()
elif task == "reverse_sum_palindrome":
num = int(input())
while num !=-1:
rev_num = int(str(num)[::-1])
num_sum = num+rev_num   הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
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.