CS 115 Exam 3, Fall 2015, Sections 1-4

Similar documents
CS 115 Exam 3, Fall 2016, Sections 5-8

CS 115 Exam 3, Fall 2016, Sections 1-4

CS 115 Exam 3, Spring 2015, Sections 1-4

CS 115 Exam 3, Spring 2014

CS 115 Exam 3, Fall 2011

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

Module 08: Searching and Sorting Algorithms

CS 115 Exam 2 (Section 1) Spring 2017 Thu. 03/31/2017

CS 115 Exam 3, Spring 2011

CS 115 Exam 2 (Section 1) Fall 2017 Thu. 11/09/2017

Binary Search APRIL 25 TH, 2014

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CS 1301 Exam 1 Answers Fall 2009

Structure and Interpretation of Computer Programs

Midterm I Practice Problems

Midterm #2a Fall minutes

CS 1110 Final Exam Solutions May 15th, 2014

CS 1301 Exam 1 Fall 2010

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Introduction to Computer Science Midterm 3 Fall, Points

CS150 Sample Final. Name: Section: A / B

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

CS150 - Sample Final

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

7. Tile That Courtyard, Please

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

CSCI 150 Exam 2 Solutions

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

CS 115 Exam 3, Spring 2010

16.216: ECE Application Programming Spring 2015 Exam 3 Solution

Sorting and Searching

Practical Questions CSCA48 Week 6

15-110: Principles of Computing Sample Exam #1

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Sorting. Task Description. Selection Sort. Should we worry about speed?

CS 1301 Exam 1 Fall 2009

Midterm Exam 2B Answer key

1 Short Answer. Math 4242 Grenfell Campus Fall 2017 Final Exam. There are 100 marks available. Attempt all questions.

Midterm #1a Spring minutes

EECE.2160: ECE Application Programming Spring 2018

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

1 Register 2 Take Course 3 Take Test 4 Get Certificate

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Midterm #1 Fall minutes

CS150 Sample Final Solution

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Looping. Arizona State University 1

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

FORM 2 (Please put your name and form # on the scantron!!!!)

Introduction to Python

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

CS 1301 Exam 1 Fall 2010

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3 5 CHAPTER 4. Algorithm Design & Problem Solving. Data Representation.

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

Computer Science Foundation Exam

Flow Control: Branches and loops

Examination Questions Midterm 2

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

15-110: Principles of Computing, Spring 2018

Conditionals and Recursion. Python Part 4

EXAMINATION INSTRUCTIONS

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Duration: 90 minutes. Page 1 of 14 Q2: /13 Q3: /13 TOTAL: /38. Bonus /1

CSE341 Spring 2017, Final Examination June 8, 2017

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Spring Semester 11 Exam #1 Dr. Dillon. (02/15)

Short Answer Questions (40 points)

EECE.2160: ECE Application Programming Spring 2018 Exam 3 Solution

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

The PCAT Programming Language Reference Manual

Data Types. Operators, Assignment, Output and Return Statements

CS 116 Fall 2016 Review Notes Created By: Rina Dong

UNIVERSITY OF TORONTO Faculty of Arts and Science

This exam has 10 pages including the title page. Please check to make sure all pages are included.

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

Practice final for EECS 380, 2001: Prof Markov

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

Sequence Types FEB

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

C/C++ Programming Lecture 18 Name:

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm 1 CSC148H1F L0201 (Liu)

TAIL CALLS, ITERATORS, AND GENERATORS 11

ENGR 101 Engineering Design Workshop

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Spring 2017 CS 1110/1111 Exam 1

You may not share any information or materials with classmates during the exam and you may not use any electronic devices.

Control Flow: Loop Statements

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

1. Compute the following numerically using the quantities given: v = [1 2 3] w = [0 4 5] A = [ ] d. (3 points) v w 7.

Homework 7: Sudoku. Preliminaries. Overview

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Instructors: Daniel Deutch, Amir Rubinstein, Teaching Assistants: Amir Gilad, Michal Kleinbort

Transcription:

, Sections 1-4 Your name: Rules You may use one handwritten 8.5 x 11 cheat sheet (front and back). This is the only resource you may consult during this exam. Explain/show work if you want to receive partial credit for wrong answers. As long as your code is correct, you will get full credit. No points for style. When you write code, be sure that the indentation level of each statement is clear. Your Score Max Score Problem 1: Binary search 10 Problem 2: Selection sort 10 Problem 3: Mergesort 10 Problem 4: Recursion 20 Problem 5: Defining classes 25 Problem 6: Using classes 25 Total 100

Reference code for Problems 1 and 2 The functions below are just for your reference on Problems 1 and 2. You do not need to read them if you understand the algorithms. def binary_search(search_list, value_to_find): first = 0 last = len(search_list) 1 while first <= last: middle = (first + last) // 2 # Problem 1: state the values of first, last, # and middle at this point in the code if value_to_find == search_list[middle]: return middle elif value_to_find < search_list[middle]: last = middle 1 else: first = middle + 1 def selection_sort(list_to_sort): for i in range(len(list_to_sort) 1): min_index = find_min_index(list_to_sort, i) list_to_sort[i], list_to_sort[min_index] = list_to_sort[min_index], list_to_sort[i] # Problem 2: Show list contents at this point def find_min_index(l, s): min_index = s for i in range(s, len(l)): if L[i] < L[min_index]: min_index = i return min_index Page 2 of 14

Reference code for Problem 3 The functions below are just for your reference on Problem 3. You do not need to read them if you understand the algorithms. def merge(l, start_index, sublist_size): index_left = start_index left_stop_index = start_index + sublist_size index_right = start_index + sublist_size right_stop_index = min(start_index + 2 * sublist_size, len(l)) L_tmp = [] while (index_left < left_stop_index and index_right < right_stop_index): if L[index_left] < L[index_right]: L_tmp.append(L[index_left]) index_left += 1 else: L_tmp.append(L[index_right]) index_right += 1 if index_left < left_stop_index: L_tmp.extend(L[index_left : left_stop_index]) if index_right < right_stop_index: L_tmp.extend(L[index_right : right_stop_index]) L[start_index : right_stop_index] = L_tmp def merge_sort(l): chunksize = 1 while chunksize < len(l): left_start_index = 0 # Start of left chunk in each pair while left_start_index + chunksize < len(l): merge(l, left_start_index, chunksize) left_start_index += 2 * chunksize chunksize *= 2 # Problem 3: Show list contents at this point Page 3 of 14

Problem 1: Binary search (10 points) Consider the following sorted list: L = [ 'becker', 'borg', 'federer', 'hewitt', 'murray', 'nadal', 'safin', 'sampras' ] and the binary search code on page 2. You may want to label the elements of L with their numeric index values before proceeding. (a) Fill out the following table tracing the call binary_search(l, 'federer'), a binary search for 'federer' in this list, according to the location of comment in the code. You should fill out one row per iteration of the loop. If there are more rows than iterations, leave the extra rows blank. Iteration Value of first Value of last Value of middle Value of L[middle] 1 2 3 4 5 (b) Fill out the following table tracing call to binary_search(l, 'roddick'), a binary search for 'roddick' in this list. Iteration Value of first Value of last Value of middle Value of L[middle] 1 2 3 4 5 Page 4 of 14

Problem 2: Selection sort (10 points) Consider the following list: L = [ 'sampras', 'murray', 'hewitt', 'borg', 'nadal', 'becker', 'safin', 'federer'] In the diagrams below, show the contents of the list after each of the first 4 iterations of the for-loop in selection_sort. If the list does not change from one iteration to the next, you can write SAME for the next iteration. INDEX INITIAL ORDER AFTER i=0 ITERATION AFTER i=1 AFTER i=2 AFTER i=3 0 sampras 1 murray 2 hewitt 3 borg 4 nadal 5 becker 6 safin 7 federer Page 5 of 14

Problem 3: Mergesort (10 points) Consider the following list: L = [ 'sampras', 'murray', 'hewitt', 'borg', 'nadal', 'becker', 'safin', 'federer'] In the diagrams below, show the contents of the list after each of the first 3 iterations of the outer while-loop in merge_sort. If the list does not change from one iteration to the next, you can write SAME for the next iteration. INDEX INITIAL ORDER AFTER chunksize=1 ITERATION AFTER chunksize=2 ITERATION AFTER chunksize=4 ITERATION 0 sampras 1 murray 2 hewitt 3 borg 4 nadal 5 becker 6 safin 7 federer Page 6 of 14

Problem 4a: Recursion (15 points) Consider the following function definition: def rec(l,e): # parameter L is a list of numbers # parameter e is a number if len(l) == 0: return True if L[0] < e: return False return rec(l[1:],e) A. What does the following snippet of code return? L = [0] rec(l, 2) B. Show the chain of recursive calls, and state what the return value is for each call, starting with: L = [27, 16, 36, 4, 9] rec(l, 4) C. How would you summarize what this function does in one sentence? Don t explain the code line-by-line. Provide a higher-level description like adds x and y or computes x factorial. Page 7 of 14

Problem 4b: Recursion (5 points) Consider the following function definition: def A(x) : print(x) if (x == 0) : return 0 else : r = x//2 return 1 + A(r) Specify the output (from the print statement) and the return value val obtained with the following function call: val = A(6) Output: Return value val: Page 8 of 14

Problem 5: Defining classes (25 points) In this problem, you will define a class to represent a Car. If you use the input() or print() functions in your solution to this problem, you re doing it wrong! Your class should be named Car, and you should define the following methods: init : This method initializes a Car object. Initialize the attributes to store the car s name (eg. Toyota Camry ), how many cylinders it has (eg. 6) and a boolean value to indicate whether it is electric or not. str : This method returns a string with the Car object s attributes, formatted as follows: Toyota Camry has 6 cylinders. Alas! It is not electric. or BMW i3 has 0 cylinders. Boy! It is electric. The above output is just an example: you should use the actual values in place of values that are underlined. Note the second statement is based on whether the car is electric or not. lt : This method compares self to another Car object. It returns True if the self object has less number of cylinders than another Car object, and False otherwise get_num_cylinders: This method returns the number of cylinders is_electric: This methods returns True if the car is electric and False otherwise upgrade: This upgrades the car to be an electric car. At the same time, it reduces the number of cylinders to 0. hybrid: This method creates a hybrid car by taking in another Car object as a parameter and adding it s name to the name of the current object (self). In addition, if the other Car object is electric, it makes the current car object (self) electric also. [Write code in next page] Page 9 of 14

[WRITE YOUR PROBLEM 5 CODE HERE] The last page of this exam has extra space for you to write your solution. Page 10 of 14

Problem 6: Using classes (25 points) For this problem, you must write a complete program. However, you can assume that the Car class from Problem 5 has already been correctly defined for you. To earn full credit, you must use the methods of the Car class whenever possible. Read the instructions carefully before you start coding! Your program should do the following: 1. A function called CreateCar to do the following: Ask the user to enter the car name (consisting of exactly two words), followed by number of cylinders (given after letter V) and a string identifying car as electric or regular, all on the same line. For example: Toyota Camry V6 regular or BMW i3 V0 electric If the user s line is blank, return None Otherwise, if the user did not provide exactly 4 values separated by whitespace, exit the program with an error message. If the first character of third word is not the letter V, exit the program with an error message. Otherwise, obtain the number of cylinders (you can assume that user only provides values between 0 and 9) Determine whether the car is electric or not. You can assume that user only provides the string regular or electric but can do so in any combination of uppercase or lowercase letters (Example: ReguLar). Create and return a Car object that uses the information the user entered. 2. A main function to do the following: Call CreateCar repeatedly until the user enters a blank line instead of the car information. Use the results of CreateCar to build a list of cars and print out each car s information consisting of name, number of cylinders and whether it is electric or not. Using your Car methods, find o Total number of electric cars and print it. o The car with maximum number of cylinders. Let s call it CarA. o Any car that is not electric (assume at least one exists). Let s call it CarB. o Upgrade CarA. o Make CarB a hybrid between itself and CarA, and print out it s new information. The last page of this exam has extra space for you to write your solution. Page 11 of 14

[WRITE YOUR PROBLEM 6 CODE HERE] Page 12 of 14

[EXTRA SPACE FOR PROBLEMS 5 AND 6] Page 13 of 14

[EXTRA SPACE FOR PROBLEMS 5 AND 6] Page 14 of 14