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

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

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

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

CS 115 Exam 3, Spring 2014

CS 115 Exam 3, Fall 2011

Module 08: Searching and Sorting Algorithms

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

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

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

CS 115 Exam 3, Spring 2011

Binary Search APRIL 25 TH, 2014

CS 1301 Exam 1 Answers Fall 2009

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 )

7. Tile That Courtyard, Please

Sorting and Searching

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

Midterm I Practice Problems

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

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

How many ways to make 50 cents? first-denomination Solution. CS61A Lecture 5. count-change. cc base cases. How many have you figured out?

Introduction to Computer Science Midterm 3 Fall, Points

CSc 372, Fall 2006; Quiz 1 Time: 3:00; Value: 2 points. 1. What is a fundamental difference between the if-then-else constructs in Java (or C) and ML?

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

CS 1110 Final Exam Solutions May 15th, 2014

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

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

Exam 2, Form A CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Exam 2, Form B CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

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

CS 1301 Exam 1 Fall 2009

Divide and Conquer. playing divide and conquer. searching in a sorted list membership in a sorted list

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

Midterm #2a Fall minutes

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

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

Sequence Types FEB

CS150 Sample Final. Name: Section: A / B

C/C++ Programming Lecture 18 Name:

List Mutation (Module 4) Accumulative Recursion (Module 5) Efficiency (Module 7) Searching & Sorting (Module 8) Dictionaries (Module 9)

CSE341 Spring 2017, Final Examination June 8, 2017

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

Spring 2017 CS 1110/1111 Exam 2

CS150 - Sample Final

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

Midterm Exam 2B Answer key

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

University of California, Berkeley. (2 points for each row; 1 point given if part of the change in the row was correct)

Final Examination, Semester 1, 2015 COMP10001 Foundations of Computing

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

Structure and Interpretation of Computer Programs

Midterm #1 Fall minutes

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

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

: Intro Programming for Scientists and Engineers Final Exam

CS 115 Exam 3, Spring 2010

Data Types. Operators, Assignment, Output and Return Statements

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

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

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

CS 1301 Exam 1 Fall 2010

Spring 2017 CS 1110/1111 Exam 1

Final Exam Version A

CSI33 Data Structures

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Flow Control: Branches and loops

Structure and Interpretation of Computer Programs

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

15-110: Principles of Computing, Spring 2018

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Structure and Interpretation of Computer Programs

EECE.2160: ECE Application Programming Spring 2018 Exam 3 May 10, 2018

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

EXAMINATION INSTRUCTIONS

Structure and Interpretation of Computer Programs

CSE341 Spring 2017, Final Examination June 8, 2017

Computer Science Foundation Exam

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 1 Fall 2010

CS 331 Summer 2017 Final Exam

Reviewing all Topics this term

Structure and Interpretation of Computer Programs

15-110: Principles of Computing Sample Exam #1

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CS150 Sample Final Solution

CSCI 150 Exam 2 Solutions

Midterm #2a Spring minutes DO NOT WRITE IN THIS AREA

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

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

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 20 Problem 6: Using classes 30 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 return None 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 = [ 'candidate', 'clinton', 'democracy', 'election', 'president', 'states', 'trump', 'votes' ] 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 table tracing the call v = binary_search(l, 'president'), a binary search for 'president' in this list, according to the 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. At the end, write the value v returned by the function Iteration Value of first Value of last Value of middle Value of L[middle] 1 2 3 4 Return value v: (b) Fill out the following table tracing call to v = binary_search(l, 'code'), a binary search for 'code' in this list. At the end, write the value v returned by the function Iteration Value of first Value of last Value of middle Value of L[middle] 1 2 3 4 Return value v: Page 4 of 14

Problem 2: Selection sort (10 points) Consider the following list: L = [ 'votes', 'trump', 'states', 'clinton', 'election', 'democracy', 'candidate', 'president'] In the diagram below, show the contents of the list after each of the first 4 iterations of the for-loop in selection_sort. INDEX INITIAL ORDER AFTER i=0 ITERATION AFTER i=1 ITERATION AFTER i=2 ITERATION AFTER i=3 ITERATION 0 votes 1 trump 2 states 3 clinton 4 election 5 democracy 6 candidate 7 president Page 5 of 14

Problem 3: Mergesort (10 points) Consider the following list: L = [ 'votes', 'trump', 'states', 'clinton', 'election', 'democracy', 'candidate', 'president'] In the diagram below, show the contents of the list after each of the first 3 iterations of the outer while-loop in merge_sort. INDEX INITIAL ORDER AFTER chunksize=1 ITERATION AFTER chunksize=2 ITERATION AFTER chunksize=4 ITERATION 0 votes 1 trump 2 states 3 clinton 4 election 5 democracy 6 candidate 7 president Page 6 of 14

Problem 4a: Recursion (10 points) Consider the following function definition: def fun(l): # parameter L is a list if len(l) == 1: return L[0] % 2 #Gives remainder on dividing by 2 else: return L[0] % 2 + fun(l[1:]) A. What does the following snippet of code return? fun([7]) B. Show the chain of recursive calls, and state what the return value is for each call, starting with: fun([4, 23, 11, 16, 7]) 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 (10 points) Consider the following function definition: def func(s) : # parameter s is a string print("current string is", s) if len(s) <= 1: print("base case! Returning", s) return s val = func(s[1:]) print("adding return value", val, "to", s[0]) return val + s[0] Specify the output obtained upon executing the following statement: print ("Final answer is", func("rac")) Page 8 of 14

Problem 5: Defining classes (20 points) In this problem, you will define a class to represent profile of a Twitter Member. Your class should be named TwitterProfile, and you should define the following methods: init : This method initializes a TwitterProfile object. Initialize the attributes to store the member s name (eg. Tim Baker) and a list of tweets (eg. [ Finals in SSU, Studying for CS115, Happy Holidays! ]). There is another attribute to store the pinned tweet (Tweet that is showcased on member s Twitter page) but it is initially an empty string. get_name: This method returns the member s name get_pinned_tweet: This method returns the member s pinned tweet get_num_tweets: This methods returns the number of tweets of this member set_pinned_tweet: This method sets the pinned tweet of the current TwitterProfile object (self) to be the element at k th index of the list of tweets, where the index k is a parameter to the method (eg. if k=2, then element at 2 nd index, Happy Holidays!, becomes the pinned tweet). add_tweet: This method adds a new tweet to the list of tweets of the current TwitterProfile object (self), where the new tweet is a parameter to the method str : This method returns a string with the TwitterProfile object s attributes, formatted as follows: Tim Baker (pinned tweet: Happy Holidays!) has 3 tweets The above output is just an example: you should use the actual values in place of values that are underlined. lt : This method compares self to another TwitterProfile object. It returns True if the self object has less number of tweets than another TwitterProfile object, and False otherwise [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 (30 points) For this problem, you must write a complete program. However, you can assume that the TwitterProfile class from Problem 5 has already been correctly defined for you. To earn full credit, you must use the methods of the TwitterProfile class whenever possible. Read the instructions carefully before you start coding! Your program should do the following: 1. A function called CreateProfile to do the following: Ask the user to enter the member s name and number of tweets on two lines. For example: Tim Baker 3 If the number of tweets is 0, return None Create a list to store the tweets of this member. Ask the user to enter each tweet and add it to this list. Create a TwitterProfile object that uses the information the user entered (member s name, list of tweets). Using the methods of the TwitterProfile object just created, make the first tweet in the list to be the pinned tweet. Return the TwitterProfile object. 2. A main function to do the following: Call CreateProfile repeatedly until the member s number of tweets is 0. Use the results of CreateProfile to build a list of Twitter member profiles. After creating the list, use the methods of the TwitterProfile class to find o Members who have more than 100 tweets and print the pinned tweet of each such member. o The member with the least number of tweets. Call it mintw o The member with the maximum number of tweets. Call it maxtw maxtw adds a new tweet Woah! Got max tweets. and makes it to be their pinned tweet. Print maxtw s profile information summarizing their name, pinned tweet and number of tweets. mintw adds a new tweet maxtw s Name wow! But how?.substitute actual name of maxtw at the underlined location. Print mintw s profile information summarizing their name, pinned tweet and number of tweets. 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