import math import random def question1 (): total = 0 for number in range(1,6): total = total + number return(total) def spell_out(word): for character in word[:-1]: print(character, end="+") print(word[-1]) def question2 (): return(spell_out('Establishment')) import re phonenumbers = {'Rachel\'s Rose Shop': '333-400-3423', 'Donald\'s Daisies': \ '333-121-2121', 'Pat\'s Petunias': '333-434-5500', \ 'Samuelson\'s Sunflowers': '333-667-9689'} def question3 (): for key in phonenumbers: if re.search('00$',phonenumbers[key]): print('My choice is', key+':', phonenumbers[key]) import os def enter_line_in_dictionary(line): clean_line = line.strip(os.linesep) ## This removes the newline character and/or the return character line_list = clean_line.split('|') ## This splits the line using the '|' as a delimiter for index in range(len(line_list)): line_list[index] = line_list[index].strip(' ') ## this loops through the list (length 2) and replaces ## each item with a 'stripped version' -- the stripped ## version does not have spaces on the ends (spaces in ## the middle are left alone). quotation_dictionary[line_list[0]] = line_list[1] ## This adds (or replaces) a dictionary entry def read_in_quotations(file): global quotation_dictionary ## declaring quotation_dictionary a global variable quotation_dictionary = {} ## initializing the dictionary as an empty dictionary instream = open(file,'r') ## creating a stream by opening the file for line in instream: ## the loop looks at each line in the file enter_line_in_dictionary(line) ## calls enter_line_in_dictionary for each line instream.close() ## closes stream def question4(): read_in_quotations('list-of-quotations.txt') print(quotation_dictionary) ed_exceptions = {'light': 'lit', 'arise': 'arose', 'rise':'rose',\ 'sit':'sat', 'babysit':'babysat', 'bite':'bit', \ 'backslide':'backslid', 'bear': 'bore', 'beat': 'beat',\ 'read': 'read', 'become': 'became', 'come': 'came',\ 'fall':'fell','tell':'told'} def add_ed(verb): if verb in ed_exceptions: return(ed_exceptions[verb]) elif verb[-1] == 'e': return(verb+'d') elif (len(verb) > 2) and (verb[-1] in 'bdglmnprt') \ and (verb[-2] in 'aeiou') and (not (verb[-3] in 'aeiou')): return(verb+verb[-1]+'ed') elif (len(verb) > 2) and (verb[-1] == 'y') and (not (verb[-2] in 'aeiou')): return(verb[:-1]+'ied') else: return(verb+'ed') ## What is printed out in the following for list: def question5(): for word in ['fix','touch','spill','pat','find','tell','have','try']: print(add_ed(word)) def sails(width,columns, rows): for row in range(rows): for number in range(1,width+1): for column in range(columns): print('~'*number+' '*(width-number),end='') print() ## sails(5,1,1) ## sails(5,2,2) def question6(): sails(5,3,3) ## sample mad lib program (question 8) def get_next_word(wordtype): word = '' while (word == ''): word = input('Please enter a '+wordtype+': ') return(word) def my_mad_lib(): answers = [] missing_word_types = ['Person Name','noun','noun','adjective','liquid or powder'] for wordtype in missing_word_types: answers.append(get_next_word(wordtype)) print(answers[0],'had a little',answers[1],'little',answers[1],'little',answers[1]) print(answers[0],'had a little',answers[1],'little',answers[1],'little',answers[1]) print('whose', answers[2], 'was', answers[3], 'as', answers[4]) ## sample poker program including all extra practice parts ## question 9 face_value = {'A':14,'J':11,'Q':12,'K':13, 'Clubs':.1,\ 'Diamonds':.2,'Hearts':.3,'Spades':.4} def pick_a_random_card(): face_number = math.floor(random.random()*13) suit = math.ceil(random.random()*4) ## Determine face value of card face_string = 'A234567890JQK'[face_number] if face_string == '0': face_string = '10' ## Determine suit of card if suit == 1: suit_string = 'Clubs' elif suit == 2: suit_string = 'Diamonds' elif suit == 3: suit_string = 'Hearts' else: suit_string = 'Spades' return([face_string, suit_string]) def pick_a_new_random_card(card_list): new_card = pick_a_random_card() while new_card in card_list: new_card = pick_a_random_card() return(new_card) def generate_poker_hand(): output = [] for number in range(5): output.append(pick_a_new_random_card(output)) return(output) def card_value(card): if card[0] in face_value: return(face_value[card[0]] + face_value[card[1]]) else: return(int(card[0])+face_value[card[1]]) def card_greater_than(card1,card2): value1 = card_value(card1) value2 = card_value(card2) return(value1 > value2) def same_suit(hand): suit = '' Fail = False for card in hand: if suit == '': suit = card[1] elif suit != card[1]: Fail = True return(not Fail) def straight(hand): valuelist = [] for card in hand: if card[0] in face_value: valuelist.append(face_value[card[0]]) else: valuelist.append(int(card[0])) valuelist.sort() Fail = False previous = 0 for value in valuelist: if not ((previous == 0) or (previous == value - 1) \ or ((value == 14) and (valuelist[0] == 2))): Fail = True previous = value return(not Fail) def identify_hand(poker_hand): if same_suit(poker_hand): if straight(poker_hand): return('straight flush') else: return('flush') elif straight(poker_hand): return('straight') else: counts = {} for card in poker_hand: if card[0] in counts: counts[card[0]] = counts[card[0]]+1 else: counts[card[0]] = 1 pairs = 0 three_of_kind = 0 four_of_kind = 0 for face in counts: if counts[face] == 2: pairs = pairs+1 elif counts[face] == 3: three_of_kind = three_of_kind + 1 elif counts[face] == 4: four_of_kind = four_of_kind + 1 if four_of_kind > 0: return('4 of a kind') elif three_of_kind > 0: if pairs > 0: return('full house') else: return('3 of a kind') elif pairs > 1: return('2 pairs') elif pairs > 0: return('1 pair') else: high = ['2','Clubs'] for card in poker_hand: if card_greater_than(card, high): high = card return('High Card '+ high[0]) ### get high card def generate_and_identify_hand(): hand = generate_poker_hand() print(identify_hand(hand)) return(hand) ## Sample for Question 10 def get_user_response(guess): done = False response = '' while (not done): print('My guess is:',guess) response = input("If this is correct, type 'yes'. Otherwise, type 'high' or 'low'.") if not (response in ['yes','high','low']): print("Your response must be 'yes', 'high', or 'low'") print("Unfortunately",reponse, 'is not valid. Please try again.') else: done = True return(response) def I_can_guess_your_number(): print('Pick an integer between 1 and 1000, but do not tell me what it is.') print('''I will make one guess at a time. If I am right, please type 'yes'. If I am wrong, please type either 'high' if my guess is too 'high' or 'low' if my guess is too low.''') done = False high = 1000 low = 1 turns = 0 while (done == False): guess = (high + low)//2 answer = get_user_response(guess) if answer == 'yes': done = True elif answer == 'high': high = guess - 1 else: low = guess + 1 turns = turns+1 if turns <= 6: print('That was easy!') elif turns >=12: print('That was difficult~') print('I guessed the right answer in',turns,'turns.')