2015-spring-prelim2-answers - CS 1110 Prelim 2(Print Last Name(Print First Name(Net ID Circle Your Lab ACCEL Tue 12:20 Tue 1:25 Tue 2:30 Tue 3:35 ACCEL

2015-spring-prelim2-answers - CS 1110 Prelim 2(Print Last...

This preview shows page 1 - 5 out of 14 pages.

CS 1110Prelim 2April 21, 2015(Print Last Name)(Print First Name)(Net ID)Circle Your Lab:ACCEL:Tue 12:20Tue 1:25Tue 2:30Tue 3:35ACCEL :Wed 10:10Wed 11:15Wed 12:20Wed 1:25Wed 2:30Wed 3:35PHILLIPS :Tue 12:20Tue 1:25Wed 12:20Write your name and NetID on every page.Check that your exam has (11) pages counting this one.In cases where you write code, you will lose points for ambiguous indentation.Academic IntegrityIt is a violation of the Academic Integrity Code to look at any exam other than your own, to look atany other reference material, or to otherwise give or receive unauthorized help. We also ask that you notdiscuss this exam with students who are scheduled to take a later makeup. Academic Integrity is expected ofall students of Cornell University at all times, whether in the presence or absence of members of the faculty.Understanding this, I declare I shall not give, use or receive unauthorized aid in this examination.(Signature)
Background image
Name:NetID:Problem 115 pointsProblem 215 pointsProblem 315 pointsProblem 420 pointsProblem 515 pointsProblem 620 points
Background image
Name:NetID:1Functions and Lists(a)Theeven-oddsort of a list that has even length permutes entries so that all the even-index entries comefirst followed by all the odd-indexed entries. To illustrate, suppose we have the following length-8 list:’a’’b’’c’’d’’e’’f’’g’’h’Here are the length-4 lists of the even-indexed entries and the odd-indexed entries:’a’’c’’e’’g’’b’’d’’f’’h’And here is the even-odd sort of the above length-8 list:’a’’c’’e’’g’’b’’d’’f’’h’This operation can be carried out very simply using list slicing and list concatenation. Indeed ifxhas lengthnandnis even, then the listx[0:n:2] + x[1:n:2]is the even-odd sort ofx. Implement that followingprocedure so that it performs as specifieddef EvenOddSort(x)""" Performs an even-odd sort of x""" Precondition: x is a list with even lengthImplement this function using just for-loops and subscripting. No list slicing or list concatenation allowed.Note thatEvenOddSortdoes not return any values.Solution:This problem builds on their perfect shuffle experience.n = len(x)m = n/2y = []z = []for k in range(m):y.append(x[2*k])z.append(x[2*k+1])for k in range(m):x[k] = y[k]x[k+m] = z[k]
Background image
Name:NetID:(b)Assuming that the procedureEvenOddSortis available, implement the following function so that itperforms as specified:def MultipleSort(x,N):
Background image
Image of page 5

You've reached the end of your free preview.

Want to read all 14 pages?

  • Spring '08
  • DAVIDGRIES
  • Following, Carriage return

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture