Spring 2010 EECS 110: Homework I.

Slides:



Advertisements
Similar presentations
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Advertisements

Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
HW 1: Problems 3 & 4 EC 1 & 2.
Main task -write me a program
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
EXAM 1 REVIEW. days until the AP Computer Science test.
Variables and Expressions CMSC 201 Chang (rev )
Practice with Lists and Strings CS303E: Elements of Computers and Programming.
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Count and add list of numbers From user input and from file.
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)
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.
 Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper.
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
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
The last CS 5 lecture you’ll ever need! Inducing labor for the machine! == Reducing labor for humans! On Warner Brothers' insistence, we affirm that this.
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.
More String Manipulation. Programming Challenge Define a function that accepts two arguments: a string literal and a single character. Have the function.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
DEVRY CIS 170 C I L AB 5 OF 7 A RRAYS AND S TRINGS Check this A+ tutorial guideline at
String and Lists Dr. José M. Reyes Álamo.
GCSE COMPUTER SCIENCE Practical Programming using Python
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
EECS 110: Lec 10: Definite Loops and User Input
String Manipulation Part 1
4. Java language basics: Function
CSc 110, Spring 2017 Lecture 12: Random Numbers
Spring 2010 EECS 110: Homework III.
EECS 110: Lec 5: List Comprehensions
Tuples and Lists.
Computer Programming Fundamentals
EECS 110: Lec 7: Program Planning
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
EECS 110: Lec 4: Functions and Recursion
Writing Functions( ) (Part 5)
Another problem to solve…
Building Java Programs
Python I/O.
T. Jumana Abu Shmais – AOU - Riyadh
Another problem to solve…
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Class Examples.
EECS 110: Lec 4: Functions and Recursion
Writing Functions( ) (Part 4)
Big problem  small steps
Console.WriteLine(“Good luck!”);
Programming Languages
Chapter 3: Selection Structures: Making Decisions
Functions continued.
Lists.
Building Java Programs
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Chapter 3: Selection Structures: Making Decisions
Spring 2018 EECS 110: Homework IV.
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
CSCE 206 Lab Structured Programming in C
Another problem to solve…
Game Description Player 1: Decides on integer x > 0
Primary School Computing
Presentation transcript:

Spring 2010 EECS 110: Homework I

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

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

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

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? ')

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

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

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

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

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

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

Extra Credit 1: Pig Latin Overview The warm-up: pigLatin(s) The challenge: spamLatin(s)

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'

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'

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

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

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

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)‏

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

Last points Name your files correctly Include docstring for your functions Test thoroughly Good luck