Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSE 1224:  Final Exam

Due by Wednesday, December 13 at 11:59 pm. You should upload one Python file with all of your solutions to Carmen.  No exams will accepted after this date.

Use only standard Python in your answers. Do not use NumPy, SciPy etc.

Please don’t include any print() statements in the file you upload;  only  in- clude your functions.

You may work on the midterm with one other person,  if you wish.   If you complete the midterm with another person, please note this person’s name in a comment at the top of your solutions.

Each question is worth 20 points.

1) Write a function called plus or box(n) that takes a positive integer greater than 2 as an argument. The function should return ann by n matrix of 0s and 1s.

If n is odd, the matrix should contain a plus made up 1s centered in the matrix, 0s elsewhere, and a 0 in in center of the matrix.

For example, plus or box(5) should return

[[0,  0,  1,  0,  0],

[0,  0,  1,  0,  0],

[1,  1,  0,  1,  1],

[0,  0,  1,  0,  0],

[0,  0,  1,  0,  0]]

If n is even, the matrix should contain a square made up 1s around the edges of the matrix but with 0s in the corners of the matrix and everywhere else.

For example, plus or box(6) should return

[[0,  1,  1,  1,  1,  0],

[1,  0,  0,  0,  0,  1],

[1,  0,  0,  0,  0,  1],

[1,  0,  0,  0,  0,  1],

[1,  0,  0,  0,  0,  1],

[0,  1,  1,  1,  1,  0]]

2) The winsorized mean replaces the smallest and largest numbers in a list with the numbers closest to them, and then takes the mean of the new list.  For example, if the list is originally [1, 5, 7, 9, 9, 10, 34], the new list becomes [5, 5, 7, 9, 9, 10, 10] and its winsorized mean is 7.7.

Write a function called winsorized(li) that takes a list of integers as an ar- gument and returns the winsorized mean of the integers.

You can assume that li is sorted from least to greatest and will contain at least 4 integers.

The integers in li are not necessarily distinct.

3) Given two string of equal length made up of only 0s and 1s, the Hamming distance is the number of places in which these two strings differ.

For example:

- The Hamming distance of ”1010” and ”1110” is 1.

- The Hamming distance of ”111” and ”000” is 3.

- The Hamming distance of ”11100” and ”00010” is 4.

- The Hamming distance of ”111” and ”111” is 0.

Write a function called hamming(s1, s2) that takes two strings of equal length made up of 0s and 1s and returns their hamming distance.

The length of the strings will be at least 1.

4) Write a function called nested dictionaries(di) that takes a dictionary asaar- gument. The keys of di will be integers, and the values of di will be dictionaries.

The keys of the sub-dictionaries will be integers, and the values of the sub- dictionaries will be lists.

nested dictionaries(di) should return the length of the longest list in the sub- dictionaries.

For example, if di =

{1  :  {2  :  [1,  4,  3],

4  :  [3],

1  :  [5,  1]},

5  :  {6  :  [4,  6],

1  :  [4,  6,  1,  11]},

7  :  {5  :  [4,  3]}

}

The function should return 4.

5) Write a function called area() that finds the area below the curve y = x2 , above the curve y = x3  and between x = 0 and x = 1.

Do not use calculus. Instead, use 10,000 random points.