Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions students/Final/Final.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Final
Binary file added students/Final/ProductInventory.zip
Binary file not shown.
88 changes: 88 additions & 0 deletions students/Module04/Trigrams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#----------------------------------------#
# Title: Trigrams.py
# Initial File
# Claire Yoon,2019-02-03,New file
#----------------------------------------#

"""
build up the trigrams dict from the list of words

returns a dict with:
keys: word pairs
values: list of followers
"""

import sys
import string
import random
# from string import maketrans

def read_in_data(filename):
objFile = open(filename, "r")
strData = objFile.read()
# return text in one line
return strData

def make_words(in_data):
in_data.replace(" *** START OF THIS PROJECT GUTENBERG EBOOK",'')
in_data = in_data.translate({ord(c): " " for c in "\"!@#$%^&*()[]{};:,/<>?\|~-=_+"})
words = in_data.split()
# print(words)
return words

def build_trigram(words):

trigrams = {}
# why -2?: last two words would not have next one.
for i in range(len(words) - 2):
pair = words[i:i + 2]
follower = words[i + 2]
val = trigrams.get((pair[0], pair[1]))

if val is None :
val = [follower]
else :
val.append(follower)
trigrams.update({(pair[0], pair[1]): val})

# build up the dict here!
# print(trigrams)
return trigrams

def build_text(word_pairs):
# returns a number between 0 and len(word_pairs) - 1
starting_point = random.randint(0,len(word_pairs) - 1)
# get word list of key of starting point
start_word = list(word_pairs.keys())[starting_point]
new_text = start_word[0] + ' ' + start_word[1]
# while the pair has next word
while(True):
word_list = new_text.split()
new_tuple = (word_list[-2] , word_list[-1])
# if the tuple is one of the keys of word_pairs:
if new_tuple in word_pairs.keys():
word_values = word_pairs.get(new_tuple)
rand_val = random.randint(0,len(word_values) - 1)
join_word = word_values[rand_val]
new_text = new_text + ' ' + join_word

# print(new_text)
# else: terminate the function
else:
break
return new_text

if __name__ == "__main__":
# get the filename from the command line
try:
filename = sys.argv[1]
except IndexError:
print("You must pass in a filename")
sys.exit(1)

in_data = read_in_data(filename)
words = make_words(in_data)
word_pairs = build_trigram(words)
new_text = build_text(word_pairs)

print(new_text)
93 changes: 93 additions & 0 deletions students/Module04/mailroom2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#----------------------------------------#
# Title: mailroom2.py
# Initial File
# Claire Yoon,2019-02-03,New file
#----------------------------------------#


# Convert main donor data structure to be a dict.
donor_dict = { 'Will Gates': [10000, 20000, 300000],
'Conan Obrien': [20000],
'Mark Zucker' : [30000, 12000],
'Jeff Bezos' : [50000],
'Tom Hardy' : [12000, 40000]
}

def thx_single():
while(True):
temp = []
for i in donor_dict.keys():
temp.append(i)
name = input("Please enter a full name (enter 'list' to see list): ")
# If user types 'list'
if name.lower() == 'list':
print(temp)
# If user types other than list - applies the same logic
else:
amount = float(input("Please enter amount of donation (in numbers): "))
if name in temp:
donors_amount = donor_dict.get(name)
donors_amount.append(amount)
history_amount = 0
for i in range(len(donors_amount)):
history_amount += donors_amount[i]
else :
donor_dict.update({name : amount})
history_amount = amount

print("\nDear", name, "\n\n I would like to thank you for your donation of $",history_amount,"so far.\n",
"Our organization relies on the generosity of donors such as yourself. \n Thank you once again. \n")
break
return

def create_report():
report_lst= []

for i in donor_dict.keys():
report_lst.append([i, sum(donor_dict.get(i)), len(donor_dict.get(i)), sum(donor_dict.get(i))/len(donor_dict.get(i))])

print()
print("<<DONATION REPORT>>")

print("Donor Name | Total Given | Num Gifts | Average Gift\n------------------------------------------------------------")
for l in report_lst:
print ('{:<20}'.format(l[0]), end='')
print ('$','{:<12}'.format(l[1]), end='')
print ('{:<11}'.format(l[2]), end='')
print ('$','{:<12}'.format(l[3]))
print()

def thx_all():
for i in donor_dict.keys():
# get donor's amount of donation
amount = sum(donor_dict.get(i))
donor_name = i.split(' ')
filename = "thx_letter"
# generate file name as : thx_letter_(firstname)_(lastname)
for j in range(len(donor_name)):
filename += '_'+(donor_name[j])
# open a file and write body
letter = open(filename, "w")
letter_body = str("\nDear "+ str(donor_name[0]) + ' ' + str(donor_name[1]) + ",\n\nI would like to thank you for your donation of $" + str(amount) +" so far.\n" +
"Our organization relies on the generosity of donors such as yourself. \n\nThank you once again. \n")

letter.write(letter_body)
letter.close()
print("\n All thank-you letters have been created. Please check your current folder. \n ")
return

def equit():
exit()


def main_menu():
while(True):
# a dict to switch between the user’s selections.
menu_dict = {1: thx_single, 2: create_report, 3: thx_all, 4: equit }
menu = int(input("Choose an action: \n 1 - Send a Thank You to a single donor \n 2 - Create a Report \n 3 - Send letters to all donors. \n 4 - Quit \n : "))
menu_dict.get(menu)()

return

if __name__ == '__main__':
main_menu()
58 changes: 58 additions & 0 deletions students/Module05/except_excercise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#----------------------------------------#
# Title: except_excercise.py
# Initial File
# Claire Yoon,2019-02-09,New file
#----------------------------------------#

#!/usr/bin/python

"""
An exercise in playing with Exceptions.
Make lots of try/except blocks for fun and profit.

Make sure to catch specifically the error you find, rather than all errors.
"""

from except_test import fun, more_fun, last_fun


# Figure out what the exception is, catch it and while still
# in that catch block, try again with the second item in the list
first_try = ['spam', 'cheese', 'mr death']

try:
joke = fun(first_try[0])
except NameError as e:
joke = fun('cheese')
# Here is a try/except block. Add an else that prints not_joke
try:
not_joke = fun(first_try[2])
except SyntaxError:
print('Run Away!')
else:
print(not_joke)

# What did that do? You can think of else in this context, as well as in
# loops as meaning: "else if nothing went wrong"
# (no breaks in loops, no exceptions in try blocks)

# Figure out what the exception is, catch it and in that same block
#
# try calling the more_fun function with the 2nd language in the list,
# again assigning it to more_joke.
#
# If there are no exceptions, call the more_fun function with the last
# language in the list

# Finally, while still in the try/except block and regardless of whether
# there were any exceptions, call the function last_fun with no
# parameters. (pun intended)

langs = ['java', 'c', 'python']

try:
more_joke = more_fun(langs[0])
except Exception as e:
more_joke = more_fun(langs[1])
finally:
last_fun()
107 changes: 107 additions & 0 deletions students/Module05/mailroom3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#----------------------------------------#
# Title: mailroom3.py
# Revised mailroom2.py
# Claire Yoon,2019-02-09,New file
#----------------------------------------#


# Convert main donor data structure to be a dict.
donor_dict = { 'Will Gates': [10000, 20000, 300000],
'Conan Obrien': [20000],
'Mark Zucker' : [30000, 12000],
'Jeff Bezos' : [50000],
'Tom Hardy' : [12000, 40000]
}

name_mapping = {i: i.lower() for i in donor_dict.keys()}

def thx_single():
while(True):
name = input("Please enter a full name (enter 'list' to see list): ")

# If user types 'list'
if name.lower() == 'list':
print([i for i in name_mapping.keys()])

# If user types other than list - applies the same logic
else:
amount = float(input("Please enter amount of donation (in numbers): "))
if name.lower() in name_mapping.values():
for camel, lower in name_mapping.items():
if lower == name.lower():
name = camel
donors_amount = donor_dict.get(name)
# append input amount value to donors_amount list
donors_amount.append(amount)
history_amount = 0
for i in range(len(donors_amount)):
history_amount += donors_amount[i]

#for newly added name
else :
name_toList = name.lower().split(' ')
for i in range(len(name_toList)):
name_toList[i] = name_toList[i].capitalize()
name = " ".join(name_toList)
new_amount = {name : [amount]}
donor_dict.update(new_amount)
print(donor_dict)
history_amount = amount
name_mapping.update({name: name.lower()})

print("\nDear", name, ",\n\nOur organization would like to thank you for your donation of $",history_amount,"so far.\n",
"Our organization relies on the generosity of donors such as yourself. \n Thank you once again. \n")
break
return

def create_report():
report_lst= []

#using comprehension
[report_lst.append([i, sum(donor_dict.get(i)), len(donor_dict.get(i)), sum(donor_dict.get(i))/len(donor_dict.get(i))]) for i in donor_dict.keys()]
print()
print("\t\t\t\t<<DONATION REPORT>>")

print("Donor Name | Total Given | Num Gifts | Average Gift\n------------------------------------------------------------")
for l in report_lst:
print ('{:<20}'.format(l[0]), end='')
print ('$','{:<12}'.format(l[1]), end='')
print ('{:<11}'.format(l[2]), end='')
print ('$','{:<12}'.format(l[3]))
print()

def thx_all():
for i in donor_dict.keys():
# get donor's amount of donation
amount = sum(donor_dict.get(i))
donor_name = i.split(' ')
filename = '_'.join(donor_name) + '_Thx_Letter'
# open a file and write body
letter = open(filename, "w")
letter_body = str("\nDear "+ str(donor_name[0]) + ' ' + str(donor_name[1]) + ",\n\nOur organization would like to thank you for your donation of $" + str(amount) +" so far.\n" +
"Our organization relies on the generosity of donors such as yourself. \n\nThank you once again. \n")

letter.write(letter_body)
letter.close()
print("\n All thank-you letters have been created. Please check your current folder. \n ")
return

def equit():
exit()

def main_menu():
while(True):
# a dict to switch between the user’s selections.
menu_dict = {1: thx_single, 2: create_report, 3: thx_all, 4: equit}
try:
menu = int(input("Choose an action: \n 1 - Send a Thank You to a single donor \n 2 - Create a Report \n 3 - Send letters to all donors. \n 4 - Quit \n : "))
menu_dict.get(menu)()
except ValueError as e :
print("** Warning: Please enter number **")
except TypeError as e:
print("** Warning: Please enter valid menu (1 - 4) **")

return

if __name__ == '__main__':
main_menu()
Loading