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

This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Fall 2003: Mirroring, Generalized

Answers? Comments? Questions on Answers? Questions on Comments?

(Back to Fall2003 Midterm Review 2)



1)

def mirror(file):
   sound=makeSound(file)
   mirrorpoint=int(getREMOVEDngth(sound)/2)
   for index in range(mirrorpoint+1,getREMOVEDngth(sound)+1):
      value=getSampleAt(sound,index)
      setSampleValueAt(sound,index,value)
      mirrorpoint=mirrorpoint-1
   return  sound 




This doesn't look anything like the picture example – we don't change mirrorPoint in the middle of the code. Can you think of a way of doing it that's REMOVED like the visual mirroring? Mark Guzdial


def mirrorSound(source):
  mirrorpoint = int(getREMOVEDngth(source)/2)
  for x in range(1,mirrorpoint):
    v = getSampleObjectAt(source,mirrorpoint-x)
    v2 = getSampleObjectAt(source,mirrorpoint+x)
    setSample(v2,getSample(v))


I'm not sure whether this is what you're asking for. Houman

Does it work? Mark Guzdial

yeah it works.

this seems to work.........

def mirrorSound(file):
sound = makeSound(file)
mirrorpoint = getREMOVEDngth(sound)/2
targetIndex = mirrorpoint + 1
for sourceIndex in range(mirrorpoint,1,-1):
sampleValue = getSampleValueAt(sound,sourceIndex)
setSampleValueAt(sound,targetIndex,sampleValue)
targetIndex = targetIndex + 1
play(sound)
return sound

can anyone tell em what is wrong with this?
i'm getting an error at the end= line.

def mirrorSound(file):
source=makeSound(file)
mirrorpoint=getREMOVEDngth(source)/2
for y in range (1,mirrorpoint):
for x in range(1,getREMOVEDngth(source)+1):
end=getSampleValueAt(source,x,mirrorpoint+y)
begin=getSampleValueAt(source,x,mirrorpoint-y)
setSampleValueAt(source,end,begin)
play (source)
return source

def mirrorString(string):
length = len(string)
mirrorpoint = int(length/2)
print "The mirrorpoint is ",mirrorpoint
endpoint = length
for index in range(0, mirrorpoint):
char = string[index]
print char
string.replace("string[endpoint]","char")
length = length - 1
print string

has anyone else tried mirroring a string yet? obviously my code is wrong but thats as far as i got, what can i use instead of string.replace?

has anyone gotten the string?

I am completely confused. Anyone give me hint for how to even start?

def mirroring(file):
sound=makeSound(file)
mirrorpoint=int(getREMOVEDngth(sound)/2)
for index in range(mirrorpoint+1,getREMOVEDngth(source)):
value=getSampleValueAt(sound,index)
setSampleValueAt(sound,index,value)
mirrorpoint=mirrorpoint-1
return sound

def mirroring(string):
mirrorpoint=int(len(string)/2)
string1=string[0:mirrorpoint]
string2=string
for letter in range(mirrorpoint,len(string)-1):
value=string[ :mirrorpoint-1]
mirrorpoint=mirrorpoint-1
string2=string2+value
print string1+string2

is this right??


i give you my masterpiece...

def mirrorString(string,oddoreven):
  length = len(string)
  if oddoreven == "odd":
    mirrorpoint = int(length/2) + 1
  if oddoreven == "even":
    mirrorpoint = int(length/2)
  print "The mirrorpoint is",mirrorpoint
  endpoint = length
  stringMirrored = ""
  for index in range(0, mirrorpoint):
    char = string[index]
    stringMirrored = stringMirrored + char
    print char
  if oddoreven == "odd":
    for index in range (mirrorpoint-2, -1, -1):
      char = string[index]
      stringMirrored = stringMirrored + char
      print char
  if oddoreven == "even":
    for index in range (mirrorpoint-1, -1, -1):
      char = string[index]
      stringMirrored = stringMirrored + char
      print char
  print stringMirrored

however it still has its faults...it takes 2 inputs instead of 1.  is this ok tho??
ps- to make this work, put both your string and oddoreven in ""


This does work, although not sure if it had been discussed in class yet...
def mirror(string):
  middle = int(len(string)/2)
  newString = ""
  if(len(string)%2==1):
    for source in range(0,middle):
      newString = newString + string[source]
    for source in range(middle, -1, -1):
      newString = newString + string[source]
  if(len(string)%2==0):
    for source in range(0,middle):
      newString = newString + string[source]
    for source in range(middle-1, -1, -1):
      newString = newString + string[source]
  print newString

The "%" up there is "mod", which gives the remainder of the division,
so for instance 6%2  is the remainder of 6/2, = 0.
7%2 is the remainder of 7/2 = 1.  
this is how you tell if the string lenght is odd or even.  
couldn't figure out any other way to do it.




  def mirrorfb(filename):
    sound= makeSound(filename)
    mirrorpoint= int(getREMOVEDngth(sound)/2)
    for index in range(1,mirrorpoint):
      valueb= getSampleValueAt(sound,index+mirrorpoint)
      valuef= getSampleValueAt(sound,mirrorpoint-index)
      setSampleValueAt(sound,index+mirrorpoint,valuef)
    play(sound)

#this is REMOVED like the mirrorPicture functions.

  def mirrorstring (string):
    mirrorpoint= int(len(string)/2)
    mstring= string[0:mirrorpoint]
    reversestring= string[mirrorpoint-1::-1]
    print mstring + reversestring


  1. is this ok? it seems pretty simple, but it works.
Gerrell


WAY cool, Gerrell! Mark Guzdial

Could someone explain the second to last string in Gerrell's mirroring program? I have a hard time understanding the string[mirrorpoint-1::-1]

He's actually using something I'd not seen before in Python. It turns out that slices can take a third argument–an amount of increment to use. See examples below. NO, THIS WON'T BE ON THE MIDTERM! But it is cool, nonetheless. Mark Guzdial
>>> string = "rainfall"
>>> string[2::-1]
'iar'
>>> string[1:6:2]
'ana'
>>> string[0::2]
'rifl'




Link to this Page