problems rock-paper-scissors (fair game) functions frenzy extra problems pig latin scoring...

20

Upload: julia-ray

Post on 18-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper
Page 2: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Problems Rock-Paper-Scissors (fair game) Functions Frenzy

Extra Problems Pig Latin Scoring Paper

Page 3: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Objective: Play rock-paper-scissors fairly

Steps for the program:1. Choose the computer choice randomly

(without revealing this value to the user)2. Ask the user for their choice

(give warning for incorrect choice)3. Figure out the winner & print out the

result

Page 4: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Using choice function from random package

Example code:from random import *

s = choice(['thread','yarn','twine'])print('I chose', s)

Page 5: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Execute a block of code repeatedly as long as a condition is satisfied.

Application: Keep asking as long as incorrect choice is given by the user

Example code:answer = 'no'while answer == 'no':

[body of the program]answer = input('Want to stop? ')

Page 6: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Problems:1. Multiplication: mult(n,m)2. Dot product: dot(L,K)3. First index: ind(e,L)4. Scoring Letter*: letterScore(let)5. Scoring Word: scrabbleScore(S)

Recursive Functions: Base case Recursive part

Page 7: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Return: product of 2 integers Requirements:

Use recursion Use only: addition, subtraction, negation

Hints: Use the construction of function power as

a guide m * n = m added together n times Handling negative numbers

Page 8: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Return: dot product of any 2 lists Return 0.0 when

Lists have unequal lengths Empty lists

Hints: Use the construction of function mysum as a

guide Dot product = sum of products of elements

in the same position Example: [5,3] o [6,4] = 5*6 + 3*4 = 42

Page 9: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Return: first index of an element in a list Requirements

Use Recursion Counting starts at 0 Nonexistence = return any integer ≥ len(L)

Examples:>>> ind(42, [55,77,42,12,42,100])2>>> ind('i', 'team')4

Page 10: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Return: score of a letter based on

Requirements No recursion needed Return 0 for input that is not from 'a' to 'z'

Hints: Use in keyword:>>> 'a' in 'this is a string with letter a'True

Page 11: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Return: score of a string Requirements

Use recursion Hints:

Use the construction of function mylen as a guide

Use letterScore(let) as previously defined

Page 12: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

The warm-up: pigLatin(s) The challenge: spamLatin(s)

Page 13: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Input: single word, lower letter string s Translate to Pig Latin based on these rules

If s is an empty string:▪ return an empty string

If s begins with a vowel (a,e,i,o,u)▪ Append 'way' to the end of s▪ Example: pigLatin('one') returns 'oneway'

If s begins with a consonant (the rest including y)▪ Move this consonant to the end▪ Then append 'ay' to the end▪ Example: pigLatin('be') returns 'ebay'

Page 14: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Input: single word, lower letter string s Translate to Pig Latin based on these

rules If s is an empty string, or begins with a

vowel, same with pigLatin(s) If s begins with a consonant▪ Move all initial consonants to the end▪ Then append 'ay' to the end▪ Example: pigLatin('string') returns 'ingstray'

Page 15: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Rules (continue) If s starts with 'y' which is followed by ▪ a vowel, then consider this 'y' to be a

consonant▪ a consonant, then consider this 'y' to be a

vowel▪ spamLatin('yttrium') returns 'yttriumway'▪ spamLatin('yoohoo') returns 'oohooyay‘

Other cases: free to decide

Page 16: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Input: a file name Output:

Total Scrabble score Total number of alphabetic characters Average Scrabble-score-per-letter

Important: both upper- and lower-case letters

Page 17: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

def printFileToScreen(fileName):

f = open(fileName) text = f.read()f.close()

print('The file contains:’)print()print(text)

Page 18: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Recursive stack is limited at 1000 calls by default

Change this temporarily:import sys

sys.setrecursionlimit(100000)

Page 19: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Test on 2 additional files Convert your files to plain text first Include the results at the top of your

program

Page 20: Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper

Name your files correctly Include docstring for your functions Test thoroughly

Good luck