EECS 110: Homework I - PowerPoint PPT Presentation

About This Presentation
Title:

EECS 110: Homework I

Description:

... [55,77,42,12,42,100]) 2 ind('i', 'team') ... return an empty string If s begins with a vowel (a,e,i,o,u) Append 'way' to the end of s Example ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 21
Provided by: Bach83
Category:
Tags: eecs | homework | team | vowel

less

Transcript and Presenter's Notes

Title: EECS 110: Homework I


1
EECS 110 Homework I
  • Spring 2010

2
Homework I
  • Problems
  • Rock-Paper-Scissors (fair game)
  • Functions Frenzy
  • Extra Problems
  • Pig Latin
  • Scoring Paper

3
Problem 3 Rock, Paper, ScissorsOverview
  • 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, ScissorsRandom 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, ScissorsA 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 FunctionsOverview
  • 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 FunctionsMultiplication
  • 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 FunctionsDot 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 56 34 42

9
Problem 4 Recursive FunctionsFirst Index
  • Return first index of an element in a list
  • Requirements
  • Use Recursion
  • Counting starts at 0
  • Nonexistence return any integer len(L)
  • Examples gtgtgt ind(42, 55,77,42,12,42,100)? 2
    gtgtgt ind('i', 'team')? 4

10
Problem 4 Recursive FunctionsScoring 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 keywordgtgtgt 'a' in 'this is a string with
    letter a'True

11
Problem 4 Recursive FunctionsScoring 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 LatinOverview
  • The warm-up pigLatin(s)
  • The challenge spamLatin(s)

13
Extra Credit 1 Pig LatinThe 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 LatinThe 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 LatinThe 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 FileOverview
  • 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 FileReading 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 FileLarge Recursive Call
  • Recursive stack is limited at 1000 calls by
    default
  • Change this temporarilyimport
    syssys.setrecursionlimit(100000)?

19
Extra Credit 2 Scoring FileTesting
  • 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
Write a Comment
User Comments (0)
About PowerShow.com