Outline. policies for the second part. with answers. MCS 260 Lecture 10.5 Introduction to Computer Science Jan Verschelde, 9 July 2014

Similar documents
Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

strings, dictionaries, and files

strings, dictionaries, and files

MCS 260 Exam 2 13 November In order to get full credit, you need to show your work.

Extended Introduction to Computer Science CS1001.py. Lecture 8 part A: Finding Zeroes of Real Functions: Newton Raphson Iteration

Simultaneous Equations Solve for x and y (What are the values of x and y): Summation What is the value of the following given x = j + 1. x i.

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

Section A.6. Solving Equations. Math Precalculus I. Solving Equations Section A.6

Python. chrysn

Math 2142 Homework 5 Part 1 Solutions

CS177 Fall Midterm 1. Wed 10/07 6:30p - 7:30p. There are 25 multiple choice questions. Each one is worth 4 points.

LAST TIME..THE COMMAND LINE

CS 177 Fall 2014 Midterm 1 exam - October 9th

CSCI-141 Exam 1 Review September 19, 2015 Presented by the RIT Computer Science Community

8. More on Iteration with For

9A. Iteration with range. Topics: Using for with range Summation Computing Min s Functions and for-loops A Graphics Applications

Roots and Coefficients Polynomials Preliminary Maths Extension 1

Root finding. Eugeniy E. Mikhailov. Lecture 06. The College of William & Mary. Eugeniy Mikhailov (W&M) Practical Computing Lecture 06 1 / 10

CS483 Design and Analysis of Algorithms

1. 10 apples for $6 is $0.60 per apple. 20 apples for $10 is $0.50 per apple. 25 $ $0.50 = 25 $0.10 = $2.50

General Form. Standard Form(Vertex Form) where a,b, and c are real numbers, with. where a, h, and k are real numbers, with.

Companion. Jeffrey E. Jones

ITI Introduction to Computing II

Name: Exam 2 Solutions. March 13, 2017

Math 2000 Practice Final Exam: Homework problems to review. Problem numbers

EECS 126 Probability and Random Processes University of California, Berkeley: Spring 2015 Abhay Parekh February 17, 2015.

Carleton University. Final Examination Fall DURATION: 2 HOURS No. of students: 275

Root finding. Eugeniy E. Mikhailov. Lecture 05. The College of William & Mary. Eugeniy Mikhailov (W&M) Practical Computing Lecture 05 1 / 10

Math123 Lecture 1. Dr. Robert C. Busby. Lecturer: Office: Korman 266 Phone :

V. RANDOM VARIABLES, PROBABILITY DISTRIBUTIONS, EXPECTED VALUE

Statistics 433 Practice Final Exam: Cover Sheet and Marking Sheet

Section 8.3 Partial Fraction Decomposition

NOTES ON OCT 4, 2012

Kinematics + Dynamics

NAME (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt):

CS1210 Lecture 23 March 8, 2019

NOTES. [Type the document subtitle] Math 0310

UNIVERSITETET I OSLO

Instructions. Do not open your test until instructed to do so!

1 Sequences of events and their limits

L435/L555. Dept. of Linguistics, Indiana University Fall 2016

Systems of Equations

When factoring, we ALWAYS start with the (unless it s 1).

Exam 2 Review Math 118 Sections 1 and 2

Homework. Turing Machines. Announcements. Plan for today. Now our picture looks like. Languages

CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis. Hunter Zahn Summer 2016

ECE 204 Numerical Methods for Computer Engineers MIDTERM EXAMINATION /8:00-9:30

ITI Introduction to Computing II

MATH 31B: MIDTERM 2 REVIEW. sin 2 x = 1 cos(2x) dx = x 2 sin(2x) 4. + C = x 2. dx = x sin(2x) + C = x sin x cos x

2 Getting Started with Numerical Computations in Python

Lecture 23: More PSPACE-Complete, Randomized Complexity

Carleton University. Final Examination Fall DURATION: 2 HOURS No. of students: 223

Calculus II. Monday, March 13th. WebAssign 7 due Friday March 17 Problem Set 6 due Wednesday March 15 Midterm 2 is Monday March 20

Introduction to Python

Introductory Probability

Matlab Sheet 4. Conditional Statements and Loops

2006 Marywood Mathematics Contest

Math Practice Exam 2 - solutions

Get acquainted with the computer program, The Quadratic Transformer. When you're satisfied that you understand how it works, try the tasks below.

5) An experiment consists of rolling a red die and a green die and noting the result of each roll. Find the events:

COS 341: Discrete Mathematics

Objective Mathematics

Part I: Definitions and Properties

CS177 Spring Midterm 2. April 02, 8pm-9pm. There are 25 multiple choice questions. Each one is worth 4 points.

MIDTERM EXAM SOLUTIONS

Organization Team Team ID#

HOMEWORK 8 SOLUTIONS MATH 4753

D I S C R E T E M AT H E M AT I C S H O M E W O R K

STAT 414: Introduction to Probability Theory

Individual Round CHMMC November 20, 2016

Introduction to Python

Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

Chapter Six. Polynomials. Properties of Exponents Algebraic Expressions Addition, Subtraction, and Multiplication Factoring Solving by Factoring

Practice Midterm Exam

Lecture 22: PSPACE

b for the linear system x 1 + x 2 + a 2 x 3 = a x 1 + x 3 = 3 x 1 + x 2 + 9x 3 = 3 ] 1 1 a 2 a

40h + 15c = c = h

NUMERICAL ANALYSIS WEEKLY OVERVIEW

Math Day at the Beach 2017

MATH10001 Mathematical Workshop Graph Fitting Project part 2

Final exam (practice) UCLA: Math 31B, Spring 2017

Computational Methods for Engineers Programming in Engineering Problem Solving

AMC Preparation Berkeley Math Circle January 31, 2012

27 Wyner Math 2 Spring 2019

Pseudorandom Generators

CS325: Analysis of Algorithms, Fall Final Exam

Physics 170 Lecture 2. Phys 170 Lecture 2 1

2. (25%) Suppose you have an array of 1234 records in which only a few are out of order and they are not very far from their correct positions. Which

Chapter Generating Functions

1.225J J (ESD 205) Transportation Flow Systems

Instructions. Do not open your test until instructed to do so!

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Partial Fractions. (Do you see how to work it out? Substitute u = ax + b, so du = a dx.) For example, 1 dx = ln x 7 + C, x x (x 3)(x + 1) = a

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 1 Discussion:

5.3 SOLVING TRIGONOMETRIC EQUATIONS

Math 1313 Experiments, Events and Sample Spaces

Math 493 Final Exam December 01

6.01: Introduction to EECS I. Discrete Probability and State Estimation

UNIT NUMBER PROBABILITY 6 (Statistics for the binomial distribution) A.J.Hobson

Transcription:

Outline 1 midterm exam on Friday 11 July 2014 policies for the second part 2 some review questions with answers MCS 260 Lecture 10.5 Introduction to Computer Science Jan Verschelde, 9 July 2014 Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 1 / 32

review of the first 9 lectures 1 midterm exam on Friday 11 July 2014 policies for the second part 2 some review questions with answers Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 2 / 32

policies The second part of the exam will be open notes, books, and with a computer. The material in this part has its focus on Python: 1 variables and assignments, elementary and composite types, intrinsic operations, tuples, lists, dictionaries, anydbm, queues and stacks; 2 if else elif, while and for, queues and stacks, top down design, functions, lambda, list comprehensions. This review contains some preliminary examples of questions which may help you prepare for the second part of the midterm exam. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 3 / 32

review of the first 9 lectures 1 midterm exam on Friday 11 July 2014 policies for the second part 2 some review questions with answers Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 4 / 32

question 1 Consider the flowchart, that takes on input the word w: n = len(w) n > 0? False stop True n = n 1 print w[n] 1 Write Python code based using the same control logic as shown in the flowchart. 2 Write Python code that uses a for loop. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 5 / 32

answer to question 1 WORD = raw_input( give a word : ) N = len(word) while N>0: N = N - 1 print WORD[N] N = len(word) for k in range(n): print WORD[N-k-1] Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 6 / 32

question 2 Consider the following pseudocode: input: x, a floating point number; c, a list of coefficients. output: c[0] + c[1]*x + c[2]*x**2 +.. + c[n]*x**n, where n = len(c) - 1. result = c[0]; i = 0; n = len(c); p = x as long as i < n-1 do result = result + c[i+1]*p p = p * x; i = i + 1 output result Write a Python script that implements this pseudocode. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 7 / 32

answer to question 2 CRAW = raw_input( give a list of coefficients : ) C = eval(craw) XRAW = raw_input( give a floating points number : ) X = float(xraw) RESULT = C[0] i = 0 N = len(c) P = X while i < N-1: RESULT = RESULT + C[i+1]*P P = P * X i = i + 1 print RESULT Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 8 / 32

question 3 What data structure of Python would you use to store the derivatives of trigonometric expressions like cos, sin, and tan? Illustrate with actual Python code. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 9 / 32

answer to question 3 D = { cos : -sin, sin : cos, tan : tan**2 + 1 } for k in D.keys(): print the derivative of, k, is, D[k] Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 10 / 32

question 4 Give the Python code to generate a list of all tuples containing all possible input values for a Boolean expression, i.e.: [(False,False),(False,True),(True,False),(True,True)]. Extend the code so it will work for triplets. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 11 / 32

answer to question 4 L = [] for i in (False, True): for j in (False, True): L.append((i, j)) print L L = [] for i in (False, True): for j in (False, True): for k in (False, True): L.append((i, j, k)) print L Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 12 / 32

question 5 Write a program to sum the values of coins. The coins are represented by a sequence of characters, like pnqdpqnnppdqddpp, where p, n, d, q represent respectively a penny ($0.01), nickel ($0.05), dime ($0.10), and quarter ($0.25). Use a dictionary. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 13 / 32

answer to question 5 DICT = { p :0.01, n :0.05, d :0.10, q :0.25} DATA = raw_input( give a sequence : ) S = 0 for C in DATA: S = S + DICT[C] print value =, S Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 14 / 32

question 6 Give Python code to convert strings like "34 dollar" and "1.23 dollar" into "$34" and "$1.23". The number in front of the dollar can be of any size. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 15 / 32

answer to question 6 INPUT = raw_input( give a string : ) SPLITTED = INPUT.split( ) AMOUNT = float(splitted[0]) if float(int(amount)) == AMOUNT: FORMATTED = %d % int(amount) else: FORMATTED = %.2f % AMOUNT OUTPUT = $ + FORMATTED print OUTPUT Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 16 / 32

question 7 Consider the following game. The computer generates a random natural number between 1 and 9. This number is the secret. The user is allowed to make three guesses for the secret. Write Python code to implement this game. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 17 / 32

answer to question 7 from random import randint SECRET = randint(1, 9) for k in range(3): GUESS = raw_input( make a guess : ) if SECRET == int(guess): print Congratulations! break else: print Please try again. print Game over. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 18 / 32

question 8 To test whether random.uniform(0,1) generates numbers uniformly distributed in [0, 1], we generate 1000 samples and build a histogram, counting how many numbers are in the four equal subintervals of [0, 1]. Write Python code to perform this test. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 19 / 32

answer to question 8 from random import uniform as u L = [u(0, 1) for k in range(1000)] H = [0 for k in range(4)] for r in L: if r < 0.25: H[0] = H[0] + 1 elif r < 0.50: H[1] = H[1] + 1 elif r < 0.75: H[2] = H[2] + 1 else: H[3] = H[3] + 1 print histogram :, H Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 20 / 32

question 9 Explain how to simulate an unfair coin that when flipped returns head with probability 0.6. Give a Python function for such unfair coin. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 21 / 32

answer to question 9 def unfair_coin(): "returns head with probability 0.6" from random import uniform toss = uniform(0, 1) if toss < 0.6: return head else: return tail print simulation of an unfair coin while True: print unfair_coin() ANS = raw_input( continue? (y/n) ) if ANS!= y : break Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 22 / 32

question 10 Define a Python function that returns as a tuple the roots of a polynomial p(x) = ax 2 + bx + c, using b± b 2 4ac 2a. Write the function so that 1 is the default value for a. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 23 / 32

answer to question 10 def roots ( cx0, bx1, ax2=1 ): "returns roots via quadratic formula" from cmath import sqrt disc = sqrt(bx1**2-4*ax2*cx0) return ((-bx1+disc)/(2*ax2), (-bx1-disc)/(2*ax2)) ARAW = raw_input( give a : ) BRAW = raw_input( give b : ) CRAW = raw_input( give c : ) A = float(araw) B = float(braw) C = float(craw) print roots =, roots(c, B, A) Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 24 / 32

question 11 A trigonometric function like A sin(k2πt) has amplitude A and frequency k (number of cycles per time unit). Write a Python function f to evaluate A sin(k2πt) at t (the first argument of f) where default values for A and k are both one. If the last optional argument of f takes the value nopi, then the function will return the value A sin(kt) instead. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 25 / 32

ARAW = raw_input( give the amplitude : ) KRAW = raw_input( give the frequency : ) TRAW = raw_input( give the time : ) AMP = float(araw) FRQ = float(kraw) TIM = float(traw) VAL = fun(tim, AMP, FRQ) print the value :, VAL VAL = fun(tim, AMP, FRQ, nopi ) Intro print to Computer with Science (MCS nopi 260) :, review VAL for the midterm, part 2 L-10.5 9 July 2014 26 / 32 answer to question 11 def fun(time, amp=1, freq=1, arg= pi ): "return amp*sin(freq*2*pi*time)" from math import pi, sin if arg == nopi : return amp*sin(freq*2*time) else: return amp*sin(freq*2*pi*time)

question 12 Give Python code to create a list of the first 20 natural numbers, raised to the power 3. Do it once with for and once without using any for or while loops, but do it with list comprehensions. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 27 / 32

answer to question 12 L = [] for k in range(20): L.append(k**3) print L L = [k**3 for k in range(20)] print L Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 28 / 32

question 13 Use list comprehensions to make L = [[( a, 97), ( b, 98),..., ( z, 122)]. The list of tuples L links the lower case letters a to z to their respective ASCII codes. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 29 / 32

answer to question 13 A = [k for k in range(ord( a ), ord( z )+1)] B = [chr(k) for k in A] L = zip(b, A) print L Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 30 / 32

question 14 Write a Python program that doubles the spaces in a file: every space in the input file is replaced by two consecutive spaces in the output file. Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 31 / 32

answer to question 14 NAME = raw_input( Give the name of the input file : ) INFILE = open(name, r ) NAME = raw_input( Give the name of the output file : ) OUTFILE = open(name, w ) while True: C = INFILE.read(1) if C == : break elif C == : OUTFILE.write(C) OUTFILE.write(C) else: OUTFILE.write(C) Intro to Computer Science (MCS 260) review for the midterm, part 2 L-10.5 9 July 2014 32 / 32