Python Week 3 All GrPa's Solutions
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")
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)
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")
if task == 'factorial':
n = int(input())
result = 1
for i in range(1, n + 1):
result *= i
print(result)
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])
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)
else:
print("Invalid task")