: Principles of Imperative Computation, Spring Homework 3 Theory [Update 1]

Similar documents
You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

Print out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.

Lecture 10 Notes Linked Lists

Most of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.

Lecture 10 Notes Linked Lists

Most of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.

Lecture Notes on Queues

Midterm 1 Exam Principles of Imperative Computation. Thursday 6 th October, This exam is closed-book with one sheet of notes permitted.

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

Print out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.

Midterm 1 Solutions Principles of Imperative Computation. Thursday 6 th October, 2016

: Principles of Imperative Computation. Summer Assignment 4. Due: Monday, June 11, 2012 in class

Midterm I Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. September 30, 2010

Lecture 10 Linked Lists

Midterm 2 Exam Principles of Imperative Computation. Tuesday 31 st March, This exam is closed-book with one sheet of notes permitted.

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

Lecture 9 Stacks & Queues

Lecture 9 Stacks & Queues

Lecture 18 Restoring Invariants

SOME TYPICAL QUESTIONS, THEIR EXPECTED ANSWERS AND THINGS

Lecture Notes on Queues

Final Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. December 10, Name: Andrew ID: Section:

CS171 Midterm Exam. October 29, Name:

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

Lecture 8 Data Structures

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Lecture 9 Notes Stacks & Queues

Lecture 11 Unbounded Arrays

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

The assignment is due by 1:30pm on Tuesday 5 th April, 2016.

Chapter 9 STACK, QUEUE

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

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, 2012

Lecture 13 Notes Sets

Algorithm Design and Analysis Homework #4

Lecture 13 Notes Sets

Lecture 24 Notes Search in Graphs

Lecture Notes on Memory Layout

Lecture Notes on Interfaces

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 3 Notes Arrays

Lecture Notes on Quicksort

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Computer Science 302 Spring 2007 Practice Final Examination: Part I

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

CS24 Week 4 Lecture 2

15-122: Principles of Imperative Computation, Fall 2015

CMPT 125: Practice Midterm Answer Key

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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)

Lecture 15 Notes Binary Search Trees

Lecture Notes on Binary Search Trees

: Principles of Imperative Computation. Fall Assignment 4: Text Editor - UPDATE 2. Due: Friday, October 19, 2012 by 23:59 [Updated]

Lecture Notes on Quicksort

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

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

C0MP1921/2011/2091 SAMPLE Final Exam Data Structures and Algorithms/Data Organisation/Computing 2

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

15-122: Principles of Imperative Computation, Spring 2013

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

The print queue was too long. The print queue is always too long shortly before assignments are due. Print your documentation

COSC242 Lecture 7 Mergesort and Quicksort

Lecture 13 Hash Dictionaries

Lecture Notes on Arrays

Lecture 17 Priority Queues

University of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm

15-110: Principles of Computing, Spring 2018

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, Name: Andrew ID: Section:

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

Lab 7 1 Due Thu., 6 Apr. 2017

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Sorting. Bringing Order to the World

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

PRIORITY QUEUES AND HEAPS

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

15 122: Principles of Imperative Computation. Practice Exam August 4, 2015

Stacks and Queues. CSE Data Structures April 12, 2002

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

.:: UNIT 4 ::. STACK AND QUEUE

Computer Science 1 Study Union Practice Problems. What are the arguments to malloc? calloc? realloc? What do they do?

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Computer Science 62. Midterm Examination

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering EXAM November 2016

Announcements. CS18000: Problem Solving And Object-Oriented Programming

Lecture 17 Notes Priority Queues

: Principles of Imperative Computation, Fall Written Homework 1 Solutions

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /5:30-7:00

UNIVERSITY REGULATIONS

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

COS 226 Midterm Exam, Spring 2009

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

COS 226 Algorithms and Data Structures Fall Midterm

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Transcription:

15-122 Homework 3 Page 1 of 15 15-122 : Principles of Imperative Computation, Spring 2013 Homework 3 Theory [Update 1] Due: Thursday, February 21, 2013, at the beginning of lecture Name: Andrew ID: Recitation: The written portion of this week s homework will give you some practice working with sorting algorithms, stacks, and queues. You can either type up your solutions or write them neatly by hand, and you should submit your work in class on the due date just before lecture begins. Please remember to staple your written homework before submission. Question Points Score 1 5 2 11 3 9 Total: 25 You must use this printout, include this cover sheet, and staple the whole thing together before turning it in. Either type up the assignment using 15122-theory3.tex, or print this PDF and write your answers neatly by hand.

15-122 Homework 3 Page 2 of 15 1. Stacks and Queues This question focuses on using interface functions for stacks and queues. The division between an implementation (the details of a data structure s low-level details) and an interface (how a client is able to interact with the data structure) is one of the most crucial distinctions in computer science. Consider the following interfaces for queue and stack that store elements of the type elem: /* Queue Interface */ queue queue_new(); /* O(1) */ bool queue_empty(queue Q); /* O(1) */ void enqueue(elem e, queue Q); /* O(1) */ elem dequeue(queue Q) /* O(1) */ //@requires!queue_empty(q); ; /* Stack Interface */ stack stack_new(); /* O(1) */ bool stack_empty(stack S); /* O(1) */ void push(elem e, stack S); /* O(1) */ elem pop(stack S) /* O(1) */ //@requires!stack_empty(s); ; For this question, assume that we do not know how these data structures are implemented. That is, we don t know if the programmer used arrays or linked lists, or something else just that they somehow implemented the functions shown above, and that they have the same observable behavior as a typical stack or queue. As a client, you can only use the interface functions specified above to interact with these data structures.

15-122 Homework 3 Page 3 of 15 (2) (a) Write a function stack_reverse(stack S) that uses a queue to reverse the order of elements on the stack S that is passed in. Include proper annotations in your code. void stack_reverse(stack S)

15-122 Homework 3 Page 4 of 15 (3) (b) Write a function stack_reverse(stack S) that uses stacks to reverse the order of elements on the stack S that is passed in. Include proper annotations in your code. void stack_reverse(stack S)

15-122 Homework 3 Page 5 of 15 2. Quacks. This question focuses on writing code to implement a particular data structure according to some given specifications. This involves making the implementation: 1. Generic (it does not make any assumptions regarding the data the client wishes to place in the data structure) 2. Accurate (it does not violate any of the data structures properties) 3. Safe (it rejects any malformed input that the client may enter) After learning about queues and stacks in 15-122, Fred Hacker decided he d try to combine the data structures to get the best of both worlds. Consider the following interface for a data structure that Fred calls a quack. bool quack_empty(quack Q); /* O(1) */ bool quack_new(); /* O(1) */ void enqueue(elem e, quack Q); /* O(1) */ elem dequeue(quack Q); /* O(1) */ void push(elem e, quack Q); /* O(1) */ elem pop(quack Q); /* O(1) */ bool quack_equal(quack Q1, quack Q2); /* O(n) */ Quacks accept elements of the type elem, which is just a generic type name if we wanted to put ints in the quack we would use typedef int elem. The actual type of elem is provided by the client; the implementation of quacks should not rely upon the concrete details of elem. Fred decided to use a linked list to implement his quack. The linked list he used is defined with these structs: struct list_node elem data; struct list_node *next; ; typedef struct list_node node; struct quack_header node *head; node *tail; ; typedef struct quack_header* quack; Fred thinks he ll be able to make lots of money off of his quack data structure, so he s kept its implementation secret. All we know is that his implementation uses the above structs to store its data and that each function has the runtime specified in the interface, that it acts like a stack when only push and pop are used, and that it acts like a queue when only enqueue and dequeue are used. Your task is to implement Fred s quack before he can release it, so you can make any money that it s worth.

15-122 Homework 3 Page 6 of 15 In your implementation, do not use an extra node at the end of the list as we ve done in class for our implementation of queues and stacks. 1. In other words, if tail is not NULL, then tail->data should contain actual data that we want to store, and tail->next should be NULL. 2. If tail is NULL, then head must also be NULL and the quack is empty. 3. In the case where there is only one thing in the quack, both head and tail should point to it. Your quack implementation, in other words, will look like this when there are four elements in the quack. (The head and tail must be assigned as shown below for relevant quack operations to have O(1) time; you should consider why this must be the case.) data next head tail You may assume you have the functions, which you may use for any pre/post conditions: bool is_quack(quack Q), which checks that the given quack is valid (it makes sure it s not NULL, that everything in the quack is as it should be, that there are no cycles in the list, that Q->head is NULL if Q->tail is NULL and vice-versa, etc.) bool elem_equal(elem e1, elem e2), which compares two elements of type elem and checks if they are equal or not. This is a client-side function, along with the type elem. elem peek_last(quack Q), which returns the last element of Q and does NOT modify it. elem peek_first(quack Q), which returns the first element of Q and does NOT modify it. In the following sub-parts, you will write the code necessary to implement quacks as per the description given above. Bear the following points in mind: 1. You must include all necessary pre-conditions/post conditions to guarantee that the operations work as specified above. Refer to the code given in lecture on the implementation of stacks and queues to get idea of what contracts to write. 2. Your functions must have the runtime performance specified in the interface. 3. Try to write the code on paper, with the aid of diagrams, instead of using coin or c0, as you will be expected to write code on the exams without access to either of these tools.

15-122 Homework 3 Page 7 of 15 (1) (a) Write the quack_empty function that returns true if and only if the given quack is empty. Your function should NOT modify the quack that is passed in. bool quack_empty(quack Q) (1) (b) Write a quack_new function that returns a valid, empty quack quack quack_new()

15-122 Homework 3 Page 8 of 15 (2) (c) Write an enqueue function which, given a quack Q, adds the element e to the tail of the quack. Include a contract that guarantees that the element e has been added to the end of the quack. void enqueue(elem e, quack Q)

15-122 Homework 3 Page 9 of 15 (2) (d) Write a dequeue function which, given a non-empty quack, removes the element at the head of the quack and returns it. elem dequeue(quack Q)

15-122 Homework 3 Page 10 of 15 (2) (e) Write a push function which, given a quack and an element e, adds e to the head of the quack. Include a contract that guarantees that the element e has been added to the front of the quack. void push(elem e, quack Q) (1) (f) Write a pop function which, given a nonempty quack, pops an element off the head of the quack and returns it. Hint: it should be possible to write this function in one simple line of code using functions you have already written. elem pop(quack Q)

15-122 Homework 3 Page 11 of 15 (2) (g) Write a quack_equal function which checks if two quacks are equal (ie they have the same number of elements and the elements are equal and are arranged in the same order. Your function should not assume anything about the type of data stored in the quacks. You can assume that both quacks have data of type elem. Your function may NOT modify the original quacks passed in bool quack_equal(quack Q1, quack Q2)

15-122 Homework 3 Page 12 of 15 3. Mergesort This question concerns the implementation of mergesort; an implementation is given below. The effect of copy_array in line 49 is to copy the array B[0, length) into A[lower, upper). A is changed so that A[lower + i] == B[i] for each i < length. 1 void mergesort (int[] A, int lower, int upper) 2 //@requires 0 <= lower && lower <= upper && upper <= \length(a); 3 //@ensures is_sorted(a, lower, upper); 4 5 if (upper lower <= 1) return; 6 int mid = lower + (upper lower)/2; 7 8 mergesort(a, lower, mid); 9 //@assert is_sorted(a, lower, mid); 10 11 mergesort(a, mid, upper); 12 //@assert is_sorted(a, mid, upper); 13 14 merge(a, lower, mid, upper); 15 return; 16 17 18 void merge (int[] A, int lower, int mid, int upper) 19 / @requires 0 <= lower && lower <= mid 20 && mid <= upper && upper <= \length(a); / 21 //@requires is_sorted(a, lower, mid); 22 //@requires is_sorted(a, mid, upper); 23 //@ensures is_sorted(a, lower, upper); 24 25 int length = upper lower; 26 int[] B = alloc_array(int, length); 27 int i1 = lower; 28 int i2 = mid; 29 30 for(int k = 0; k < length; k++) 31 //@loop_invariant 0 <= k && k <= length; 32 //@loop_invariant lower <= i1 && i1 <= mid; 33 //@loop_invariant mid <= i2 && i2 <= upper; 34 //@loop_invariant (i1 lower) + (i2 mid) == k; 35 //@loop_invariant is_sorted(b, 0, k); 36 / @loop_invariant k == 0 37 ((i1 == mid A[i1] >= B[k 1]) 38 && (i2 == upper A[i2] >= B[k 1]); / 39 40 if(i2 == upper (i1 < mid && A[i1] <= A[i2])) 41 B[k] = A[i1]; 42 i1++; 43 else 44 B[k] = A[i2]; 45 i2++; 46 47 48 //@assert is_sorted(b, 0, length); 49 copy_array(a, lower, upper, B, 0, length); 50

15-122 Homework 3 Page 13 of 15 In part (a) of this question we will reason operationally about the mergesort, and in part (b) we will reason logically about safety. (4) (a) Suppose we have the array A[] below, and call mergesort(a, 0, 8). Step through the execution of mergesort(a, 0, 8), and write down the contents of A[] after each merge call terminates. Thus, the first line should contain A[] immediately after the first merge call terminates, the second line should contain A[] immediately after the second merge call terminates, and so on. Note that mergesort does not always call merge, and that even when merge is called on a smaller subarray of A[], you should fill in the entire contents of A[]. Finally, the number of lines provided may be more than the number of times merge is called; any excess lines should be left blank. merge # A[] 7 5 0 3 4 1 2 6 1 2 3 4 5 6 7 8 9

15-122 Homework 3 Page 14 of 15 (5) (b) The merge function contains both a number of array accesses. For the following array accesses, list the facts that establish the safety of that array access, as well as the line that immediately provides each of those facts. You do not need to give the intermediate reasoning, just the raw facts that would allow you to establish safety and the lines of code that immediately justify those facts. Do not include any extra or unnecessary facts. Line 37, A[i1] 0 <= lower (line 19) lower <= i1 (line 32) i1 <= mid (line 32) i1!= mid (line 37) mid <= upper (line 20) upper <= \length(a) (line 20) Line 37, B[k-1] Line 38, A[i2] Line 38, B[k-1] (same as the justification from line 37) Line 40, A[i1]

15-122 Homework 3 Page 15 of 15 Line 40, A[i2] 0 <= lower && lower <= mid (line 19) mid <= i2 && i2 <= upper (line 33) i2!= upper (line 40) upper <= \length(a) (line 20) Line 41, B[k] 0 <= k (line 31) k < length (line 30) length = \length(b) (line 26) Line 41, A[i1] Line 44, B[k] (same as the justification from line 41) Line 44, A[i2]