0% found this document useful (0 votes)
12 views

Python Week 3 All GrPa's Solutions

The document contains solutions for various programming tasks in Python, organized into four groups (GrPA 1 to GrPA 4). Each group includes a set of tasks that require specific functionalities without using certain programming constructs like loops. The tasks cover a range of operations including summation, string manipulation, and mathematical calculations.

Uploaded by

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

Python Week 3 All GrPa's Solutions

The document contains solutions for various programming tasks in Python, organized into four groups (GrPA 1 to GrPA 4). Each group includes a set of tasks that require specific functionalities without using certain programming constructs like loops. The tasks cover a range of operations including summation, string manipulation, and mathematical calculations.

Uploaded by

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

Python Week 3 All GrPa's Solutions

GrPA 1

# 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 from 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":


result = []
while True:
word = input().strip()
if word.lower() == "stop":
break
if word.endswith("ed") or word.endswith("ing"):
result.append(word)
print("\n".join(result)) # Print each word on a new line

elif task == "reverse_sum_palindrome":


def is_palindrome(n):
s = str(n)
return s == s[::-1]
result = []
while True:
number = int(input())
if number == -1:
break
reversed_number = int(str(number)[::-1])
if is_palindrome(number + reversed_number):
result.append(number)
print("\n".join(map(str, result))) # Print each number on a new line

elif task == "double_string":


while True:
text = input().strip()
if text == "":
break
print(text * 2)

elif task == "odd_char":


result = []
while True:
text = input().strip()
if text.endswith("."):
result.append(text[::2]) # Extract characters at even indices (0-based)
break
result.append(text[::2]) # Extract characters at even indices (0-based)
print(" ".join(result))

elif task == "only_even_squares":


result = []
while True:
number = input().strip()
if number.lower() == "nan":
break
number = int(number)
if number % 2 == 0:
result.append(number ** 2)
print("\n".join(map(str, result))) # Print each square on a new line

elif task == "only_odd_lines":


result = []
line_count = 1
while True:
line = input().strip()
if line == "END":
break
if line_count % 2 != 0:
result.insert(0, line) # prepend the line
line_count += 1
print("\n".join(result))

GrPA 2

# 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 = sum(i for i in range(n) if i % 4 != 0 and i % 5 != 0)
print(total)

elif task == 'from_k':


n = int(input())
k = int(input())
count = 0
for num in range(k, 0, -1):
if count >= n:
break
if '5' not in str(num) and '9' not in str(num) and num % 2 != 0:
print(str(num)[::-1])
count += 1

elif task == 'string_iter':


s = input()
prev_digit = 1
for char in s:
digit = int(char)
print(prev_digit * digit)
prev_digit = digit

elif task == 'list_iter':


lst = eval(input()) # this will load the list from input
for elem in lst:
print(f"{elem} - type: {type(elem)}")

else:
print("Invalid")

GrPA 3

task = input()

if task == 'permutation':
s = input().strip()
for i in range(len(s)):
for j in range(len(s)):
if i != j: # Ensure no repetition of characters
print(s[i] + s[j])

elif task == 'sorted_permutation':


s = input().strip()
for i in range(len(s)):
for j in range(len(s)):
if i != j and s[i] <= s[j]: # Ensure no repetition and print only if sorted
print(s[i] + s[j])

elif task == 'repeat_the_repeat':


n = int(input())
for _ in range(n):
print("".join(map(str, range(1, n + 1))))

elif task == 'repeat_incrementally':


n = int(input())
for i in range(1, n + 1):
print("".join(map(str, range(1, i + 1))))

elif task == 'increment_and_decrement':


n = int(input())
for i in range(1, n + 1):
line = "".join(map(str, range(1, i + 1)))
line += line[-2::-1] # reverse the line excluding the last character
print(line)

else:
print("Invalid task")

GrPA 4

# this is to ensure that you cannot use the built in any, all and min function for this exercise but
you can use it in the OPPEs.
any = None
all = None
min = None

task = input()

if task == 'factors':
n = int(input().strip())
factors = []
for i in range(1, n + 1):
if n % i == 0:
factors.append(i)
for factor in factors:
print(factor)

elif task == 'find_min':


n = int(input().strip())
numbers = []
for _ in range(n):
numbers.append(int(input().strip()))
minimum = numbers[0]
for num in numbers[1:]:
if num < minimum:
minimum = num
print(minimum)

elif task == 'prime_check':


n = int(input().strip())
is_prime = True
if n <= 1:
is_prime = False
else:
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
is_prime = False
break
print(is_prime)

elif task == 'is_sorted':


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

elif task == 'any_true':


n = int(input().strip())
any_divisible_by_3 = False
for _ in range(n):
number = int(input().strip())
if number % 3 == 0:
any_divisible_by_3 = True
break
print(any_divisible_by_3)

elif task == 'manhattan':


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

else:
print("Invalid task")

You might also like