You are on page 1of 5

AYPC_4 1 of 5

Final Exam
Problem #1
1. The number of collections of k objects without repetition from a set of n objects is given by
the formula,

which is read “n choose k”.

Write a recursive function in Python to calculate “n choose k”.

(two lines plus doc.string)

Notice that ‘n choose k’ is always an integer, even though it results from a division. Which
means that the function should always return an integer, not a float.

To test the function show that for a poker hand of 5 cards choosen without repetition from a
deck of 52 cards there are 2 598 960 combinations.

2. Tartaglia’s triangle (after Nicolò Fontana Tartaglia who published it in 1556) is a triangular
array of “n choose k”, illustrated by the figure below:

There is more than one way to generate the coefficients of the Tartaglia’s triangle, but perhaps
the simplest in Python is to create a nested list in which n is the row (starting at 0), and k is
column (also starting at 0). We see that in the array above, the fifth row (n = 4), has 5 elements,
k from 0 to 4.

Write a Python function to make a nested list that contains the coefficients of Tartaglia’s
triangle.

(two lines plus doc.string)

Print the list using the imported method pprint.

Test the function with n = 7. The result should be:

[[1],
[1, 1],
AYPC_4 2 of 5

[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1]]

3. From the coefficients in a Tartaglia triangle, create a dictionary in which the key is the row
number, and the value is a list of the coefficients corresponding to that row:
{row: [coefficients of row]}

(two lines plus doc.string)

4. For a given set of n objects the sum of all possible collections of k objects taken is equal to
the number of permutations. For example, if from a set of 3 objects we choose 0, 1, 2 and
3, the sum of these collections equals 2**3, the number of possible permutations.

Using the dictionary built in (3) above, show that for any row n of a Tartaglia triangle the
sum of the coefficients is equal to 2**n.

(Four lines of code)

Test your code with a triangle of 15 rows. The results should be the same as the table below:

Row_n Values 2^n Values=2^n


0 1 1 True
1 2 2 True
2 4 4 True
3 8 8 True
4 16 16 True
5 32 32 True
6 64 64 True
7 128 128 True
8 256 256 True
9 512 512 True
10 1024 1024 True
11 2048 2048 True
12 4096 4096 True
13 8192 8192 True
14 16384 16384 True

Problem #2
AMPLITUDE MODULATION
To transmit information over a distance without a cable connection, what we call “wireless
transmission”, engineers have developed a techique called modulation. There are various types
of modulation but the simplest of all is amplitude modulation (AM). An electromagnetic wave
which has the information (or sound) is superimposed on another electromagnetic wave of
fixed frequency called the carrier signal. The “sound” wave changes, or modulates, the
amplitude of the carrier, while its frequency remains unchanged.

AYPC_4 3 of 5

The carrier is a higher-frequency wave (around 1 MHz for broadcast radio), like this simple
sinusoidal wave:

The message signal, which contains the sound, could be something like in the figure below.

The amplitude modulation is done by multiplying the carrier wave with the message, let’s call it
m(t). If the carrier wave is Acos(2πfct), then the AM-modulated carrier is given by

(1 + m(t))Acos(2πfct). A 1 is added to the message to raise it to the range [0, 2] so that the
carrier just vanishes at the smallest message values. The result is shown in the next figure.

The original message is the envelope of the modulated carrier (in red). Notice that it is not
changed in any way. The carrier wave, however, has its amplitude changed, but not its
frequency. The radio receiver is tuned to the frequency of the carrier, fc, detects it and extracts
the message.

A COMPUTER MODEL
We can model amplitude modulation by plotting three graphs on the same plane. When
studying and testing analog modulation, it is convenient to use a sinusoid as the message
signal. The independent variable is time, t, which in this exercise will be represented by x.

Using the guide provided by the Python program GuideProblem2.py (download it), you are to
draw three graphs, properly identified and labeled. In addition to the problem guide, you
also need to download to the working directory the module PlotFunctions.py that contain the
functions that you will use to plot the graphs.

The graphs are:

(1) The upper band of the ‘message’ wave,

1 + modulation_index*sin(2πx)

The modulation_index is a parameter in the program with a value of 1.

(2) The lower band of the ‘message’ wave,

AYPC_4 4 of 5

– (1 + modulation_index*sin(2πx))
This is simply the negative value of the expression in (1).

(3) The carrier wave for the exercise is cos(16πx). Plot the AM-modulated carrier, which
is given by the expression:

(1+ modulation_index*sin(2πx))*cos(16πx)

The three graphs should be plotted in the interval -7π/8 to 7π/8.

The complete plot must include, the axes with labels, marks on the axes with labels, the 3
graphs in different colors (1 and 2 should have the same color), a legend on the upper left-hand
side, a label indicating the value of the modulation_index, and a title for the whole plot. Follow
the instructions and comments in the guide, GuideProblem2.py.

ANALYSIS OF THE RESULTS


Use the Python program that you have assembled
to explore the effect of the modulation index on
amplitude modulation for radio transmission. You
should get plots similar to those in the figure on the
right.

The modulation index is the ratio of the amplitude


of the message signal to the amplitude of the
carrier. At a modulation index of 1 modulation is at
100%, the side bands touch indicating that the
carrier vanishes at this points, and the message
wave has its lowest values, but it is not distorted.
Notice that the amplitude of the modulated carrier
is at 2.0, the combined amplitude of the carrier and
the message signal.

For values of the modulation index less than 1, for


example 0.5, the two sidebands do not touch, the
amplitude of the message signal is less than the
amplitude of the carrier, and the message signal is
transmited without distortion.

When the index is greater than 1, the two


sidebands overlap and at low values of the
message signal there is distortion. This is a case of
overmodulation and is undesirable in radio transmission.

Problem #3
With the same data file used in Problem#2 Week#14, students.txt, calculate for each student
the Grade Point Average (GPA), and store it in a dictionary together with the list of courses and
grades.

The GPA for a student is a weighted average of the grades obtained in a particular period. For
example, student Andrés Pilo. has obtained these grades in the first semester:

EXOE 3, 3.1

PREM 4, 4.2

AYPC_4 5 of 5

AGEO 4, 2.8

IINE 2, 4.5

ALI 3, 3.5

The total number of credits taken was (3 + 4 + 4 + 2 + 3) = 16

The grade points (or credit points) earned was: (3x3.1 + 4x4.2 + 4x2.8 + 2x4.5 + 3x3.5) = 56.8

The GPA is 56.8/16 = 3.55

Write a function in Python that will do the following tasks:

1. Read the file students.txt

2. For each student calculate the GPA with the procedure described in the previous
parragraph. Round the GPA to two digits after the decimal point.

3. Create a dictionary in which the key is the student’s full name (apellidos + nombres), and
the values are the GPA and a list of courses. The list has the course identifier, the number
of credits and the grade obtained. An example of an entry in the dictionary follows:

{‘López Vesga Miguel Ángel': (4.09,


[('EXOE', ' 3', ' 3.2'),
('PREM', ' 4', ' 3.8'),
('AGEO', ' 4', ' 4.6'),
('IINS', ' 2', ' 5.0’)])}
4. Print the dictionary with pprint to make it easier to read.

This problem is somewhat similar to Problem#2 of Week 14. A suggestion is to use the function
developed in Problem#2 of Week 14 student_dict(file_name), and modify it to calculate the
GPA and the new dictionary.

You might also like