HW 1: Problems 3 & 4 EC 1 & 2

1 / 18

# HW 1: Problems 3 & 4 EC 1 & 2 - PowerPoint PPT Presentation

HW 1: Problems 3 & 4 EC 1 & 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!

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## HW 1: Problems 3 & 4 EC 1 & 2

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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

- The user's choice

- The winner

Problem 3:Completely 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

Problem 3:Using 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!

[body of the program]

answer = raw_input('Would you like to stop? ')‏

Problem 3:Example Run

>>> rps( )‏

Welcome to RPS! I have made my choice.

You chose scissors.

I chose rock.

I win!

And I didn't even cheat this time!

Problem 4:Fun 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

Problem 4:Fun 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] <dot> [3,4] -> (1*3) + (2*4)‏

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:

>>> ind(42, [ 55, 77, 42, 12, 42, 100 ])‏

2

>>> ind('hi', [ 'hello', 42, True ])‏

3

>>> ind('i', 'team')‏

4

Problem 4:Fun 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:

>>> 'a' in 'this is a string including a'

True

>>> 'q' in 'this string does not have the the letter before r'

False

Problem 4:Fun With Functions

Write a scrabbleScore function which takes any word and returns

its associated scrabblescore

- Use the letterscore function with recursion

Examples:

>>> scrabbleScore('quetzal')‏

25

>>> scrabbleScore('jonquil')‏

23

Extra Credit Option 1:Pig Latin Warm up

WritepigLatin( 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.

Extra Credit Option 1:Pig 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

Extra Credit Option 1:Pig 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.

Extra Credit Option 1:Pig Latin Challenge

Example

>>> spamLatin('string')‏

ingstray

>>> spamLatin('yttrium')‏

yttriumway

>>> spamLatin('yoohoo')‏

oohooyay

Extra Credit Problem 2:Scrabble 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.

Extra Credit Problem 2:Scrabble 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!

Extra Credit Problem 2:Scrabble 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)‏

Extra Credit Problem 2:Scrabble 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.