0% found this document useful (0 votes)
6 views5 pages

GRPA WEEK 3

The document contains a series of Python code snippets that implement various tasks based on user input. Each task includes operations such as summing numbers, calculating factorials, checking for prime numbers, and manipulating strings. The tasks are organized into groups (GRPA 1 to GRPA 4) and cover a range of programming concepts and algorithms.

Uploaded by

darlingabhi122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

GRPA WEEK 3

The document contains a series of Python code snippets that implement various tasks based on user input. Each task includes operations such as summing numbers, calculating factorials, checking for prime numbers, and manipulating strings. The tasks are organized into groups (GRPA 1 to GRPA 4) and cover a range of programming concepts and algorithms.

Uploaded by

darlingabhi122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

GRPA 1

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
if str(num_sum) == str(num_sum)[::-1]:
print(num)
num = int(input())

elif task == "double_string":

line = input()
while line != "":
print(line*2)
line = input()

elif task == "odd_char":

line = input()
while line[-1] != ".":
print(line[::2],end=" ")
line = input()
print(line[::2])

elif task == "only_even_squares":

while True:
line = input()
if line == "NAN":
break
num = int(line)
if num % 2 == 0:
print(num * num)

elif task == "only_odd_lines":

result = ""
line_no = 1
while True:
line = input()
if line == "END":
break
if line_no%2:
result = line + "\n" + result
line_no+=1
print(result)

GRPA 2
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")

GRPA 3

if task == 'permutation':
s = input()
for i in range(len(s)):
for j in range(len(s)):
if i != j:
print(s[i] + s[j])

elif task == 'sorted_permutation':


s = input()
for i in range(len(s)):
for j in range(len(s)):
if i != j and s[i] < s[j]:
print(s[i] + s[j])

elif task == 'repeat_the_repeat':

n = int(input())
for _ in range(n):
for i in range(1, n+1):
print(i, end='')
print()

elif task == 'repeat_incrementally':

n = int(input())
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end='')
print()

elif task == 'increment_and_decrement':

n = int(input())
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end='')
for j in range(i-1, 0, -1):
print(j, end='')
print()

else:
print("Invalid")

GRPA 4

if task == "factors":
n = int(input())
for i in range(1, n+1):
if n % i == 0:
print(i)

elif task == "find_min":


n = int(input())
minimum = int(input())
for i in range(n-1):
current = int(input())
if current< minimum:
minimum = current
print(minimum)

elif task == "prime_check":


n = int(input())
if n <= 1:
print(False)
else:
flag = True
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
flag = False
print(False)
break
if flag:
print(True)
# The below is an alternate implementation using for...else block in python
which do not require the flag variable.
# for i in range(2, int(n**0.5) + 1):
# if n % i == 0:
# print(False)
# break
# else:
# print(True)

elif task == "is_sorted":


s = input()
for i in range(len(s)-1):
if s[i] > s[i+1]:
print(False)
break
else:
print(True)

elif task == "any_true":


n = int(input())
for _ in range(n):
n = int(input())
if n % 3 == 0:
print(True)
break
else:
print(False)

elif task == "manhattan":


x, y = 0, 0
while True:
move = input().strip()
if move == "STOP":
break
elif move == "UP":
y += 1
elif move == "DOWN":
y -= 1
elif move == "LEFT":
x -= 1
elif move == "RIGHT":
x += 1
print(abs(x) + abs(y))

else:
print("Invalid task")

You might also like