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

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

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 Final Exam Review December 16, 2015 Presented by the RIT Computer Science Community

CS 206 Introduction to Computer Science II

Priority Queue ADT. Revised based on textbook author s notes.

CSC148 Week 2. Larry Zhang

What is an algorithm?

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

CS171 Midterm Exam. October 29, Name:

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

Queues. Queue ADT Queue Implementation Priority Queues

ITI Introduction to Computing II

CS 331 Midterm Exam 2

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

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS 314 Exam 2 Spring

Problem One: Loops and ASCII Graphics

Linked Structures. See Section 3.2 of the text.

THE UNIVERSITY OF WESTERN AUSTRALIA

CS 216 Exam 1 Fall SOLUTION

CSCA48 Term Test 1 Seminar

Computer Science First Exams Pseudocode Standard Data Structures Examples of Pseudocode

Lesson 3: Understanding General Software Development

CS 331 Midterm Exam 2

Data structure and algorithm in Python

Faster Sorting Methods

CS 206 Introduction to Computer Science II

About this exam review

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

(b) If a heap has n elements, what s the height of the tree?

CS350 - Exam 1 (100 Points)

CS61B Fall 2015 Guerrilla Section 3 Worksheet. 8 November 2015

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

CS 211. Project 5 Infix Expression Evaluation

INSTITUTE OF AERONAUTICAL ENGINEERING

COS 226 Algorithms and Data Structures Spring Midterm

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

COS 226 Algorithms and Data Structures Fall Midterm

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

table1 = [ [1, 2, 3, 4], [4, 5, 6, 7], [8, 9,10,11] ] we get: >>> sumcol(table1,0) 13 >>> sumcol(table1,1) 16

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

Abstract Data Types Chapter 1

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Student Number: Lab day:

DSC 201: Data Analysis & Visualization

Programming Assignment 2

Computer Science E-22 Practice Final Exam

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

Stacks and Queues

CSC148H Week 3. Sadia Sharmin. May 24, /20

ECE 250 Algorithms and Data Structures

CS 161 Fall 2015 Final Exam

Matriculation number:

Linear Data Structure

B. Including the Event Structure within a loop. C. Configuring a Timeout case within the Event Structure

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CSCE 2014 Final Exam Spring Version A

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Computer Science Foundation Exam

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Computer Science Spring 2005 Final Examination, May 12, 2005

Motivation for Queues

Computer Science Foundation Exam

Munster Programming Training - Cycle 2

CS24 Week 4 Lecture 2

CS 314 Midterm 2 Fall 2012

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Loop Invariants. while!done do // what is true at every step // Update/iterate // maintain invariant od CPS

CS 331 Summer 2017 Final Exam

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

CS Prelim 2 Review Fall 2018

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

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

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

UNIT 6A Organizing Data: Lists Principles of Computing, Carnegie Mellon University

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

CP222 Computer Science II. Searching and Sorting

STACKS AND QUEUES. Problem Solving with Computers-II

csci 210: Data Structures Stacks and Queues

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CS 111X - Spring Final Exam - KEY

CS 302 ALGORITHMS AND DATA STRUCTURES

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

ECE250: Algorithms and Data Structures Midterm Review

811312A Data Structures and Algorithms, , Exercise 1 Solutions

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

I want an Oompa-Loompa! screamed Veruca. Roald Dahl, Charlie and the Chocolate Factory

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Data structure and algorithm in Python

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Data Structure Advanced

Computer Programming II Python

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

Transcription:

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community http://csc.cs.rit.edu Linked Lists 1. You are given the linked sequence: 1 2 3. You may assume that each node has one field called value and one called rest. (a) 1 points to 2 and 2 points to 3. What does 3 point to? The implementer may cause the 3 node to point to either None or a sentinel node. (b) Draw out the linked list structure and add a 5 to the end. +----------+ +----------+ +----------+ +------------+ value: 1 value: 2 value: 3 value: 5 rest: -------> rest: -------> rest: -------> rest: None +----------+ +----------+ +----------+ +------------+ OR +----------+ +----------+ +----------+ +----------+ +-----------------+ value: 1 value: 2 value: 3 value: 5 value: SENTINEL rest: -------> rest: -------> rest: -------> rest: -------> +----------+ +----------+ +----------+ +----------+ +-----------------+ (c) Write pseudocode to add an element onto the end of a linked sequence. def append ( l i n k e d l i s t, value ) : newend = new node newend. value = value newend. r e s t = None # ( or SENTINEL) cur = l i n k e d l i s t while cur i s not end o f l i n k e d l i s t : cur = cur. r e s t cur. r e s t = newend (d) Let s say we want to add a 4 to the list from 1b. What is a procedure for inserting this value at a certain position in the linked sequence? (There are many possibilities.) Use some variation of this strategy: Find the preceding element s node, link its rest node to the new node containing 4, then link that new node to what was the following node. 1

Binary Search using Python Lists 2. 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, 1 6, 2 5, 6 9, 4 2 0, 1 3 3 7 ] [ 1, 4, 9, 1 6, 2 5, 6 9, 4 2 0, 1 3 3 7 ] [ 1, 4, 9, 1 6, 2 5, 6 9, 4 2 0, 1 3 3 7 ] Sorting 3. Below is Python code for a function that performs an insertion sort and prints data after each iteration of the for loop. 1 def i n s e r t i o n s o r t ( data ) : 2 for marker in range ( 1, len ( data ) ) : 3 v a l = data [ marker ] 4 i = marker 5 while i > 0 and data [ i 1] > v a l : 6 data [ i ] = data [ i 1] 7 i = 1 8 data [ i ] = v a l 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 ) 2

4. 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 3

Stacks and Queues 5. It is possible to implement both stacks and queues using only simple Python lists. (a) Write the following functions that implement stack functionality atop a Python list. Your stack must provide the following functionality: push(lst, elm) Push elm onto the top of the stack pop(lst) Return the top element of the stack and remove it from the stack isempty(lst) Return whether the stack is empty peek(lst) Return the top element of the stack without modifying the stack 1 def push ( l s t, val ) : 2 l s t. append ( val ) 3 def pop ( l s t ) : 4 return l s t. pop ( ) 5 def peek ( l s t ) : 6 return l s t [ 1] 7 def isempty ( l s t ) : 8 return ( len ( l s t ) == 0) (b) Write the following methods to create a queue in similar fashion to previous question (with a Python list as the data structure managing elements under the hood ): enqueue(lst, val) - put a value into the queue dequeue(lst) - take a value out of the queue 1 def enqueue ( l s t, val ) : 2 l s t. append ( val ) 3 def dequeue ( l s t ) : 4 l s t. pop ( 0 ) (c) Which of the data structures you implemented is more efficient and why? Give a better way to implement the slower structure, and discuss how this would change the time complexity of its operations. Because the queue must be able to modify both ends of the list, it pays an O(n) cost to remove the beginning element during each dequeue operation. This could be reduced to O(1) by using a linked list instead of a Python list. 4

Structures 6. For each of the following structures with the given attributes, write the code to define the structures: A Hotel name (a string) rooms (a list of Room structures) location (a string) A Room number (an integer) capacity (an integer) price per night (a float) 1 from r i t l i b import 2 3 Hotel = s t r u c t t y p e ( Hotel, 4 ( str, name ), ( l i s t, rooms ), ( str, l o c a t i o n ) ) 5 6 Room = s t r u c t t y p e ( Room, 7 ( int, number ), ( int, c a p a c i t y ), ( float, p r i c e ) ) 5

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! 8. In the game Black and White 1, the player is faced with a row of identical double-sided chips. You can probably guess what colors the two sides of each chip are. The objective is to flip as many chips as necessary so their exposed colors match that of a target pattern. The catch? Reordering the chips is said to be Impossible by those who seem to know what they re talking about. The real catch? Flipping a group of consecutive tiles can be accomplished in a single action. If every flip takes one action, write a function bwmoves that, given a starting pattern and target pattern as equal-length strings, returns the minimum number of actions required to get them to match. For instance, bwmoves( BBWBBWBBBB, WWWWWBBWWB ) should return 3. 1 def bwmoves( s t a r t, t a r g e t ) : 2 a c t i o n s=0 3 f i r s t =0 4 for index in range ( len ( s t a r t ) ) : 5 i f s t a r t [ index]== t a r g e t [ index ] : 6 i f f i r s t!= index : # Each f l i p works up to ( but not i n c l u d i n g ) 7 a c t i o n s+=1 # the index p o i n t e r. I f f i r s t==index, t h a t s 8 f i r s t=index+1 # 0 elements, so t h e r e i s nothing to f l i p. 9 # ( i. e. There were two no f l i p s in a row. ) 10 i f s t a r t [ 1]!= t a r g e t [ 1]: 11 a c t i o n s+=1 12 return a c t i o n s 1 Special thanks to Professor Butler for unwittingly allowing us to rip off his problem. 6