HW 1: Problems 3 - PowerPoint PPT Presentation

About This Presentation
Title:

HW 1: Problems 3

Description:

General introduction of a new product taking customer wishes into account – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 19
Provided by: wilk226
Category:
Tags: problems | team | vowel

less

Transcript and Presenter's Notes

Title: HW 1: Problems 3


1
HW 1 Problems 3 4EC 1 2
2
Problem 3 Rock, Paper, Scissors
This problem should play rock, paper, scissors
fairly (remember last week, the user always
won!)? Steps 1. Have your program randomly
choose between the options rock, paper and
scissors, but don't tell the user! 2. Ask the
user for their choice, if they do not choose the
correct answer then you will give a
warning. 3. Figure out who the winner is
(compare your random choice to the user's
input) and then print out - Your choice - The
user's choice - The winner
3
Problem 3Completely Random!
In order to make a random choice between rock,
paper and scissors we will use python's random
package. Your code should look almost like
this...
from random import s choice(
'thread', 'yarn', 'twine' )? print 'I
chose', s
4
Problem 3Using a While Loop
Optionally, your function could use a while loop,
in which it asks the user to play again and then
continue (or not), based on their answer. This is
not required, but good practice!
answer 'no' while answer 'no' body
of the program answer raw_input('Would you
like to stop? ')?
5
Problem 3Example Run
gtgtgt rps( )? Welcome to RPS! I have made my
choice. Choose your poison scissors You
chose scissors. I chose rock. I win! And I
didn't even cheat this time!
6
Problem 4Fun with Functions!
Write a mult recursive function that multiplies
any two numbers. Instead of using the
multiplication operator use the
addition/subtraction/negation operators that were
discussed in class.
Hints Use the power function discussed in
class as a guide Remember that m n is m added
together n times, but keep in mind you need to
be able to multiply negative numbers also
7
Problem 4Fun with Functions!
Write a dot recursive procedure that outputs the
dot product of the lists L and K. The function
should just return 0.0 when 1. The two
functions are not of equal length 2. The two
lists are both empty
Hints Use the mysum function discussed in
class as a guide but remember that dot uses two
lists Remember that the dot product of two
lists is s the sum of the products of the
elements in the same position in the two lists.
For example 1, 2 ltdotgt 3,4 -gt (13) (24)?
8
Problem 4 Fun With Functions!
Write an ind recursive procedure, which takes in
a sequence L and an element e. L might be a
string or, more generally, a list. ind should
return the index at which e is first found in L.
Important Notes - Counting begins at 0, as is
usual with lists. - If e is NOT an element of
L, then ind(e, L) should return any integer
larger than or equal to len(L).
Examples gtgtgt ind(42, 55, 77, 42, 12, 42, 100
)? 2 gtgtgt ind('hi', 'hello', 42, True
)? 3 gtgtgt ind('i', 'team')? 4
9
Problem 4Fun With Functions
Write an non-recursive letterscore function which
takes a single character input and gives its
related scrabble score. - If the input is not
one of the letters from 'a' to 'z', the function
should return 0.
Hints Instead of using 25 if and elif statements
use the following gtgtgt 'a' in 'this is a string
including a' True gtgtgt 'q' in 'this string does
not have the the letter before r' False
10
Problem 4Fun With Functions
Write a scrabbleScore function which takes any
word and returns its associated scrabblescore -
Use the letterscore function with recursion
Examples gtgtgt scrabbleScore('quetzal')? 25 gtgtgt
scrabbleScore('jonquil')? 23
11
Extra Credit Option 1Pig Latin Warm up
Write pigLatin( s ), which will take as input a
string s. s will be a single word consisting of
lowercase letters and then output the translation
of s to pig latin. - If the input word has no
letters at all (the empty string), your
function should return the empty string - If
the input word begins with a vowel, the pig latin
output simply appends the string 'way' at the
end. 'y' will be considered a consonant, and
not a vowel, for this problem. - Hint Consonant
letters in the English alphabet are B, C, D, F,
G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Z,
and Y.
12
Extra Credit Option 1Pig Latin Warm up
Example Example
pigLatin('one') returns 'oneway' If the word
begins with a vowel, put way on the end
pigLatin('be') returns 'ebay' Of course, this
does not handle words beginning with multiple
consonants correctly. For example,
pigLatin('string') returns 'tringsay'.Don't worry
about this. However, if you would like to tackle
this more
13
Extra Credit Option 1Pig Latin Challenge
Create a function called spamLatin( s ) that
handles more than one initial consonant correctly
in the translation to Pig Latin. That is,
spamLatin moves all of the initial consonants to
the end of the word before adding 'ay'. - You
may want to write and use a helper function to do
this.Also, spamLatin should handle an
initial 'y' either as a consonant OR as a vowel,
depending on whether the y is followed by a vowel
or consonant, respectively. - 'yes' has an
initial y acting as a consonant. - 'yttrium',
however has an initial y acting as a vowel.
14
Extra Credit Option 1Pig Latin Challenge
Example gtgtgt spamLatin('string')? ingstray gtgtgt
spamLatin('yttrium')? yttriumway gtgtgt
spamLatin('yoohoo')? oohooyay
15
Extra Credit Problem 2Scrabble Scoring a File
Write a function named scoreFile( fileName ) that
takes in a string, which is the name of the file
to be scored. Then, your function should open
that file, read its contents, and compute the
following - The total scrabble score of all of
the characters in the file. Non-alphabetic
characters get a score of 0. Both upper- and
lower-case letters,however, should count toward
this total according to their usual scrabble
scores. - The total number of alphabetic
characters in the file. This includes both
upper- and lower-case letters. - The average
scrabble-score-per-letter for the file.
16
Extra Credit Problem 2Scrabble Scoring a File
Input from a file def printFileToScreen(
fileName ) """ simply prints the contents of the
file to the screen input fileName, a string with
the file's name """ f file( fileName ) f is
the opened file text f.read() text is the
name of all of f's text f.close() this closes
the file f - a good idea print 'The file
contains' drumroll print blank line print
text ta da!
17
Extra Credit Problem 2Scrabble Scoring a File
If you run scoreFile on files with more than
1,000 characters, it may use more memory than
Python allocates to the recursive stack (and
crash). To get around this, you can add the
following lines at the top of your hw1pr5.py
file These lines allow Python to build a
stack of up to 100,000 function calls -- or until
the memory your operating system has given Python
runs out
import sys sys.setrecursionlimit(100000)?
18
Extra Credit Problem 2Scrabble Scoring a File
Testing the problem - Test the program on two
additional files - Do not use MS Word files (too
many formatting marks!), use a plain text file -
In a comment at the top of your hw1ec2.py file,
report the results from the two files you chose.
Write a Comment
User Comments (0)
About PowerShow.com