Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #Question 1
  2.  
  3. def input_shopping_list():
  4. shoppingList= list()
  5. item = input("Enter an item: ")
  6. while item != "done":
  7. shoppingList.append(item)
  8. item = input("Enter an item: ")
  9. return shoppingList;
  10.  
  11.  
  12. shoppingList = input_shopping_list()
  13. count = 0
  14. print("\n")
  15. for i in shoppingList:
  16. print(i)
  17. count = count + 1
  18. print("Total items on shopping list: " + str(count))
  19.  
  20. # Question 2
  21. p=[1,2,3]
  22. q=['a', 1, 'b', 3]
  23. def find_common_elements(list_p, list_q):
  24. common_list = []
  25. for num in list_p:
  26. if num in list_q and num not in common_list:
  27. common_list.append(num)
  28. return common_list
  29.  
  30.  
  31. print(find_common_elements(p,q))
  32.  
  33. #Question 3
  34. def linearSearch(t, v):
  35. i = 0
  36. index = -1
  37. while i < len(t):
  38. if t[i] == v:
  39. index = i
  40. break
  41. i += 1
  42. return index
  43.  
  44.  
  45. print(linearSearch('egg', 'e'))
  46. print(linearSearch('egg', 'v'))
  47. print(linearSearch([1, 3, 2, 5], 2))
  48. print(linearSearch([1, 3, 2, 5], 6))
  49.  
  50. #Question 4
  51. pets = {
  52. "Hammond": "dog",
  53. "Mike": "fish",
  54. "Hunter": "zebra"
  55. }
  56.  
  57. for name, animal_type in pets.items():
  58. print(name + " is a " + animal_type + ".")
  59.  
  60. #Question 5
  61.  
  62. s = input("Enter a string:")
  63.  
  64. d = {'a':0, 'e':0, 'i':0, 'o':0, 'u':0}
  65.  
  66. for letter in s:
  67. if letter in d:
  68. d[letter] += 1
  69. for key in d:
  70. print("There are", d[key], "instances of", key, "in", s)
  71.  
  72. #Question 6
  73. inventory = {'gold': 150,
  74. 'pouch' : ['flint', 'twine', 'lucky charm'],
  75. 'backpack' : ['knife', 'bedroll', 'jerky']}
  76.  
  77. inventory['pocket'] = ['pebble', 'large chestnut', 'lint']
  78.  
  79. inventory['backpack'].sort()
  80.  
  81. inventory['backpack'][1] = 'crackers'
  82.  
  83. inventory['gold'] += 25
  84.  
  85. print(inventory)
  86.  
  87. #Question 7
  88. d={}
  89. d['Darci Lynne'] = 0
  90. d['Angelica Hale'] = 0
  91. d['Angelina Green'] = 0
  92.  
  93. while True:
  94. name = input('Enter contestant name:')
  95. if name == 'done' or name=='Done':
  96. break
  97. else:
  98. if name in d.keys():
  99. d[name] = d[name] + 1
  100. else:
  101. d[name] = 0
  102. for i in d.keys():
  103. print(i,'',d[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement