CS-141 Final Exam Review December 16, 2015 Presented by the RIT Computer Science Community

Similar documents
CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community

CS-141 Final Exam Review May 16, 2015 Presented by the RIT Computer Science Community

CS-141 Exam 2 Review April 4, 2015 Presented by the RIT Computer Science Community

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

Basic Data Structures (Version 7) Name:

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CSCI-140 Midterm Review October 10, 2015 Presented by the RIT Computer Science Community

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Lecture 5: Sorting Part A

CS 445: Data Structures Final Examination: Study Guide

Q1 Q2 Q3 Q4 Q5 Q6 Total

Summer Final Exam Review Session August 5, 2009

CS301 - Data Structures Glossary By

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

About this exam review

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

9. Heap : Priority Queue

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS 310 Advanced Data Structures and Algorithms

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Midterm solutions. n f 3 (n) = 3

COMP 103 Introduction to Data Structures and Algorithms

CS521 \ Notes for the Final Exam

Multiple-choice (35 pt.)

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

INSTITUTE OF AERONAUTICAL ENGINEERING

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

TREES AND ORDERS OF GROWTH 7

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

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

Balanced Binary Search Trees. Victor Gao

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Heaps, Heapsort, Priority Queues

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

A data structure and associated algorithms, NOT GARBAGE COLLECTION

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

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

Searching, Sorting. part 1

Section 05: Solutions

Sorting is a problem for which we can prove a non-trivial lower bound.

Data Structures and Algorithms

Properties of a heap (represented by an array A)

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

CS-301 Data Structure. Tariq Hanif

COMP Data Structures

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Computer Science Foundation Exam

The divide and conquer strategy has three basic parts. For a given problem of size n,

Data Structures and Algorithms Week 4

CSE 214 Computer Science II Heaps and Priority Queues

Computer Science E-22 Practice Final Exam

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Priority Queues and Heaps. Heaps of fun, for everyone!

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Programming II (CS300)

An Introduction to Trees

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

n 1 i = n + i = n + f(n 1)

Search and Optimization

CS/ENGRD2110: Final Exam

Stacks. Revised based on textbook author s notes.

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CS102 Binary Search Trees

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

CS 331 Midterm Exam 2

Binary Search Tree. Revised based on textbook author s notes.

Name CPTR246 Spring '17 (100 total points) Exam 3

( ) n 3. n 2 ( ) D. Ο

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

CSCE 2014 Final Exam Spring Version A

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Data Structures and Algorithms Chapter 4

Comparing Implementations of Optimal Binary Search Trees

CSC148-Section:L0301

O(n): printing a list of n items to the screen, looking at each item once.

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

The Heap Data Structure

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Structure and Interpretation of Computer Programs

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Transcription:

CS-141 Final Exam Review December 16, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu Python Basics 1. Although most in the industry will be reasonably forgiving when it comes to coding by hand, you should still be comfortable with the syntax of your language of choice. (a) Identify and fix the line(s) that have invalid syntax in the following code: If you are unsure about a specific line, just write a brief explanation of the error. Assume that incorrect lines do not affect the validity of other lines which may depend on them. 1 include math 2 3 int a = 0 4 b = 0 5 6 def function ( arg ): 7 return ( arg + 2) 8 9 c = " hello " 10 sum = b + math. sqrt (c) 11 total = a + ( b ** 0.5) 1 This line should read import math, or, alternatively, from math import *. 3 Python is a dynamically-typed language, so you cannot specify a variable s type in this manner. 10 This line will cause a runtime error because sqrt tries to perform mathematical operations on a string, which is invalid. (b) Which of the following are keywords or standard functions in Python? import in from class define int str String True false where while or not isinstance new print range float char bool xor sum All of these are valid, except define, String, false, where, new, xor, char. General Recursion 2. Sally is being plagued by an army of lookalike suiters, each of which presents her with an enticing but unbearable meal upon arrival. The dishes all smell amazing, however, so she can t help but try each one. A dish will never fail to disappoint her, but fortunately, some of the suiters shared recipes and created identical concoctions. Once Sally tastes a meal once, she can immediately smell out instances of the same dish and send their bearers away. (a) Write a Python function that, given a list of dishes, returns a list of the rejected dishes in the order they failed to fool her. (e.g. [1, 2, 2, 3, 3, 3] would return [2,3,3] ) 1

1 def find_dupes ( lst ): 2 result =[] 3 seen =[] 4 for member in lst : 5 if member not in seen : 6 seen. append ( member ) 7 else : 8 result. append ( member ) 9 return result (b) If your solution to 2a was iterative, write it recursively; if it was recursive, write it iteratively. 1 def find_recur (lst, seen =[]): 2 if len ( lst )==0: 3 return [] 4 if lst [0] not in seen : 5 seen. append ( lst [0]) 6 return find_recur ( lst [1:], seen ) 7 return [ lst [0]]+ find_recur ( lst [1:], seen ) or 1 def find_tail (lst, result =[], seen =[]): 2 if len ( lst )==0: 3 return result 4 if lst [0] not in seen : 5 seen. append ( lst [0]) 6 else : 7 result. append ( lst [0]) 8 return find_tail ( lst [1:], result, seen ) (c) What is the time complexity of your approach? Why? O(N 2 ), since the seen list must be searched linearly 2

3. Write a recursive function that reverses a string (e.g. Racecar yields racecar ). 1 def reverse ( string ): 2 if string == '' : 3 return string 4 else : 5 return reverse ( string [1:] ) + string [0] Other solutions are possible. 4. Perform a substitution trace on reverse( Doge ). reverse ('Doge ') = reverse ('oge ') + 'D' = ( reverse ('ge ') + 'o') + 'D' = (( reverse ('e') + 'g') + 'o') + 'D' = ((( reverse ('') + 'e') + 'g') + 'o') + 'D' = ((( '' + 'e') + 'g') + 'o') + 'D' = 'egod ' Files/IO 5. Write a function that takes in a file name, and returns the average size of a word in that file. Assume the files will only have 1 word per line, for example: No soup for you! which has an average length of: 3.25 Assume a function len( str ) which returns the length of a string is provided. 1 def average_wordlength ( filename ): 2 characters = 0 3 words = 0 4 for line in open ( filename ): 5 words += 1 6 characters += len ( line ) 7 return characters / words 3

Tail Recursion 6. Below is a function that, given a lower bound i, upper bound n, and function from integers to integers f, computes n f(k). k=i 1 def series_sum (i, n, f): 2 if(i == n): 3 return f( n) 4 return f( i) + series_ sum ( i + 1, n, f) (a) Invoke the function to compute 5 k=1 k 2. series_sum(1, 5, lambda x: x**2) OR def square(x): return x**2 series_sum(1, 5, square) (b) Rewrite the series sum function to be tail recursive. 1 def series_sum (i, n, f, s =0): 2 if(i == n): 3 return s + f(n) 4 return series_sum (i + 1, n, f, s + f(i)) (c) What is the advantage of the new implementation? Tail recursive functions make no additional calculations after the execution of the recursive call completes; thus, they do not necessitate the preservation of stack frames belonging to intermediate recursive invocations. Provided the language implementation supports sufficient optimization (Note: Python does not), there is no more runtime memory overhead for calling a tail recursive function that makes 100 recursive calls as compared to one that makes none at all. The true benefit becomes apparent when the recursion depth becomes much deeper: a tail recursive function can handle a recursion depth in the millions, which would cause a non tail recursive one to overflow the stack. Greedy Algorithms 7. Given that an algorithm is greedy, is it guaranteed to return an optimal solution? NO. Greedy algorithms always choose the current best solution, which is not necessarily the overall best solution! 4

Structures 8. For the sake of this question, you find yourself to be the head programmer under Kim Jong Un s glorious reign. It also just so happens that a nation-wide track meet is being held today. Thus, the glorious leader has demanded that you write a program to keep track of information relating to all track runners present at the event. (a) Write a struct called TrackRunner to keep track of each competing runner. You will need to store each runner s name (a string), age (an int), and fastesttime (an int). from rit_lib import * class TrackRunner ( struct ): _slots = ( (str,'name '), (int,'age '), (int,'fastesttime ') ) (b) Now write a function to make an individual TrackRunner struct. def makerunner ( name, age, fastesttime ): return TrackRunner ( name, age, fastesttime ) (c) The glorious leader has decided that, on this day, no runner named Joe may win gold. Given a list of TrackRunner structs, write the function awinnerisyou(runners) that returns the runner in the list runners with the fastest time whose name is not Joe. Use this function to find the runner s name, age, and fastest time and print those results. def awinnerisyou ( runners ): best = None for i in range ( len ( runners )): curr = runners [i] if curr. name!= 'Joe ': if best == None : best = curr elif best. fastesttime > curr. fastesttime : best = curr return best winner = awinnerisyou ( runnerslst ) print ( winner.name, winner.age, winner. fastesttime ) 5

Stacks and Queues 9. How can you use a stack to check if an input string of exclusively parentheses, brackets, and braces is properly balanced? (e.g. [{}] is accepted, but (() and [(]) are not.) Assume that you are provided with a Stack structure that has push(), pop(), and peek() functions. 1 def closing_match ( char ): 2 if char == '(': 3 return ')' 4 elif char == '[': 5 return ']' 6 elif char == '{': 7 return '}' 8 elif char == '<': 9 return '>' 10 else : 11 return None 12 13 def delims_are_balanced ( instring ): 14 ''' 15 String -> boolean 16 17 Determines if the input string has balanced delimiters. 18 Delimiters are () [] {} and <>. 19 20 ''' 21 stack = Stack () 22 for char in instring : 23 if closing_match ( char )!= None : # a starting grouping symbol 24 stack. push ( char ) 25 else : 26 if stack. is_empty (): 27 return False 28 elif char == closing_match ( stack. peek ()): 29 stack. pop () 30 else : # mismatched grouping symbol 31 return False 32 return stack. is_empty () 6

Searching 10. Given the sorted list [1, 4, 9, 16, 25, 69, 420, 1337], write out the steps that a binary search would take to find the number 69. [1,4,9,16, 25,69,420,1337] [1,4,9,16,25,69, 420,1337] [1,4,9,16,25, 69,420,1337] Trees 11. (a) If we have a balanced binary search tree containing n nodes: i. What is its height? log 2 (n) ii. How much time would it take to traverse to any of the leaf nodes of this tree? log 2 (n) iii. What s the worst case search time for an unbalanced search tree? n (b) How many leaves are in a complete BST containing n nodes? (n + 1)/2 (c) In terms of runtime efficiency, what is the worst possible configuration (e.g. arrangement of nodes and their children) of a binary search tree? Sketch a small example of what such a configuration looks like. A perfectly imbalanced tree, in which all nodes have one or 0 children. 1 2 3 4 5 (d) Based on your answer above, what data structure is such a BST reminiscent of? A perfectly imbalanced tree as described above operates like a linked list. 7

12. (a) Write a binary tree program that defines the following: a TreeNode struct with the given properties: - data (an object) : the data in this node - left : the TreeNode head of the left subtree (or None) - right : the TreeNode head of the right subtree (or None) a Tree struct with the given properties: - size (an int) : the size of the binary tree - head (a TreeNode) : the head node a recursive function is in tree(head, element) that determines whether the binary tree with the given head contains the specified element 1 from rit_lib import * 2 3 class TreeNode ( struct ): 4 _slots = (( object,'data '), 5 (( ' TreeNode ', NoneType ),'left '), 6 (( ' TreeNode ', NoneType ),'right ') ) 7 8 class Tree ( struct ): 9 _slots = ( (int,'size '), ( TreeNode,'head ') ) 10 11 def is_in_tree ( head, element ): 12 if head == None : 13 return False 14 elif head. data == element : 15 return True 16 elif head. data < element : 17 return is_in_tree ( head. right, element ) 18 elif head. data > element : 19 return is_in_tree ( head. left, element ) (b) Give an example of how you could test your code. newtree = Tree (3, TreeNode (3, TreeNode (1, None, None ), TreeNode (4, None, None ))) print ( is_in_tree ( newtree. head, 1)) # -> True print ( is_in_tree ( newtree. head, 3)) # -> True print ( is_in_tree ( newtree. head, 5)) # -> False 8

Heaps 13. For a binary heap containing n elements, what is the maximum number of swaps occurring after an insert operation? log 2 (n + 1), rounded down. 14. Given a node in an array-based binary heap at index i, where are the indices of both its children? What is the index of its parent? The children are at 2i + 1 and 2i + 2. The parent is at i 1. Note that in 2 a 1 indexed 15. Run a heap sort in reverse order on the following list: [3,5,1,3,2,7,9], showing the heap at array system, each stage. Be sure to heapify the list first. the children Heapify: [3, 5, 1, 3, 2, 7, 9] [3, 5, 1, 3, 2, 7, 9] [1, 5, 3, 3, 2, 7, 9] [1, 3, 3, 5, 2, 7, 9] [1, 2, 3, 5, 3, 7, 9] [1, 2, 3, 5, 3, 7, 9] [1, 2, 3, 5, 3, 7, 9] Sort: [2, 3, 3, 5, 9, 7, 1] [3, 3, 7, 5, 9, 2, 1] [3, 5, 7, 9, 3, 2, 1] [5, 9, 7, 3, 3, 2, 1] [7, 9, 5, 3, 3, 2, 1] [9, 7, 5, 3, 3, 2, 1] [9, 7, 5, 3, 3, 2, 1] would be at 2i and 2i + 1. The parent would be at i 2. 9

Sorting 16. Fill in the table for the asymptotic running time of each sorting algorithm. Best Worst Average Merge sort O(nlog(n)) O(nlog(n)) O(nlog(n)) Quicksort O(nlog(n)) O(n 2 ) O(nlog(n)) Heap sort O(nlog(n)) O(nlog(n)) O(nlog(n)) 17. Below is Python code for a function that performs an insertion sort and prints data after each iteration of the for loop. 1 def insertion_ sort ( data ): 2 for marker in range ( 1, len ( data ) ): 3 val = data [ marker ] 4 i = marker 5 while i > 0 and data [i -1] > val : 6 data [i] = data [i -1] 7 i -= 1 8 data [ i] = val 9 print ( data ) (a) Write out what the function will print for the input list: [3,2,7,1]. [2,3,7,1] [2,3,7,1] [1,2,3,7] (b) What is the sort algorithm s time complexity? O(N 2 ) 18. In what scenario does Quicksort experience its worst-case time complexity? You may assume that we always pick the first element as the pivot. Data that is (nearly) sorted or is (nearly) sorted in reverse order. 19. What causes Quicksort to run so slowly on the input you describe in the last question? Quicksort splits its input into two lists based on the value of the pivot. If the pivot is either the smallest or the largest element, then one list will only have no elements, while the others will have all of the elements except the pivot. 20. In Quicksort, why should we select a random pivot value, rather than always pivoting on, for example, the first or last element? With real-world data, we re more likely to encounter ordered or semi-ordered data than randomized data. This makes it more likely for us run into Quicksort s worst-case time complexity. We run into this bad time complexity if we select pivots which are near the lowest or highest values. Selecting a random value to pivot on helps us encounter the average case evens out the distribution of ordered and unordered data. Even if we re getting in sorted data, if we select pivots randomly, we should be able to end up with average time complexity. 10

21. Show the stages of a merge sort and a quicksort on the following list: [3,5,1,3,2,7,9]. Be sure to identify your pivot. Merge sort: 3 5 1 3 2 7 9 3 1 2 9 5 3 7 3 2 1 9 5 7 3 3 2 1 9 5 7 3 2 3 1 9 5 7 3 1 2 3 9 3 5 7 1 2 3 3 5 7 9 Quicksort (using the first element in the list as a pivot): 3 5 1 3 2 7 9 1 2 3, 3 5 7 9 1 2 3, 3 5 7 9 1 2 3, 3 5 7 9 3, 3 5 7 9 3, 3 5 7 9 1 2 3 3 5 7 9 11