Neural Sync Active
May 2026 - Python - Week 3 - GrPA 1 - While Loop - GRADED
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
plaintextif 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:
textsum_until_0 5 3 2 0
Expected Output:
text10
Actual Output:
text10
Case 2
Input:
textsum_until_0 10 -5 5 0
Expected Output:
text10
Actual Output:
text10
Case 3
Input:
texttotal_price 2 50 1 100 3 30 END
Expected Output:
text290
Actual Output:
text290
Case 4
Input:
texttotal_price 5 10 2 20 END
Expected Output:
text90
Actual Output:
text90
Case 5
Input:
textonly_ed_or_ing Reading completed running start END STOP
Expected Output:
textReading completed running
Actual Output:
textReading completed running
Case 6
Input:
textonly_ed_or_ing opened close stopped move ingested STOP
Expected Output:
textopened stopped ingested
Actual Output:
textopened stopped ingested
Case 7
Input:
textreverse_sum_palindrome 56 99 32 -1
Expected Output:
text56 32
Actual Output:
text56 32
Case 8
Input:
textreverse_sum_palindrome 12 19 23 34 87 -1
Expected Output:
text12 23 34
Actual Output:
text12 23 34
Case 9
Input:
textdouble_string hello world
Expected Output:
texthellohello worldworld
Actual Output:
texthellohello worldworld
Case 10
Input:
textdouble_string foo bar baz
Expected Output:
textfoofoo barbar bazbaz
Actual Output:
textfoofoo barbar bazbaz
Case 11
Input:
textodd_char Hello WORLD.
Expected Output:
textHlo WRD
Actual Output:
textHlo WRD
Case 12
Input:
textodd_char This is a sample sentence.
Expected Output:
textTi i a sml snec.
Actual Output:
textTi i a sml snec.
Case 13
Input:
textonly_even_squares 3 4 5 NAN
Expected Output:
text16
Actual Output:
text16
Case 14
Input:
textonly_even_squares 2 7 8 NAN
Expected Output:
text4 64
Actual Output:
text4 64
Case 15
Input:
textonly_odd_lines one two three four END
Expected Output:
textthree one
Actual Output:
textthree one
Case 16
Input:
textonly_odd_lines line1 line2 line3 END
Expected Output:
textline3 line1
Actual Output:
textline3 line1
Private Test Cases
Case 1
Input:
textsum_until_0 100 200 300 0
Expected Output:
text600
Actual Output:
text600
Case 2
Input:
textsum_until_0 -1 -1 2 0
Expected Output:
text0
Actual Output:
text0
Case 3
Input:
texttotal_price 1 1 2 1 3 1 END
Expected Output:
text6
Actual Output:
text6
Case 4
Input:
texttotal_price 3 4 4 5 END
Expected Output:
text32
Actual Output:
text32
Case 5
Input:
textonly_ed_or_ing tracked jumped book coding none STOP
Expected Output:
texttracked jumped coding
Actual Output:
texttracked jumped coding
Case 6
Input:
textonly_ed_or_ing raced coded running STOP
Expected Output:
textraced coded running
Actual Output:
textraced coded running
Case 7
Input:
textreverse_sum_palindrome 21 123 456 -1
Expected Output:
text21 123
Actual Output:
text21 123
Case 8
Input:
textreverse_sum_palindrome 44 55 77 -1
Expected Output:
text44
Actual Output:
text44
Case 9
Input:
textdouble_string test example
Expected Output:
texttesttest exampleexample
Actual Output:
texttesttest exampleexample
Case 10
Input:
textdouble_string python programming
Expected Output:
textpythonpython programmingprogramming
Actual Output:
textpythonpython programmingprogramming
Case 11
Input:
textodd_char Programming in python is fun.
Expected Output:
textPormig i pto i fn
Actual Output:
textPormig i pto i fn
Case 12
Input:
textodd_char Make it work.
Expected Output:
textMk i wr.
Actual Output:
textMk i wr.
Case 13
Input:
textonly_even_squares 9 10 12 NAN
Expected Output:
text100 144
Actual Output:
text100 144
Case 14
Input:
textonly_even_squares 15 22 27 NAN
Expected Output:
text484
Actual Output:
text484
Case 15
Input:
textonly_odd_lines A B C D END
Expected Output:
textC A
Actual Output:
textC A
Case 16
Input:
textonly_odd_lines one two three four five END
Expected Output:
textfive three one
Actual Output:
textfive 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