Guest User

Untitled

a guest
May 21st, 2019
63
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math
  2. import string
  3. import os
  4. # ---Question 1---
  5.  
  6. class IceCreamSundae(object):
  7. def __init__(self, flavor, toppings):
  8. self.flavor = flavor
  9.  
  10. self.toppings = toppings
  11.  
  12. def does_it_have_nuts(self):
  13. if len(self.toppings) == 0:
  14. print('it has nothing')
  15. if 'nuts' in self.toppings:
  16. print('yes')
  17. else:
  18. print('no')
  19.  
  20.  
  21. AWESOME_SUNDAE = IceCreamSundae('vanilla',
  22. ['sprinkles', 'nuts', 'chocolate', 'caramel',
  23. 'coconut', 'whipped_cream', 'cherries'])
  24.  
  25.  
  26. # =========================================
  27.  
  28.  
  29. def eat_sundae(sundae):
  30. del sundae
  31. print('yum!')
  32.  
  33.  
  34. def combine_sundaes(self, other):
  35. new_toppings = self.toppings + other.toppings
  36.  
  37. new_flavor = self.flavor + other.flavor
  38. return IceCreamSundae(new_flavor, new_toppings)
  39.  
  40. # ---Question 2---
  41.  
  42.  
  43. sqrt_list = [math.sqrt(x) for x in range(10, 30)]
  44.  
  45. three_four_multiples = [y for y in range(1,100) if y % 3 == 0 or y % 4 == 0]
  46.  
  47. vowels = ['a', 'e', 'i', 'o', 'u']
  48. letters = list(string.ascii_lowercase)
  49.  
  50. vowel_combs = [a + b for a in letters for b in letters if a in vowels or b in vowels]
  51.  
  52.  
  53. # ---Question 3---
  54.  
  55.  
  56. def gen_odd():
  57. i = 1
  58. while True:
  59. yield i
  60. i += 2
  61.  
  62.  
  63. def gen_powers():
  64. i = 0
  65. while True:
  66. yield 2**i
  67. i += 1
  68.  
  69. def gen_e(x):
  70. i = 0
  71.  
  72. sum = 0.0
  73. while True:
  74. sum += (x**i/math.factorial(i))
  75. yield sum
  76. i += 1
  77.  
  78. # ---Question 4---
  79.  
  80. fp = "C:/git/trader-interns-2019/python_course/object_oriented_python/wsmith/my_generator_results"
  81. os.mkdir(fp)
  82.  
  83. with open(fp + "/odds", 'w') as file:
  84. gen = gen_odd()
  85. for i in range(5):
  86. file.write(str(next(gen)) + '\n')
  87.  
  88. file.close()
  89.  
  90. with open(fp + "/powers", 'w') as file:
  91. gen = gen_powers()
  92. for i in range(5):
  93. file.write(str(next(gen)) + '\n')
  94.  
  95. file.close()
  96.  
  97. with open(fp + "/exp", 'w') as file:
  98. gen = gen_e(2)
  99. for i in range(5):
  100. file.write(str(next(gen)) + '\n')
  101.  
  102. file.close()
RAW Paste Data