From e44850903a6f9d7b2f4f9c813da59f667542ff2b Mon Sep 17 00:00:00 2001 From: Meowse Date: Sun, 25 Jan 2015 16:05:37 -0800 Subject: [PATCH 1/3] Update README.rst --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index 835744c3..bb0e70cc 100644 --- a/README.rst +++ b/README.rst @@ -13,3 +13,4 @@ Class lecture materials are available in a rendered version from: http://UWPCE-PythonCert.github.io/IntroToPython +Change for the sake of change (to verify that students can pull in changes from my repository) From 99c4dec02dd135bb10a61e19edfdd949aa1725d2 Mon Sep 17 00:00:00 2001 From: tzane Date: Sun, 25 Jan 2015 16:23:27 -0800 Subject: [PATCH 2/3] Adding session2 HW --- Students/Tyler G/session2/Ack.py | 26 ++++++++++++ .../Tyler G/session2/Session2_ExtraCredit.py | 23 +++++++++++ Students/Tyler G/session2/series.py | 40 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 Students/Tyler G/session2/Ack.py create mode 100644 Students/Tyler G/session2/Session2_ExtraCredit.py create mode 100644 Students/Tyler G/session2/series.py diff --git a/Students/Tyler G/session2/Ack.py b/Students/Tyler G/session2/Ack.py new file mode 100644 index 00000000..e50d11a0 --- /dev/null +++ b/Students/Tyler G/session2/Ack.py @@ -0,0 +1,26 @@ +import sys + +def Ack(m, n): + """ + This is the Ackerman function + which is recursive and I don't understand + all that well. It returns a non-negative integer. + """ + if m < 0 or n < 0: + return None + if m == 0: + return n + 1 + elif m > 0 and n == 0: + return Ack(m-1,1) + elif m > 0 and n > 0: + return Ack(m-1,Ack(m,n-1)) + +def main(): + m_entry = int((sys.argv[1])) + n_entry = int((sys.argv[2])) + + print Ack(m_entry, n_entry) + +if __name__ == "__main__": + main() + diff --git a/Students/Tyler G/session2/Session2_ExtraCredit.py b/Students/Tyler G/session2/Session2_ExtraCredit.py new file mode 100644 index 00000000..cee015f7 --- /dev/null +++ b/Students/Tyler G/session2/Session2_ExtraCredit.py @@ -0,0 +1,23 @@ + + +#Session 2 Extra Credit +def FizzBuzzFrodoBilboGandalf(n): + for i in range(1,n+1): + holder = " " + if i % 3 == 0: + holder += "Fizz" + if i % 5 == 0: + holder += "Buzz" + if i % 7 == 0: + holder += "Frodo" + if i % 11 == 0: + holder += "Bilbo" + if i % 13 == 0: + holder += "Gandalf" + if holder == " ": + print i + else: + print holder.lstrip(" ") + + +FizzBuzzFrodoBilboGandalf(100) \ No newline at end of file diff --git a/Students/Tyler G/session2/series.py b/Students/Tyler G/session2/series.py new file mode 100644 index 00000000..ee6576df --- /dev/null +++ b/Students/Tyler G/session2/series.py @@ -0,0 +1,40 @@ +def fibonacci(n): + """ + Returns the nth value of the fibonacci sequence. + """ + sequence = [0,1] + for i in range(n-2): + sequence.append(sequence[len(sequence)-2] + sequence[len(sequence)-1]) + return sequence[n-1] + +def lucas(n): + """ + Returns the nth value of the lucas sequence. It is + the same as the fibonacci series except the starting + numbers are 2 and 1 instead of 0 and 1. + """ + sequence = [2,1] + for i in range(n-2): + sequence.append(sequence[len(sequence)-2] + sequence[len(sequence)-1]) + return sequence[n-1] + +def sum_series(n, o = 0, p = 1): + """ + First parameter returns nth value of series. The second + and third optional parameters allows the user to define + the first two values in the fibonacci sequence. + """ + sequence = [o, p] + for i in range(n-2): + sequence.append(sequence[len(sequence)-2] + sequence[len(sequence)-1]) + return sequence[n-1] + +def main(): + n_entry = int((sys.argv[1])) + o_entry = int((sys.argv[2])) + p_entry = int((sys.argv[3])) + + print sum_series(n_entry, o_entry, p_entry) + +if __name__ == "__main__": + main() \ No newline at end of file From f5b6bc0d5dea7e2a1a433162dcce7b16cf2c87b3 Mon Sep 17 00:00:00 2001 From: tzane Date: Fri, 30 Jan 2015 16:31:36 -0800 Subject: [PATCH 3/3] Add session3 HW --- Students/Tyler G/session3/list_lab.py | 85 ++++++++++++++++++++++ Students/Tyler G/session3/mailroom.py | 93 +++++++++++++++++++++++++ Students/Tyler G/session3/rot13.py | 30 ++++++++ Students/Tyler G/session3/string_lab.py | 40 +++++++++++ 4 files changed, 248 insertions(+) create mode 100644 Students/Tyler G/session3/list_lab.py create mode 100644 Students/Tyler G/session3/mailroom.py create mode 100644 Students/Tyler G/session3/rot13.py create mode 100644 Students/Tyler G/session3/string_lab.py diff --git a/Students/Tyler G/session3/list_lab.py b/Students/Tyler G/session3/list_lab.py new file mode 100644 index 00000000..592c98d9 --- /dev/null +++ b/Students/Tyler G/session3/list_lab.py @@ -0,0 +1,85 @@ +fruits = ["Apples", "Pears", "Oranges", "Peaches"] + +for fruit in fruits: + print fruit + +new_fruit = raw_input("What kind of fruit would you like you to add to the list? ") +fruits.append(new_fruit) + +for fruit in fruits: + print fruit + +number = raw_input("Enter a whole number: ") +try: + number = int(number) +except ValueError: + print "Must be a whole number!" + +x = 0 +for i in range(number,number + len(fruits)): + print "%i is to %s." % (number, fruits[x]) + number += 1 + x += 1 + +fruits.insert(0,"Bananas") + +print "Only fruits which start with 'p':" +for fruit in fruits: + if fruit[0].lower() == "p": + print fruit + +print "Full list:" +for fruit in fruits: + print fruit + +fruits.pop(0) + +print "First element removed:" +for fruit in fruits: + print fruit + +removed_fruit = str(raw_input("Type the name of the fruit you wish to remove from the list: ")) + +list_size = len(fruits) +for fruit in fruits: + if removed_fruit.lower() == fruit.lower(): + fruits.remove(fruit) +if list_size == len(fruits): + print "Could not find '%s' in the list of fruits!" % removed_fruit + +print "Full list:" +for fruit in fruits: + print fruit + + +for fruit in fruits: + print "Do you like %s?" % fruit + removed_fruits = [] + while True: + question = raw_input("Enter 'yes' or 'no': ") + if question.lower() == "no": + print "Removing %s.." % fruit + removed_fruits.append(fruit) + break + elif question.lower() == "yes": + print "Keeping %s.." % fruits + break + else: + print "You must enter 'yes' or 'no'!" +for fruit in fruits: + if fruit in removed_fruits: + fruits.remove(fruit) + + +print "Here's the list of fruits again: " +for fruit in fruits: + print fruit + +new_fruits = [] + +for i in fruits: + new_fruits.append(i[::-1]) + +print "Now here's the fruit list with each element reversed: " +for i in new_fruits: + print i \ No newline at end of file diff --git a/Students/Tyler G/session3/mailroom.py b/Students/Tyler G/session3/mailroom.py new file mode 100644 index 00000000..b8f77b56 --- /dev/null +++ b/Students/Tyler G/session3/mailroom.py @@ -0,0 +1,93 @@ +from textwrap import dedent +import math + +# Original list of historical donors and their amounts donated +donations = [] +donations.append( ("Marshawn Lynch", [238.99, 159.23]) ) +donations.append( ("Russell Wilson", [9910.00, 159.23, 357.00]) ) +donations.append( ("Richard Sherman", [426.48, 3859.94, 5496.00]) ) +donations.append( ("Kam Chancellor", [999.99]) ) +donations.append( ("Bobby Wagner", [999.99]) ) + +def show_donations(): + x = 0 + print "\nThe names of previous donors are: " + for person in donations: + print donations[x][0] + x += 1 + +def write_thank_you(name, amount): + print dedent(''' + Dear %s, + Thank you for your very kind donation of $%d. + It will be put to very good use. + Sincerely, + -The Team + ''') % (name, amount) + +def check_add_donators(name): + x = 0 + move_on = False + for person in donations: + if name.lower() == donations[x][0].lower(): + print "\n%s is a previous donor!" % donations[x][0] + choice = None + while not choice: + amount = raw_input("Please add another donation entry for this individual. Be sure to enter an integer or float: ") + try: + choice = float(amount) + except ValueError: + print "Must be an integer or float value!" + donations[x][1].append(float(amount)) + write_thank_you(donations[x][0], float(amount)) + move_on = True + x += 1 + if move_on == False: + print "\n%s is a new donor! Adding the name to registry." % name + choice = None + while not choice: + amount = raw_input("How much $ did this individual donate? Be sure to enter an integer or float: ") + try: + choice = float(amount) + except ValueError: + print "Must be an integer or float value!" + donations.append( (name, [float(amount)]) ) + write_thank_you(name, float(amount)) + +def sort_key(item): + return item[1] + +def create_report(): + report = [] + for (name, amount) in donations: + total = sum(amount) + count = len(amount) + avg = total / count + report.append( (name, total, count, avg) ) + report.sort(key=sort_key) + print "%25s | %11s | %9s | %12s" % ("Name", "Total Donated", "Freq Donated", "Avg Donated") + print "-"*66 + x = 0 + for row in report: + print "%25s %11.2f %12i %17.2f" % row + +print "\n" +print "*"*10, "Welcome to the Mailroom command-line script", "*"*10 +while True: + print dedent(''' + Type "list" to show the names of our donors. Type "Thank You" to generate + a thank-you letter to a donor. Type "Create Report" to generate a report + of our donors which includes their number of donations, average donation, + and total donation amounts. Finally, type "Exit" to exit the program.''') + response = raw_input("\nWhich option you like to select?: ") + if response.lower() == "list": + show_donations() + if response.lower() == "thank you": + name_query = raw_input("Enter the full name of the donor: ") + check_add_donators(name_query) + if response.lower() == "create report": + create_report() + if response.lower() == "exit": + print "Exiting now..." + break + \ No newline at end of file diff --git a/Students/Tyler G/session3/rot13.py b/Students/Tyler G/session3/rot13.py new file mode 100644 index 00000000..97e9f8ab --- /dev/null +++ b/Students/Tyler G/session3/rot13.py @@ -0,0 +1,30 @@ +# Returns a dictionary of the alphabet encrypted. The shift value is 13 by default. +def code_shift(shift = None): + if shift == None: + shift = 13 + letters = "abcdefghijklmnopqrstuvwxyz" + coder = {} + x = 0 + for i in range(len(letters)): + if i < (len(letters)-shift): + coder[letters[i]] = letters[i + shift] + else: + coder[letters[i]] = letters[x] + x += 1 + return coder + +def rot13(n): + encrypted = code_shift() + holder = " " + for char in n: + if char.lower() in encrypted: + if char == char.lower(): + holder += encrypted[char.lower()] + else: + holder += encrypted[char.lower()].upper() + else: + holder += char + return holder.lstrip(" ") + + + \ No newline at end of file diff --git a/Students/Tyler G/session3/string_lab.py b/Students/Tyler G/session3/string_lab.py new file mode 100644 index 00000000..48cc7e58 --- /dev/null +++ b/Students/Tyler G/session3/string_lab.py @@ -0,0 +1,40 @@ +# Slicing lab +def switch(n): + return n[-1:] + n[1:-1] + n[:1] + +def every_other[n] + return n[::2] + +def big_string(n): + return every_other(n[4:-4]) + +def big_strip2(n): + return n[4:-4:2] + +def reverse_sequence(n): + return n[::-1] + +def third_chop(n): + thirds = len(n) / 3 + first_third = n[:thirds] + middle_third = n[thirds:2*thirds] + last_third = n[2*thirds:] + return middle_third + last_third + first_third + + + +# Session 3 Extra Credit +def FizzBuzzAll(n, dictionary = None): + if dictionary == None: + dictionary = {3: "Fizz", 5: "Buzz", 7: "Frodo", 11: "Bilbo", 13: "Gandalf"} + assert type(n) == int and type(dictionary) == dict, "You must insert an integer and an optional dictionary." + for num in range(1, n + 1): + holder = " " + for value in dictionary: + if num % value == 0: + holder += dictionary[value] + if holder != " ": + print holder.lstrip(" ") + else: + print num +