# # Iterative construction of strings and lists # # Practice # # THE PROBLEM 3 7 9 5 6 [3, 4, 7, 9, 5, 6] "Kolargol" "oa" # Preliminary notes: - Presumed knowledge: - That practiced in pervious videos - Basic structure of strings, lists - while loop and for loop - The functions range(), length(), eval() - The operators in, not in, *, + - Relation to tuples - Use only the studied material. In particular do not use type functions, save, perhaps, for append() # INTRODUCTORY REMARK ABOUT THE NATURE OF # THE FOR LOOP AND ABOUT ITS KINDS # NOW TO THE QUESTIONS # Q. 1 # Write a program that receives strings one after another, # until an empty string is entered. The program # concatenates the strings in the order of their reception, # and prints the resulting string. s = input("Please enter any string: ") newS = "" while s != "": newS = newS + s + " " s = input("Please enter any string: ") print("THe concatenated string s: ", newS) newS = newS[:-1] print(newS + "-") "chair" "table" # Q. 2 # Write a program that receives characters (strings # of length 1) one after another, until the string # stop is entered. The program prints a string # comprising all the entered characters, in a # reversed order. E.g. if the program receives the # characters t, then a, then c, the program prints # the string cat. c = input("Please enter any character: ") newS = "" while c != "stop": newS = c + newS c = input("Please enter any character: ") print("THe concatenated string s: ", newS) # variation with for s = "ybab" newS = "" for c in s: newS = c + newS print("THe concatenated string s: ", newS) # Q. 3 # Write a program that receives a string s, # and prints a string based on s, with no # character duplication. E.g. if the received # string is abbeecdad, the printed string # is abecd. s = input("Please input any string: ") newS = "" for c in s: if c not in newS: newS = newS + c print("The string without duplicates is: ", newS) # Q. 4 # Write a program that prints all odd numbers in range # 1-100 (including 100). The numbers are printed in # a single line, each separated from the next with # a comma. # version 1 newS = "" for i in range(1, 101): newS = newS + str(i) + ", " print(newS) # version 2 for i in range(1, 101): print(i, end = ", ") # Q. 5 # Write a program that receives any string. # Any number of spaces may appear at the beginning # of the string, in the middle of it, and at its end. # The program prints the string without leading # and closing spaces, and also with no double spaces. #s = " Prof. Albus Dumbledore " s = input("Please enter any string: ") newS = "" lastC = "" for c in s: if not (c == " " and (newS == "" or lastC == " ")): newS = newS + c lastC = c print(newS + "-") # Q. 6 # Write a program that produces a list # of 10 random numbers in range 1-10. import random lst = [] for i in range(10): lst = lst + [random.randint(1, 10)] print(lst) print([3, 4] + [5]) # A WORD ABOUT + VS. APPEND # another implementation import random lst = [] for i in range(10): lst.append( random.randint(1, 10) ) print(lst) # Q. 7 # Write a program that receives student names # one after another, until the string -1 is entered. # The program produces a list comprising all entered # names. Next the program receives another name # and checks whether it is one of the names # in the list. If it is not, it should be added # to the end of the list. studName = input("Please enter a student's name: ") names = [] while studName != "-1": names = names + [studName] studName = input("Please enter a student's name: ") studName = input("Please enter a student's name: ") if studName not in names: names = names + [studName] print(names) # Q. 8 # The readers club Ulysses accepts as members # only readers who have read more than 5000 # books during the last 5 years. Write a program # that receives, one after another, pairs of # reader name and number of books s/he read # during the last 5 years, until the string q is entered. # The program produces a list including # the names of readers who may be # accepted as members of the club. name = input("Please input name: ") names = [] while name != "q": booksNum = int(input("Please input number of books: ")) if booksNum > 5000: names = names + [name] name = input("Please input name: ") print(names) # Q. 9 # Write a program that receives a list of temperatures. # The program produces a list including all # temperatures below 0, and a separate list # including all other entered temperatures. ts = eval(input("PLease enter a list of temperatures: ")) #print(ts) #print(type(ts)) under0 = [] rest = [] for t in ts: if t < 0: under0 = under0 + [t] else: rest = rest + [t] print(under0) print(rest) # Q. 10 # Write a program that receives two lists, # lst1 and lst2. lst1 includes 5 integers. # lst2 includes all integers in range 0-4, # arranged in a random number. E.g. # lst1 = [81, 24, 12, 58, 32] # lst2 = [1, 3, 2, 4, 0] # The program scans the numbers in lst2, # using them as indices to lst1, and # for printing the numbers it includes. : # Thus for the example lists the program prints # the the numbers in lst2 in the following order: # 24, 58, 12, 32, 81 lst1 = [81, 24, 12, 58, 32] lst2 = [1, 3, 2, 4, 0] for ind in lst2: print(lst1[ind]) Q. 11 Write a program that receives a list of integers and rearranges it such that all negative integers come before 0, which itself comes before all positive integers. #lst = [7, -3, 0, 0, 0, 5, -1] lst = eval(input("Please enter any list of integers: ")) #[-3, -1, 0, 0, 0, 7, 5] negatives = [] zeros = [] positives = [] for num in lst: if num > 0: positives = positives + [num] elif num < 0: negatives = negatives + [num] else: zeros = zeros + [num] newLst = negatives + zeros + positives print(newLst) # forthcoming... lst = eval(input("Please enter any list of integers: ")) #[-3, -1, 10, 0, 0, 7, 5] newLst = [] for num in lst: if num > 0: newLst = newLst + [num] elif num < 0: newLst = [num] + newLst else: ? print(newLst) -3 -1 -3 -1 -3 0 10