Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Spring 2003: Duplicate a List

Comments? Concerns? Answers? Questions? Questions about Answers?
Back to Sp2003 Midterm Review 2


Can anyone help with this?


For this problem, do we have to return [1,1,2,2,3,3] or is it ok to return [1,2,3,1,2,3]?

I'm looking for [1,1,2,2,3,3]. Mark Guzdial



def duplicateList(list):
  length = len(list)
  newlist =list + list
  newlength = len(newlist)
  x=1
  y=2 
  z = 1
  for i in newlist:
    newlist[newlength -x] = list [length -z]
    newlist[newlength-y] = list [length-z]
    x = x+2
    y = y+2
    z = z +1
  print newlist

This worked when I tested it a couple times. Does it look right?Katie Graybeal

Interesting! Yup, it seems to work, but it can be made less complicated. Mark Guzdial


I can't get Katie's program to work. What am I doing wrong?

Make sure you are taking a list as input. You must do
>>>list=["1","4","6","7",9]
>>>duplicateList(list)

So you can't do:
duplicateList("1","4","6","7","9")

Why not? That's how I've previously entered lists.

Yes, you can do that, if you don't forget the square brackets ["like","this"]. Mark Guzdial


ooh! i got it! it looks so simple once you see it... =) Kristie Fisher
def duplicateList(list):
  newlist = list + list
  newlist.sort()
  print newlist


umm, slight problem. what if your original list was ["5","3","7"]? when you run it through this, you get [3,3,5,5,7,7] not [5,5,3,3,7,7] since sort puts it in numerical value, right?
does that matter?

def duplicateList(list):
  newlist = []
  for item in list:
    newlist = newlist + [item] + [item]
  return newlist

This one works for me. It doesnt use sort and isnt so much code. Scott Forbus

Slick, Scott! Yes, that works. Here's another idea. What if you had an index into the original list, and you copied (like Scott does) the element at the index into a newlist. Then you added 0.5 to the index, and used the int of the index to get the next element of the original list. That's almost like...sampling? Mark Guzdial


def duplicateList(list):
  list = []
  x = 0
  for entry in list:
    original =int (entry [x, x+1])
    print original ", " original ", "

Prof. G: I think this is along the lines of what you were thinking, at least it was before we shortened it. We skipped the step of actaully duplicating by having the original printed twice. I don't know if this will work, but please let me know what you think!

Brittany Selden and Erin McLean
Close, Brittany and Erin. You want to actually BUILD a new list (not just print it), so you don't want to set the input "list" to []. Here's a working version that I made by modifying yours. Mark Guzdial
def duplicateList(list):
  newlist = []
  originalPosition = 0
  for count in range(0,len(list)*2): # New list will be twice as long
    newlist = newlist + [list[int(originalPosition)]]
    originalPosition = originalPosition + 0.5
  return newlist



You should know this for the final! Think about doing this with map, reduce, and filter. Mark Guzdial



Links to this Page