Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import string
- import os
- # ---Question 1---
- class IceCreamSundae(object):
- def __init__(self, flavor, toppings):
- self.flavor = flavor
- self.toppings = toppings
- def does_it_have_nuts(self):
- if len(self.toppings) == 0:
- print('it has nothing')
- if 'nuts' in self.toppings:
- print('yes')
- else:
- print('no')
- AWESOME_SUNDAE = IceCreamSundae('vanilla',
- ['sprinkles', 'nuts', 'chocolate', 'caramel',
- 'coconut', 'whipped_cream', 'cherries'])
- # =========================================
- def eat_sundae(sundae):
- del sundae
- print('yum!')
- def combine_sundaes(self, other):
- new_toppings = self.toppings + other.toppings
- new_flavor = self.flavor + other.flavor
- return IceCreamSundae(new_flavor, new_toppings)
- # ---Question 2---
- sqrt_list = [math.sqrt(x) for x in range(10, 30)]
- three_four_multiples = [y for y in range(1,100) if y % 3 == 0 or y % 4 == 0]
- vowels = ['a', 'e', 'i', 'o', 'u']
- letters = list(string.ascii_lowercase)
- vowel_combs = [a + b for a in letters for b in letters if a in vowels or b in vowels]
- # ---Question 3---
- def gen_odd():
- i = 1
- while True:
- yield i
- i += 2
- def gen_powers():
- i = 0
- while True:
- yield 2**i
- i += 1
- def gen_e(x):
- i = 0
- sum = 0.0
- while True:
- sum += (x**i/math.factorial(i))
- yield sum
- i += 1
- # ---Question 4---
- fp = "C:/git/trader-interns-2019/python_course/object_oriented_python/wsmith/my_generator_results"
- os.mkdir(fp)
- with open(fp + "/odds", 'w') as file:
- gen = gen_odd()
- for i in range(5):
- file.write(str(next(gen)) + '\n')
- file.close()
- with open(fp + "/powers", 'w') as file:
- gen = gen_powers()
- for i in range(5):
- file.write(str(next(gen)) + '\n')
- file.close()
- with open(fp + "/exp", 'w') as file:
- gen = gen_e(2)
- for i in range(5):
- file.write(str(next(gen)) + '\n')
- file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement