Practice Midterm Exam #2

Similar documents
Practice Midterm Examination #1

Practice Midterm Examination

Practice Midterm Exam #1

Practice Midterm Examination

Practice Final Examination #1

The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind.

Practice Final Examination #2

Practice Final Examination #2

Practice Midterm Examination #2

Practice Final Examination

CS201 Discussion 7 MARKOV AND RECURSION

Assignment 2: Welcome to Java!

MA 1128: Lecture 02 1/22/2018

CS106B Sprint 2017 April 27 th, 2017 CS106B Practice Midterm (KEY)

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, April 23 rd

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Mehran Sahami Handout #7 CS 106A September 24, 2014

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Spring 2018 June 20 th, 2018 Extra Practice Problems. Short Problems

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

One of Mike s early tests cases involved the following code, which produced the error message about something being really wrong:

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Assignment #2: Simple Java Programs Due: 1:15pm on Friday, April 19th

Assignment #4 Hangman Due: 3:15pm on Monday, November 3rd Your Early Assignment Help (YEAH) hours: 4:15pm-5:15pm, Friday, October 24th in Hewlett 200

CmpSci 187: Programming with Data Structures Spring 2015

Practice Midterm Examination

Discrete Structures. Fall Homework3

Section Handout 4. Problem One: Weights and Balances (Courtesy of Eric Roberts)

Order from Chaos. University of Nebraska-Lincoln Discrete Mathematics Seminar

PRACTICE MIDTERM EXAM #2

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Snowflake Numbers. A look at the Collatz Conjecture in a recreational manner. By Sir Charles W. Shults III

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

Solutions to In Class Problems Week 9, Fri.

Pick any positive integer. If the integer is even, divide it by 2. If it is odd,

Assignment #1 Simple C++

Problem One: A Quick Algebra Review

CS106X Handout 17 Winter 2015 January 28 th, 2015 CS106X Practice Exam

Assignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required)

CS103 Spring 2018 Mathematical Vocabulary

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

CS106X Handout 24 Winter 2018 February 9 th, 2018 CS106X Midterm Examination

Binary, Hexadecimal and Octal number system

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, Jan 29 th

Problem A: Collatz Conjecture

Practical Questions CSCA48 Week 6

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Homework Assignment 2: Java Console and Graphics

1 GB FLIP INTRODUCTION Introduction. This is GB FLIP, the module used by GraphBase programs to generate random numbers.

15110 PRINCIPLES OF COMPUTING SAMPLE EXAM 2

CS106B Handout #25 J Zelenski Feb 11, Midterm Practice

CS106X Handout 25 Autumn 2012 October 23 rd, 2012 CS106X Midterm Examination

CS3 Midterm 1 Fall 2006

15100 Fall 2005 Final Project

Assignment #3 Recursion

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

University of Illinois at Chicago Department of Computer Science. Final Examination. CS 151 Mathematical Foundations of Computer Science Fall 2012

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

CS173 Longest Increasing Substrings. Tandy Warnow

CS3: Introduction to Symbolic Programming. Lecture 11: Tree Recursion, beginning lists, and Midterm 2. Spring 2007 Nate Titterton

Extra Practice Problems 2

Chris and Chris Fall 2016 Nov 3 rd, 2016 CS106B Midterm

CMPSCI 250: Introduction to Computation. Lecture #24: General Search, DFS, and BFS David Mix Barrington 24 March 2014

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Subprime Fibonacci sequences

Recursion Chapter 3.5

Eric Roberts Handout #39 CS 106B February 20, 2015 Sets

SECTION 5.1. Sequences

Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12

ELEC-270 Solutions to Assignment 5

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CS2500 Exam 2 Fall 2011

Mathematical Operations

(Refer Slide Time: 1:40)

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

Objective- Students will be able to use the Order of Operations to evaluate algebraic expressions. Evaluating Algebraic Expressions

CS64 Week 5 Lecture 1. Kyle Dewey

EXAM Computer Science 1 Part 1

#23: Sequences March 28, 2009

Practice Final Examination

Grey Codes in Black and White

Bits, Words, and Integers

Discrete Structures Lecture The Basics of Counting

EASY

Autumn 2012 October 29 th, 2012 CS106X Midterm Examination Solution

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Only to be used for arranged hours. Order of Operations

9 abcd = dcba b + 90c = c + 10b b = 10c.

APCS-AB: Java. Recursion in Java December 12, week14 1

Midterm Exam 2A Principles of Computing Fall November 10, 2014

Assignment #4 Hangman Due: 11am on Monday, May 14th This assignment may be done in pairs (which is optional, not required)

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

Computer Science 236 Fall Nov. 11, 2010

Turn in a printout of your code exercises stapled to your answers to the written exercises at 2:10 PM on Thursday, January 13th.

Transcription:

Eric Roberts Handout #25 CS106B January 30, 2013 Practice Midterm Exam #2 Review session: Sunday, February 3, 7:00 9:00 P.M., Hewlett 201 (next door) Midterm #1: Tuesday, February 5, 3:15 5:15 P.M., Braun Auditorium (Chemistry) Midterm #2: Tuesday, February 5, 7:00 9:00 P.M., CEMEX Auditorium (GSB) Problem 1: Tracing C++ programs and big-o (10 points) Assume that the function puzzle has been defined as follows: int puzzle(int n) { if (n == 0) { return 0; } else { return puzzle(n - 1) + 1 + puzzle(n - 1); } } (a) What is the value of puzzle(4)? (b) What is the computational complexity of the puzzle function expressed in terms of big-o notation, where N is the value of the argument n. In this problem, you may assume that n is always a nonnegative integer and that all arithmetic operators execute in constant time. Problem 2: Vectors, grids, stacks, and queues (10 points) As most of you surely already know, the Japanese puzzle game Sudoku requires you to fill in the entries in a 9 9 grid of integers so that each of the digits between 1 and 9 appears exactly once in each row, each column, and each of the smaller 3 3 squares. A legal Sudoko grid (taken from Figure 5-13 of the reader on page 252) therefore looks something like this: When you write a program to check whether a Sudoku grid is legal, it simplifies the code to decompose the problem into two phases. In the first phase, your program calls a

2 function that extracts a row, column, or 3 3 subsquare from the entire grid and returns it as a vector of nine integers. You can then use a single function to check the integrity of any row, column, or subsquare. In this problem, your task is to write the function Vector<int> extract3x3subsquare(grid<int> & grid, int bigrow, int bigcol); that takes a complete Sudoku grid and returns a vector containing the nine integers in the 3 3 subsquare indicated by the indices bigrow and bigcol, which correspond to the row and column of the desired subsquare as shown in the following diagram: For example, if grid contains the complete Sudoku grid from the previous page, calling extract3x3subsquare(grid, 2, 0) should extract the nine values in the square at the lower left corner, which means that the result should be a Vector<int> containing the following values: [9, 6, 7, 4, 2, 3, 5, 1, 8] In writing your solution, you may assume that the grid parameter is a 9 9 grid and that the bigrow and bigcol parameters are both legal values between 0 and 2, inclusive. The elements in the vector should be returned in row-major order (defined in the reader on page 239), in which the elements from the first row appear in left-to-right order, followed by the elements from the second row, and then the elements from the third row. Problem 3: Lexicons, maps, and iterators (15 points) Exercise 10 in Chapter 1 (which appears on page 54 of the reader) introduces the hailstone sequence, which is the basis for one of the most fascinating unsolved problems in mathematics. If you starting with a positive integer n, you can compute the terms in the hailstone sequence by repeatedly executing the following steps: If n is equal to 1, you ve reached the end of the sequence and can stop. If n is even, divide it by two. If n is odd, multiply it by three and add one.

3 No matter what number you start with, this sequence always seems to get back to 1 eventually. So far, however, no one has yet been able to prove that claim, which is called the Collatz conjecture. Three years ago, the Collatz conjecture gained additional visibility when it appeared in Randall Monroe s xkcd. As the cartoon at the right makes clear, many of the sequences include common patterns. For example, all hailstone sequences (except for 1, 2, 4, and 8, of course) go through the value 16 and therefore share the last four elements of the path. If you ve followed through this path once, you don t need to follow it again. Keeping track of these common patterns can make calculating the length of hailstone sequences vastly more efficient. Suppose, for example, that you are calculating the number of steps in all the hailstone sequences between 1 and 100. What happens when you get to 24, which appears at the lower left corner of the cartoon? By the time you get to this point, you ve already figured out that it takes nine steps to get from 12 down to 1, so 24 must take ten steps, given that 12 is just one step away from 24. Note that the count is the number of steps; if you start with 1, the number of steps is 0. It s even more interesting to follow what happens with 7, which is in the middle of the right hand side. Here, figuring out the number of steps for 7 entails running through the number 22, 11, 34, 17, 52, 26, 13, 40, and 20 before you come to 10, which you ve already encountered in counting the number of steps from 3 and 6. At this point, you have learned an enormous amount. Given that it takes six steps to reach 1 starting at 10, you know that 20 takes seven steps, 40 takes eight, 13 takes nine, and so forth, backwards along the list of numbers in the chain, until you determine that there are sixteen steps in the hailstone chain between 7 and 1. If you keep track of these values in, for example, a Map<int,int>, you d already know the answers for the other values in the chain. Keeping track of previously computed values so that you can use them again without having to recompute them is called caching. Write a function int counthailstonesteps(int n, Map<int,int> & cache); that determines the number of steps in the hailstone sequence starting at n. The second parameter is a map containing previously computed values. Your implementation should look up each value it encounters during the process to check whether the answer from that point is already known. If so, it should use that previously computed value not only to compute the current result, but also to add the counts for all the intermediate steps to the map. Thus, if you call counthailstonesteps with 7 as the first parameter, your code should add the counts for 20, 40, 13, 26, 52, 17, 34, 11, 22, and 7 to the map before returning the answer. In this example, the other numbers in the sequence (10, 5, 16, 8, 4, and 2) are already in the map from previous calls and do not need to be added again.

4 Problem 4: Recursive functions (10 points) The waste of time in spelling imaginary sounds and their history (or etymology as it is called) is monstrous in English... George Bernard Shaw, 1941 In the early part of the 20th century, there was considerable interest in both England and the United States in simplifying the rules used for spelling English words, which has always been a difficult proposition. One suggestion advanced as part of this movement was the removal of all doubled letters from words. If this were done, no one would have to remember that the name of the Stanford student union is spelled Tresidder, even though the incorrect spelling Tressider occurs at least as often. If doubled letters were banned, everyone could agree on Tresider. Write a recursive function string removedoubledletters(string str); that takes a string as its argument and returns a new string with any consecutive substring consisting of repeated copies of the same letter replaced by a single copy letter of that letter. For example, if you call removedoubledletters("tresidder") your function should return the string "tresider". Similarly, if you call removedoubledletters("bookkeeper") your method should return "bokeper". And because your function compresses strings of multiple letters into a single copy, calling removedoubledletters("xxx") should return "x". In writing your solution, you should keep the following points in mind: You do not need to write a complete program. All you need is the definition of the function removedoubledletters that returns the desired result. Your function should not try to consider the case of the letters. For example, calling the function on the name "Lloyd" should return the argument unchanged because 'L' and 'l' are different letters. Your function must be purely recursive and may not make use of any iterative constructs such as for or while. Problem 5: Recursive procedures (15 points) In CS 106A, I usually include a problem on the midterm exam to check whether students understand the precedence of operators. For example, I might ask students to evaluate the expression

5 9 + 7 * 5-3 + 1 To do so, those students would have to know that the multiplication was performed first. Of course, I also want to make sure that the answer comes out to be some recognizable value. In this case, for example, the expression evaluates to 42, which is the answer to the ultimate question of life, the universe, and everything from Douglas Adams s Hitchhiker s Guide to the Galaxy. Generating such examples by hand is sufficiently difficult that it makes sense to use the power of recursion to solve it algorithmically. Your job in this problem is to write a function void tryalloperators(string exp, int target); in which exp is an expression string in which the operators have been replaced by question marks and target is the desired value. The function operates by replacing those question marks with the four standard arithmetic operators (+, -, *, /), trying every possible combination to see if any produce the desired target value. For example, calling tryalloperators("9? 7? 5? 3? 1", 42) should produce the following output, since that is the only combination of operators that gives 42 as a result: 9 + 7 * 5-3 + 1 TryAllOperators If there is more than one way to produce the target value, the function should list all of them. Thus, calling tryalloperators("2? 3? 5? 7? 11? 13", 42) should produce the following output, since there are two ways of achieving 42: TryAllOperators 2 + 3 + 5 * 7-11 + 13 2 * 3 + 5 + 7 + 11 + 13 Writing this program would be very difficult if you had to write the code to evaluate an expression. We will do precisely that later in the quarter, but for now, you should assume that you have a function int evaluateexpression(string exp);

6 that takes an arithmetic expression involving integers and the four arithmetic operators and returns the calculated value. Given this function, all you have to do for this problem is 1. Recursively generate every possible expression by replacing the question marks in the input string with each of the actual operators in turn. 2. Call evaluateexpression for each of those generated expressions. 3. Print out the expression if the result of evaluateexpression equals the target value.