Difference between revisions of "CSC111 Lab 5 Solutions 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Solution Programs for Lab #5= (will be released after the last lab section of the week) <!-- <br /> <source lang="python"> # lab5 solution programs (loops) # D. T...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 21:37, 25 February 2014 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 21:37, 25 February 2014 (EST)
 
----
 
----
 +
 +
<onlydft>
 +
 
=Solution Programs for Lab #5=
 
=Solution Programs for Lab #5=
(will be released after the last lab section of the week)
+
<!--
 
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
Line 43: Line 45:
  
 
"""
 
"""
# Nested For-Loops
+
for i in range(4):
 
  for j in range( i+1 ):
 
      print( "*", end="" )
 
  print()
 
 
 
print()
 
 
 
for i in range( 4-1, 0-1, -1):
 
  for j in range( i+1 ):
 
      print( "*", end="" )
 
  print()
 
 
 
print()
 
 
 
for i in range(4):
 
  print( ' ' * (3-i), end="" )
 
  for j in range( i+1 ):
 
      print( "*", end="" )
 
  print()
 
 
 
print()
 
 
 
for i in range(4-1, 0-1, -1):
 
  print( ' ' * (3-i), end="" )
 
  for j in range( i+1 ):
 
      print( "*", end="" )
 
  print()
 
 
 
print()
 
 
 
count = 1
 
for i in range(4-1, 0-1, -1):
 
  print( ' ' * (3-i), end="" )
 
  for j in range( i+1 ):
 
      print( count, end="" )
 
      count = count + 1
 
  print()
 
 
 
print()
 
 
 
 
# JES Program
 
# JES Program
 
# JES program to take a sound file and play with it
 
# JES program to take a sound file and play with it
Line 93: Line 55:
 
blockingPlay( sound )
 
blockingPlay( sound )
  
 +
# =======================================================
 +
# clear waveform after "Remember"
 
# get start index for clearing
 
# get start index for clearing
 
startIndex = 13393 # index just before "the force..."
 
startIndex = 13393 # index just before "the force..."
Line 106: Line 70:
 
#blockingPlay( sound )
 
#blockingPlay( sound )
  
 +
# =======================================================
 
# Copy "remember" to the end of itself in the waveform
 
# Copy "remember" to the end of itself in the waveform
 
rememberLength = startIndex  # that's the length of the word "remember"
 
rememberLength = startIndex  # that's the length of the word "remember"
Line 116: Line 81:
  
 
# get the original sound again  
 
# get the original sound again  
# and clear all but the word "remember"
 
 
sound = makeSound( file )
 
sound = makeSound( file )
 
blockingPlay( sound )
 
blockingPlay( sound )
 +
 +
# =======================================================
 +
# copy Always right after Remember
 +
startAlways = 30444  # index just before "Always"
 +
endRemember = 8968
 +
 +
# set j to the end of Remember
 +
j = endRemember
 +
for i in range( startAlways, noSamples ):
 +
  # copy from Always
 +
  value = getSampleValueAt( sound, i )
 +
  # into waveform after Remember
 +
  setSampleValueAt( sound, j, value )
 +
  # advance j by 1
 +
  j = j+1
 +
 +
# now j points to the end of the moved "Always"
 +
endRememberAlways = j
 +
 +
# clear from the end of RememberAlways to the end of the waveform
 +
for i in range( endRememberAlways, noSamples ):
 +
  setSampleValueAt( sound, i, 0 )
 +
 
 +
blockingPlay( sound )
 +
 +
"""
 +
# =======================================================
 +
# create an echo
  
 
startIndex = 13393    # index just before "the force..."
 
startIndex = 13393    # index just before "the force..."
Line 134: Line 126:
 
   sourceValue = getSampleValueAt( sound, i)
 
   sourceValue = getSampleValueAt( sound, i)
 
   destValue  = getSampleValueAt( sound, i+offset )
 
   destValue  = getSampleValueAt( sound, i+offset )
   setSampleValueAt( sound, i+offset, sourceValue + destValue )  
+
   setSampleValueAt( sound, i+offset, sourceValue//3 + destValue )  
 +
 
 +
blockingPlay( sound )
 +
"""
 +
 
 +
# =======================================================
 +
# reverse the whole waveform
 +
file = pickAFile()
 +
sound = makeSound( file )
 +
blockingPlay( sound )
 +
 
 +
# make j the index of the last sample in the sound
 +
j = getLength(sound)-1
  
 +
# make i go from beginning of sound to half way.
 +
for i in range( getLength( sound ) // 2 ):
 +
  # get the two samples at Index i and at Index j
 +
  valueAti = getSampleValueAt( sound, i )
 +
  valueAtj = getSampleValueAt( sound, j )
 +
 
 +
  # swap them
 +
  temp = valueAti
 +
  valueAti = valueAtj
 +
  valueAtj = temp
 +
 
 +
  # put the swapped versions back in sound
 +
  setSampleValueAt( sound, i, valueAti )
 +
  setSampleValueAt( sound, j, valueAtj )
 +
 
 +
  # move j to the left by 1.  The for loop moves i to the right
 +
  j = j-1
 +
 
 
blockingPlay( sound )
 
blockingPlay( sound )
 
</source>
 
</source>
-->
+
</onlydft>
 +
 
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 20:39, 9 January 2015

--D. Thiebaut (talk) 21:37, 25 February 2014 (EST)



...