Chapter 3 Test 2019
Write type signatures for each function.
On a piece of paper, a person has written what they claim are the results of flipping a coin 120 consecutive times, writing heads as 1 and tails as 0. The numbers are written in rows of 10.
-
(
q1
) A human transcribed the numbers into a computer, but in order to make it easier to type quickly, they type ‘x’ for 1 and ‘c’ for 0. They still break the data every line. Translate the data into numbers.[[1,0,1,0,1,0,1,1,1,0], [1,0,1,1,0,1,1,1,0,0]] == q1 ["xcxcxcxxxc", "xcxxcxxxcc"]
-
(
q2
) The nested list format is good for writing, but not necessarily the best for analyzing. Write a program to “flatten” the nested input lists into one great big long list.[1,0,1,0,1,0,1,1,1] == q2 [[1,0,1,0], [1,0,1,1,1]]
-
(
q3
) Now you start to suspect that the person may not actually have flipped a coin 120 times, so you want to find out how many heads and tails they actually got. Take in a flattened list (likeq2
outputs) and output a tuple containing the number of 0s and number of 1s in the data.(3, 6) == q3 [1,0,1,0,1,0,1,1,1]
-
(
q4
) One way in which random data and human-faked data are different is that humans tend to make shorter runs of the same number than actually occur in truely random data. You are going to write a program to find the length of the first run of the same number.5 == q4 [1,1,1,1,1,0,1,0,0,0,0,0,0,0]
-
(
q5
) Now write a function that outputs a list of all of the run-lengths.[5,1,1,7] == q5 [1,1,1,1,1,0,1,0,0,0,0,0,0,0]
-
(
q6
) Bonus Given the output fromq5
, produce a list of tuples (run length, number of runs of that length). Order does not matter.[(1,2),(5,1),(7,1)] == q6 $ q5 [1,1,1,1,1,0,1,0,0,0,0,0,0,0]