Help Me Understand How To Properly Do These Problems
Write the function firstLetter that takes a string s and returns the first letter of s as a string. Include an assertion that the length of the string is at least 1. Call the function on some test data. Sample input s= 'garden' Expected output ' g ' Note that when you print a string, it strips off the quotes. Exercise 2 Write the function secondLetter that takes a string s and returns the second letter of s as a string. Include an assertion that the length of the string is at least 2. Call the function on some test data. Sample input s= 'garden' Expected output ' a ' Write the function lastLetter that takes a string s and returns the last letter of s as a string. Include an assertion that the length of the string is at least 1. Call the function on some test data. Constraint: you should only use nonnegative indices to solve this problem. Sample input s= 'garden' Expected output 'n' Exercise 4 Write the function first0rSecond that takes a string s and returns the first letter of s as a string, unless this first letter is a vowel, in which case it returns the second letter of s as a string. Include an assertion that the length of the string is at least 2. Call the function on some test data. Sample input s= 'garden' Expected output ' g ' Sample input 8= 'orange' Expected output ' r ' Let's say that y is not a vowel. y can be either a vowel or a consonant: it is a vowel when it forms a diphthong (e.g., y is a vowel in say) but a consonant otherwise (e.g., y is a consonant in 'beyond'). Deciding whether it is used as a diphthong is beyond the scope of this lab. Sample input 8= 'yard' Expected output 'a' Practice problem 1 In class, we flipped a coin using random.random (see Canvas for the code: we prefer the last version). Write the function flip2 that takes no parameters and returns the bool True or False with equal probabilities, using the random. choice function in the random module, applied to a string. True represents a head, and False represents a tail. If a is a sequence (like a string), random.choice(s) returns a random element of the sequence. Possible output True (a bool, not a string) (must be random) Then write the function f11p3 that takes no parameters and returns the bool True or False with equal probabilities, using the random. randint function in the random module. If ab, random, randint (a,b) returns a random integer N where aNb Possible output False (a bool, not a string) (must be random) Practice problem 2 Write the function probabilityHead that takes an integer n, flips a coin n times, counts the number of heads, and returns the proportion of flips that are heads (a value between 0 and 1). For example, if 49 of 100 flips are heads, you would return .49. You can use my last flip function from lecture 5 or or one of the flip functions that you designed above. Sample input n=100 Possible output 49 Note that randomization is being used, so the output is not totally predictable (although trends can be observed). Compute the proportion for n=10,n=100,n=1000, and n=1000000. What do you observe as n increases? (Write as a text cell under the 4 experiments.) Since this is a random experiment, try it again and see if the trend is repeated. What would you expect to happen as n increases? (Write as a text coll under the 4 experiments.)