Download presentation
Presentation is loading. Please wait.
1
Spring 2010 EECS 110: Homework I
2
Homework I Problems Extra Problems Rock-Paper-Scissors (fair game)
Functions Frenzy Extra Problems Pig Latin Scoring Paper
3
Problem 3: Rock, Paper, Scissors Overview
Objective: Play rock-paper-scissors fairly Steps for the program: Choose the computer choice randomly (without revealing this value to the user) Ask the user for their choice (give warning for incorrect choice) Figure out the winner & print out the result
4
Problem 3: Rock, Paper, Scissors Random choice
Using choice function from random package Example code: from random import * s = choice(['thread','yarn','twine']) print 'I chose', s
5
Problem 3: Rock, Paper, Scissors A while loop
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 = raw_input('Want to stop? ')
6
Problem 4: Recursive Functions Overview
Problems: Multiplication: mult(n,m) Dot product: dot(L,K) First index: ind(e,L) Scoring Letter*: letterScore(let) Scoring Word: scrabbleScore(S) Recursive Functions: Base case Recursive part
7
Problem 4: Recursive Functions Multiplication
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
8
Problem 4: Recursive Functions Dot Product
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
9
Problem 4: Recursive Functions First Index
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
10
Problem 4: Recursive Functions Scoring Letter
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
11
Problem 4: Recursive Functions Scoring Word
Return: score of a string Requirements Use recursion Hints: Use the construction of function mylen as a guide Use letterScore(let) as previously defined
12
Extra Credit 1: Pig Latin Overview
The warm-up: pigLatin(s) The challenge: spamLatin(s)
13
Extra Credit 1: Pig Latin The Warm-up pigLatin(s)
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'
14
Extra Credit 1: Pig Latin The Challenge spamLatin(s)
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'
15
Extra Credit 1: Pig Latin The Challenge spamLatin(s)
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
16
Extra Credit 2: Scoring File Overview
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
17
Extra Credit 2: Scoring File Reading a File
def printFileToScreen(fileName): f = file(fileName) text = f.read() f.close() print 'The file contains:' print print text
18
Extra Credit 2: Scoring File Large Recursive Call
Recursive stack is limited at 1000 calls by default Change this temporarily: import sys sys.setrecursionlimit(100000)
19
Extra Credit 2: Scoring File Testing
Test on 2 additional files Convert your files to plain text first Include the results at the top of your program
20
Last points Name your files correctly
Include docstring for your functions Test thoroughly Good luck
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.