Write a function called average_list(a_list)
,
which takes a list of integers as a parameter. Your function
should print the average of the input grades.
>>> average_list([0, 1, 2]) 1.0 >>> average_list([1, 1, 1, 1, 1, 1]) 1.0 >>> average_list([2, 4, 6, 8]) 5.0
Lists are similar to Strings, as far as Python is concerned.
We can use a for loop to iterate over the items of our
list. for item in a_list
You are going to start by using the accumulator pattern for this example. To that end, you need to create an accumulator variable above the for loop for your accumulation. This variable should start at 0.
After your for loop ends, you should divide the accumulator sum by the number of items in the list.
Write a function called print_evens(a_list)
, which
takes as a parameter a list of integers. Your function should print
only the even values in the list.
>>> print_evens([0, 1, 2, 3, 4, 5]) 0 2 4 >>> print_evens([2, 1, 2, 3, 2, 4, 2]) 2 2 2 4 2 >>> print_evens([1, 3, 4, 7, 9, 11, 13]) >>>
Lists are similar to Strings, as far as Python is concerned.
We can use a for loop to iterate over the items of our
list. for item in a_list
You can check to see if a number \(x\) is even if x % 2 == 0
In a file called reverse_sound.py write a function
called reverse(a_sound)
. This function should
modify the passed in sound to play backwards.
Before
Nato Phonetic Alphabet Normal
After
Nato Phonetic Alphabet
Reversed
Sounds are represented as a collection of samples. This collection of samples can be thought of as a list, which you can leverage in order to reverse the order of the samples.
You can get the number of samples in a file by using
the getNumSamples(a_sound)
function. This returns
a floating point number, so you may need to convert it to an
integer before you use it. You can use the int
function for that.
The getSamples(a_sound)
function gives you a list
of the samples from the sound. You need to store this in a
variable, as it will be very useful later.
To get the value of a specific sample of the sound, you can use
the getSampleValue(sample)
function. This
is very similar to getRed(pixel)
for images.
In this case, your sound has some duration \(d\). for the \(i^{th}\) sample in your reversed sound, you want to get the sample at index \(d - i - 1\).
To set the sample value for your sound, you use
the setSampleValue(sample, value)
function. This
is like the setRedValue(pixel, color)
function for images.