# # Sets # # Practice # Preliminary notes: - Based on Chapter 8 - Aim: more than technique - Use only studied material. # Q. 1 # A list of pairs is given, Each pair includes # a string and three-digit positive integer. # Write a program that prints all integers # in which all digits are different. lst = [['a', 545], ['b', 789], ['c', 123]] for s, num in lst: if len(set(str(num))) == 3: print(num) # Q. 2 # Yasmin decided to open a restaurant. # She wishes to serve all dishes served # in the three exisiting restaurants in town. # Write a program that given menu lists # of the other three restaurant, produces # the menu of Yasmin-s restaurant, stored # in a list. The menu does not include duplicates. menus = [["soup", "pasta", "cake"], ["salad", "pasta", "cake"], ["soup", "pasta", "ice cream"] ] newMenu = set() for menu in menus: newMenu = newMenu | set(menu) print(list(newMenu)) menu1 = ["soup", "pasta", "cake"] menu2 = ["salad", "pasta", "cake"] menu = set(menu1) | set(menu2) menu3 = ["soup", "pasta", "ice cream"] menu = list(menu | set(menu3)) print(menu) Q. 3 Dan wants to know how many dishes served in his restaurant are not served in the three other restaurants in town. Write a program that prints this number. Dan = ["soup", "pasta", "cake", "pizza"] menus = [["soup", "pasta", "cake"], ["salad", "pasta", "cake"], ["soup", "pasta", "ice cream"] ] Dan = ["soup", "pasta", "cake", "pizza"] DanExclusive = set(Dan) for menu in menus: DanExclusive = DanExclusive - set(menu) print(len(DanExclusive)) Dan = ["soup", "pasta", "cake", "pizza"] menu1 = ["soup", "pasta", "cake"] DanExclusive = set(Dan) - set(menu1) menu2 = ["salad", "pasta", "cake"] DanExclusive = DanExclusive - set(menu2) print(DanExclusive) Q. 4 Adam prepared his restaurant-s menu as follows: he went to other restaurants in town, one after another, and added to his menu all dishes of each restaurant. He stopped collecting dishes when his menu included 4 dishes. Write a program that given all other menus prepares Dan-s. # with for menus = [["soup", "pasta", "cake"], ["salad", "pasta", "cake"], ["soup", "pasta", "ice cream"] ] newMenu = set() for menu in menus: newMenu = newMenu | set(menu) if (len(newMenu) == 4): break print(list(newMenu)) # with while menus = [["soup", "pasta", "cake"], ["salad", "pasta", "cake"], ["soup", "pasta", "ice cream"] ] newMenu = set() ind = 0 while len(newMenu) < 10 and ind < len(menus): newMenu = newMenu | set(menus[ind]) ind = ind + 1 print(list(newMenu)) # Q. 5 # You are provided with a list of lists. # Each inner list holds number of stars # given to three restaurants # in three different reviews. # Write a program that prints # True if at least one restaurant received # a number of starts not given to # the other restaurants, False otherwise. stars = [[5, 4, 3], [3, 4, 5], [4, 3, 5]] stars0 = set(stars[0]) result = False for currentStars in stars: if len(stars0 & set(currentStars)) != 3: result = True break print(result) # Q. 6 # Yasmin, Yona and Rona each may eat # a certain number of ice cream scoops # in one week. There are four types of # ice cream. Write a program that accepts pairs: # a child name and the type of ice cream s/he likes, # one after another, until there are no longer # scoops of any kind. If a child asks for a type # of ice crean which is no longer in stock # the program issues a proper message. # The program prints # the name of childs that ate ice cream. scoops = [1, 0, 0, 2] childs = set() while sum(scoops) != 0: name = input("Please enter your name: ") kind = int(input("Please enter ice cream kind 1-4: ")) if scoops[kind - 1] > 0: scoops[kind - 1] = scoops[kind - 1] - 1 childs.add(name) else: print("Sorry, maybe next time...") print(childs)