# # For loop in strings and lists - Practice # # Part III - Scanning indices # # 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 - Completions for previous video # Q. 1 # Write a program that receives any list # including it least two values, # and prints True if it contains at least # adjacent pair of equal values, False otherwise. #lst = eval(input("PLease enter any list: ")) # scanning values lst = [ 3, 4, 4, 5, 5, 3, 9] lastItem = lst[0] result = False for item in lst[1:]: if item == lastItem: result = True lastItem = item print(result) lst = ['a', 'b', 'c'] 0 1 2 range(3) 0, 1, 2 for i in range(len(lst)): # scanning indices lst = [ 3, 4, 4, 5, 5, 3, 9] 0 1 2 3 4 5 6 -2 -1 result = False #for item in lst[1:]: for i in range(1, len(lst)): if lst[i] == lst[i-1]: result = True print(result) # Q. 2 # Write a program that receives a list of # non-negative integers, and prints True # if at least one of the integers # equals the index in which it resides, # False otherwise. #lst = eval(input("PLease enter any list: ")) # scanning values lst = [ 3, 4, 4, 5, 4, 5] ind = 0 result = False for item in lst: if item == ind: result = True ind = ind + 1 print(result) #lst = eval(input("PLease enter any list: ")) # scanning indices lst = [ 3, 4, 4, 5, 999, 4, 5] result = False for i in range(len(lst)): if lst[i] == i: result = True print(result) # Q. 3 # Write a program that receives any list, # and prints all values in odd indices, one # after another, each in a separate line. #lst = eval(input("PLease enter any list: ")) # scanning indices lst = [ 3, 4, 4, 5, 999, 4, 5] for i in range(1, len(lst), 2): print(lst[i]) print(range(3)) print(list(range(3))) print(list(range(3, 8))) print(list(range(3, 8, 3))) print(list(range(1, 10, 4))) # Q. 4 # Write a program that receives a list of integers # and an integer, and returns the index of the # first appearance of the integer in the list. # If the integer is not in the list # the program issues a proper message. #lst = eval(input("PLease enter any list: ")) # scanning indices lst = [ 3, 4, 4, 5, 999, 4, 5] num = int(input("Please enter an integer: ")) result = "Not found." for i in range(len(lst)): if result == "Not found." and lst[i] == num: result = i print(result) # with break lst = [ 3, 4, 4, 5, 999, 4, 5] num = int(input("Please enter an integer: ")) result = "Not found." for i in range(len(lst)): if lst[i] == num: result = i break print(result) # Q. 5 # Write a program that receives any string and prints # its first character, then the two first characters, # next the first three character and so forth, until # it prints the entire string. s = "ice cube" 01234567 for i in range(1, len(s) + 1): print(s[0:i]) s[0:7] # Q. 6 # Write a program that receives an integer comprised # of three digits or more and prints the two # adjacent digits whose difference one from the other # is the biggest. You may assume there are only # two such digits. "25417" num = input("Please enter an integer greater than 99: ") #num = "25417" # 01234 maxNum = 0 for i in range(len(num) - 1): diff = abs(int(num[i + 1]) - int(num[i])) if maxNum < diff: maxNum = diff position = i print(position) # with max() #num = input("Please enter an integer greater than 99: ") num = "25417" maxs = [] for i in range(len(num) - 1): maxs = maxs + [abs(int(num[i + 1]) - int(num[i]))] print(max(maxs)) print(sum([5, 3, 2, 1])) print(abs(-7)) # Q. 7 # Write a program that receives a title of a book. # The title comprises several words, and each # two words are separated by a single space. # The program prints a string comprising the # intiials of the words. E.g. if the title is # Looking Up Again, the program prints LUP. title = input("Please input a title of a book: ") #title = "Looking up again" # 0123456789 initials = title[0] for i in range(1, len(title)): if title[i-1] == " ": initials = initials + title[i] print(initials) # Q. 8 # A TikTok addict sets to herself the task # of reducing in each of the five coming days, # the time she wastes in this social network. # Write a program that receives the amount of time # (in minuets) she uses TikTok in each of these days, # The program prints "OK" if in each day beginning # in the second the number of minuets was smaller # than in the previous day. If that was not the case, # the program prints the numbers representing the # first pair of days in which there was no reduction, # e.g. 2-3. #minuets = eval(input("Please enter list of minuets: ")) minuets = [83, 77, 88, 88, 11] result = True for i in range(len(minuets) - 1): if result == True and (minuets[i + 1] - minuets[i] >= 0): first = i second = i + 1 result = False if result: print("OK") else: print(first, second) # Q. 9 # Write a program that receives a list of non-negative # integers and produces a list in which each value # in index k is the sum of all values in range 0-k. # E.g. if the list is the following # [1, 2, 3] # the produced list is # [1, 3, 6] lst = eval(input("Please enter a list of non-negative integers: ")) # lst = [2, 2, 3, 10] sums = [] for i in range(1, len(lst) + 1): sumNums = 0 for num in lst[0:i]: sumNums = sumNums + num sums = sums + [sumNums] print(sums) # using sum #lst = eval(input("Please enter a list of non-negative integers: ")) lst = [2, 2, 3, 10] sums = [] for i in range(1, len(lst) + 1): sums = sums + [sum(lst[0:i])] print(sums) print(lst[0:1]) print(lst[0:2]) print(lst[0:3]) 0 0 0 1 0 2 # Q. 10 # 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 = " AAA BBB CCC " newS = "" for i in range(len(s)): if not((s[i:i+2] == " ") or s[i] == " " and i == len(s) - 1 or s[i] == " " and newS == "") : newS = newS + s[i] print(newS + "-") 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 = eval(input("Please enter any list of integers: ")) -1 -3, 10 0 1 2 lst = [-3, -1, 10, 0, 0, -7, 5] newLst = [] negNum = 0 for num in lst: if num > 0: newLst = newLst + [num] elif num < 0: newLst = [num] + newLst negNum = negNum + 1 else: newLst[negNum:negNum] = [0] print(newLst) Q. 12 Write a program that receives two lists, lst1 and lst2. Each list is of length 5, and includes only integers. E.g.: lst1 = [2, 3, 1, 2, 3] lst2 = [1, 3, 2, 2, 3] Next the program procues a third list of length 5, which holds the multiplications of the integers in corresponding indices in the two entered list. Thus for the two example list the following list is produced and printed: [2, 9, 2, 4, 9] lst1 = [2, 3, 1, 2, 3, 6] lst2 = [1, 3, 2, 2, 3] newLst = [] for i in range(len(lst1)): newLst = newLst + [ lst1[i] * lst2[i] ] print(newLst)