LabX1: Supplementary Practice Problems¶
These are similar to programming problems you may encounter in the mid-terms. They are not graded but we will review them in lab sessions.
1. (10 points) The logistic map is defined by the following simple function
For \(x_0 = 0.1\) and \(r = 4.0\), store the first 10 values of the iterated logistic map \(x_{i+1} = rx_i(1-x_i)\) in a list. The first value in the list should be \(x_0\).
2. (10 points) Write a function to find the greatest common divisor (GCD) of 2 numbers using Euclid’s algorithm.:
Find the GCD of 5797 and 190978.
Now write a function to find the GCD given a collection of numbers.
Find the GCD of (24, 48, 60, 120, 8).
3. (10 points) Find the least squares linear solution to the following data
y = [1,2,3,4]
x1 = [1,2,3,4]
x2 = [2,3,4,5]
That is, find the “best” intercept and slope for the variables x1
and x2
.
4. (10 points) Read the mtcars
data frame from R to a pandas
DataFrame. Find the mean wt
and mpg
for all cars grouped by the
number of gear
s.
5. (10 points) Read the iris
data frame from R to a pandas
DataFrame. Make a seaborn
plot showing a linear regression of
Petal.Length
(y) against Sepal.Length
(x). Make a separate
regression line for each Species
.
6. (10 points) Write a function that can flatten a nested list of arbitrary depth. Check that
flatten([1,[2,3],[4,[5,[6,7],8],9],10,[11,12]])
returns
[1,2,3,4,5,6,7,8,9,10,11,12]
For simplicity, assume that the only data structure you will encounter is a list. You can check if an item is a list by using
isinstance(item, list)
7. (10 points) Create the following table
array([[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[ 1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0],
[ 1, 4, 6, 4, 1, 0, 0, 0, 0, 0, 0],
[ 1, 5, 10, 10, 5, 1, 0, 0, 0, 0, 0],
[ 1, 6, 15, 20, 15, 6, 1, 0, 0, 0, 0],
[ 1, 7, 21, 35, 35, 21, 7, 1, 0, 0, 0],
[ 1, 8, 28, 56, 70, 56, 28, 8, 1, 0, 0],
[ 1, 9, 36, 84, 126, 126, 84, 36, 9, 1, 0],
[ 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]])
Start with the first row
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
and build the subsequent rows using a simple rule that only depends on the previous row.
8. (10 points) Read the following data sets into DataFrames.
- url1 = “https://raw.github.com/vincentarelbundock/Rdatasets/master/csv/DAAG/hills.csv”
- url2 = “https://raw.github.com/vincentarelbundock/Rdatasets/master/csv/DAAG/hills2000.csv”
Create a new DataFraem only containing the names present in both
DataFrames. Drop the timef
column and have a single column for
dist
, climb
and time
that shows the average value of the
two DataFrames. The final DtataFrame will thus have 4 columns (name,
dist, climb, time).