# # Nested loops # # Practice # # Preliminary notes: - Presumed knowledge: - That practiced in pervious videos - Basic structure of strings, lists - for loop - The functions range(), length(), eval() - The operators in, *, + - Iterative consturction of strings and lists - Relation to tuples - Use only the studied material. In particular do not use type functions # Q. 1 # Write a program that receives a positive integer n, # and then prints a triangle with the following strucutre: # 1...n # 1..n-1 # ... # 12 # 1 # E.g. if n == 5 the program prints: # 12345 # 1234 # 123 # 12 # 1 n = int("Please enter a positive integer: ") #n = 9 lst = list(range(1, n + 1)) [1, 2, 3, 4, 5] 0 1 2 3 4 for i in range(n): for num in range(1, n + 1 - i): print(num, end = "") print() 12345 i = 0 range(1, n + 1) 1234 i = 1 range(1, n + 1 - i) 123 i = 2 range(1, n + 1 - i) 12 i = 3 range(1, n + 1 - i) 1 i = 4 range(1, n + 1 - i) for i in range(n): for num in range(1, n + 1): print(num, end = "") print() line = "" for num in lst: line = line + str(num) print(line) Q. 2 Write a program that receives a positive integer n, and then prints a triangle with the following strucutre: n.....1 n-1..1 ... 21 1 E.g. if n == 5 the program prints: 54321 5432 543 54 5 # solution 1 #n = int("Please enter a positive integer: ") #n = 9 for i in range(n): line = "" for num in range(1 + i, n + 1): line = str(num) + line print(line) # solution 2 n = int("Please enter a positive integer: ") #n = 9 for i in range(n): for num in range(n, i, -1): print(num, end = "") print() range(3) range(1, 7) print(list(range(3, 10, 4))) print(list(range(10, 3, -4))) # Q. 3 # Write a program that receives a positive integer n # greater than 1, and prints all pairs of positive integers whose # sum is n. Each pair is printed once, and in each # printed pair, the first integer is smaller # than the second or equal to it. # E.g. if n == 6 the program prints # 1 5 # 2 4 # 3 3 # Challenge: do the same for triples, # e.g. if n == 6 the program prints: # 1 1 4 # 1 2 3 # 2 2 2 1...n-l 1...5 n = 6 for i in range(1, n): for j in range(1, n): if i <= j and i + j == n: print(i, j) # Q. 4 # Write a program that receives an integer comprised # of three or more digits, and prints the two digits # whose difference one from the other is the biggest. # Assume there are only two such digits and that # they appear anywhere in the integer. num = input("Please enter an integer greater than 99: ") #num = "74539" maxDiff = 0 for digit1 in num: for digit2 in num: if maxDiff < abs(int(digit1) - int(digit2)): maxDiff = abs(int(digit1) - int(digit2)) pair1 = digit1 pair2 = digit2 print(pair1, pair2 ) # Q. 5 # Write a program that receives a non-empty list # of numbers and prints True if no number # appears in the list more than once, False # otehrwise. # lst = eval(input("Please enter a non-empty list: ")) lst = [5, 5, 3, 2, 6, 7, 7] result = True for num1 in lst: counter = 0 for num2 in lst: if num2 == num1: counter = counter + 1 if counter > 1: result = False print(result) # Q. 6 # Write a program tha produces 10 samples # of size 5 from the range 1:20, without revieion. import random for i in range(10): mySample = [] while len(mySample) < 5: randNum = random.randint(1, 20) if randNum not in mySample: mySample = mySample + [randNum] print(mySample)