< Back

Lab 22: Sounds


Practice Problem 1

Write a function called maximum(a_list), which takes a list of integers as a parameter. Your function should print the maximum of the values in the list.

Example

>>> maximum([0, 1, 2])
2
>>> maximum([1, 1, 1, 1, 1, 1])
1
>>> maximum([2, 8, 6, 4])
8

Hint

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 do not need to use the accumulator pattern for this exercise. Instead, you need to use variable reassignment. For example, the following block of code will reassign the variable x to the maximum of x or y

def compute_max(x, y):
    if x < y:
        x = y

    print(x)

You should initialize a variable maximum_so_far to the first value inside of the list as your first step. To do that, use the index operator \([\mbox{ }]\): maximum_so_far = a_list[0]

Use the for element in a_list: loop to go through all of the value of a_list. Inside of this loop, compare element to maximum_so_far. If element is bigger than maximum_so_far, then change the value of maximum_so_far to element.


Practice Problem 2

Write a function called print_firsts(a_list), which takes as a parameter a list of string. Your function should print only the first character of every string in the list.

Example

>>> print_firsts(["alpha", "beta", "charlie", "delta", "echo", "foxtrot"])
a
b
c
d
e
f
>>> print_firsts(["INQ", "241"])
I
2
>>> print_firsts(["I", "<3", "yik", "yak"])
I
<
y
y
>>>

Hint

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

Just like Lists are similar to string, strings are similar to lists. We can use the index operator \([\mbox{ }]\) On strings as well. The index of the first character in a string is \(0\).


Adding Sound

In a file called adding_sounds.py write a function called combine_sounds(sound_1, sound_2). This function should create a new sound, which is the result of appending sound_1 to the end of sound_2.

Example

Before

Alpha

Bravo

After

Alpha Bravo

Hint

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.

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.

You can get the number of sample in a list, by using the len function. This tells you how many samples are in a list. You can add the number of two different lists together to get how long the resulting sound file should be.

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.

Copying the samples from the first sound is easy, you copy directly from one index in the first list to the same index in the new list. Copying from the second list is a little more tricky. If you are looking to copy sample \(x\) from the second list, you want to place it at \(x + num\_samples\_sound\_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.