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

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Main task -write me a program
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
An Introduction to Textual Programming
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Programming for Linguists An Introduction to Python 24/11/2011.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Programming, an introduction to Pascal
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
22/11/ Selection If selection construct.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
 Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
HKOI Programming HKOI Training Team (Intermediate) Alan, Tam Siu Lung Unu, Tse Chi Yung.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Spring  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums.
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
GCSE Computing: Programming GCSE Programming Remembering Python.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
String and Lists Dr. José M. Reyes Álamo.
C++ Memory Management – Homework Exercises
3.1 Fundamentals of algorithms
Algorithmic complexity: Speed of algorithms
Line Continuation, Output Formatting, and Decision Structures
Tuples and Lists.
Engineering Innovation Center
Starter Write a program that asks the user if it is raining today.
Writing Functions( ) (Part 5)
Line Continuation, Output Formatting, and Decision Structures
Python I/O.
Pick a number, any number …
Loops CIS 40 – Introduction to Programming in Python
Coding Concepts (Basics)
Introduction to TouchDevelop
Spring 2010 EECS 110: Homework I.
String and Lists Dr. José M. Reyes Álamo.
Algorithmic complexity: Speed of algorithms
Introduction to Python
Writing Functions( ) (Part 4)
Python programming exercise
Console.WriteLine(“Good luck!”);
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Functions continued.
Algorithmic complexity: Speed of algorithms
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Spring 2018 EECS 110: Homework IV.
File Handling.
COMPUTING.
Presentation transcript:

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! 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

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! answer = 'no' while answer == 'no': [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. Choose your poison: scissors 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 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.

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