module4

pdf

School

George Washington University *

*We aren’t endorsed by this school

Course

1012

Subject

Statistics

Date

Jan 9, 2024

Type

pdf

Pages

8

Uploaded by ProfessorKnowledgeWaterBuffalo330

Report
Module 4 Exercise Answers 4.2 Exercise Answer: iteration k comment 1 k is initially set to 1 1 while statement ‘k < 6’ is satisfied as true print ‘1’ 2 k is incremented by 1 in k + 1 = 2 2 while statement ‘k < 6’ is satisfied as true print ‘2’ 3 k is incremented by 1 in k + 1 = 3 3 while statement ‘k < 6’ is satisfied as true print ‘3’ 4 k is incremented by 1 in k + 1 = 4 4 while statement ‘k < 6’ is satisfied as true print ‘4’ 5 k is incremented by 1 in k + 1 = 5 5 while statement ‘k < 6’ is satisfied as true print ‘5’ 6 k is incremented by 1 in k + 1 = 6 6 while statement ‘k < 6’ is not satisfied and now is False Execution is taken out of loop Final Output: 1 2 3 4 5 4.3 Exercise Answer: iteration k comment 7 k is initially set to 7 1 while statement ‘k < 6’ is not satisfied and now is False Execution is taken out of loop Final Output: 4.4 Exercise Answer: iteration k comment 1 k is initially set to 1
1 while statement ‘k < 6’ is satisfied as true print ‘1’ 2 while statement ‘k < 6’ is satisfied as true print ‘1’ 3 while statement ‘k < 6’ is satisfied as true print ‘1’ 4 while statement ‘k < 6’ is satisfied as true print ‘1’ 5 while statement ‘k < 6’ is satisfied as true print ‘1’ 6 while statement ‘k < 6’ is satisfied as true print ‘1’ Final Output: 1 1 1 1 1 1 4.7 Exercise Answer: iteration x s k comments 0.5 0 0 x is initially set to 0.5, s is initially set to 0, k is initially set to 0 1 while statement ‘s <= 2’ is satisfied as true 0.5 x or 0.5 is added to s 1 k = k + 1 = 0 + 1 = 1 2 while statement ‘s <= 2’ is satisfied as true 1 x or 0.5 is added to s 2 k = k + 1 = 1 + 1 = 2 3 while statement ‘s <= 2’ is satisfied as true 1.5 x or 0.5 is added to s 3 k = k + 1 = 2 + 1 = 3 4 while statement ‘s <= 2’ is satisfied as true 2 x or 0.5 is added to s 4 k = k + 1 = 3 + 1 = 4 5 while statement ‘s <= 2’ is satisfied as true 2.5 x or 0.5 is added to s 5 k = k + 1 = 4 + 1 = 5 6 while statement ‘s <= 2’ is not satisfied and now is False Execution is taken out of loop
print ‘s = 2.5 k = 5’ Final Output: s = 2.5 k = 5 4.8 Exercise Answer: iteration x s k comments 1 0 0 x is initially set to 1, s is initially set to 0, k is initially set to 0 1 while statement ‘s < 0.9’ is satisfied as true 0.5 x = x/2 = ½ = 0.5 0.5 s = s + x = 0 + 0.5 = 0.5 1 k = k + 1 = 0 + 1 = 1 2 while statement ‘s < 0.9’ is satisfied as true 0.25 x = x/2 = 0.5/2 = 0.25 0.75 s = s + x = 0.5 + 0.25 = 0.75 2 k = k + 1 = 1 + 1 = 2 3 while statement ‘s < 0.9’ is satisfied as true 0.125 x = x/2 = 0.25/2 = 0.125 0.875 s = s + x = 0.75 + 0.125 = 0.875 3 k = k + 1 = 2 + 1 = 3 4 while statement ‘s < 0.9’ is satisfied as true 0.0625 x = x/2 = 0.125/2 = 0.0625 0.9375 s = s + x = 0.875 + 0.0625 = 0.9375 4 k = k + 1 = 4 + 1 = 5 while statement ‘s < 0.9’ is not satisfied and now is False Execution is taken out of loop print ‘4’ Final Output: 4 4.11 Exercise Answer: Iteration of while loop k s[k] comment 4 k is initially set to len(s) -1 = len(hello) -1 = 5-1 = 4 1 while statement ‘k >= 0’ is satisfied as True o s[k] = s[4] = o print ‘o’ 3 k changes to k – 1 = 4-1 = 3 2 while statement ‘k >= 0’ is satisfied as True l s[k] = s[3] = l print ‘l’ 2 k changes to k – 1 = 3-1 = 2 3 while statement ‘k >= 0’ is satisfied as True
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
l s[k] = s[2] = l print ‘l’ 1 k changes to k – 1 = 2-1 = 1 4 while statement ‘k >= 0’ is satisfied as True e s[k] = s[1] = e print ‘e’ 0 k changes to k – 1 = 1-1 = 0 5 while statement ‘k >= 0’ is satisfied as True h s[k] = s[0] = h print ‘h’ -1 k changes to k – 1 = 0-1 = -1 6 while statement ‘k >= 0’ is not satisfied and now is False Execution is taken out of loop Iteration of for loop k s[k] comment 1 4 k is initially set to len(s) -1 = len(hello) -1 = 5-1 = 4 o s[k] = s[4] = o print ‘o’ 2 3 k changes to 3 l s[k] = s[3] = l print ‘l’ 3 2 k changes to 2 l s[k] = s[2] = l print ‘l’ 4 1 k changes to 1 e s[k] = s[1] = e print ‘e’ 5 0 k changes to 0 h s[k] = s[0] = h print ‘h’ Final Output: o l l e h o l l e h
4.12 Exercise Answer: Iteration of while loop k A list A[k] len(A[k]) comments 0 k is initially set to 0 ['hello', 'hey there', 'howdy', 'huzzah', 'hi', 'greetings'] hello A list is set to B list A[k] = A[0] = hello 5 len(A[k] = len(hello) = 5 1 while statement A[k].startswith('h') and (len(A[k]) > 4) and (k < len(A)) is satisfied as True print ‘hello’ 1 k = k + 1 = 0 + 1 = 1 hey there A[k] = A[1] = hey there 9 len(A[k] = len(hey there) = 9 2 while statement A[k].startswith('h') and (len(A[k]) > 4) and (k < len(A)) is satisfied as True print ‘hey there’ 2 k = k + 1 = 1 + 1 = 2 howdy A[k] = A[2] = howdy 5 len(A[k] = len(howdy) = 5 3 while statement A[k].startswith('h') and (len(A[k]) > 4) and (k < len(A)) is satisfied as True print ‘howdy’ 3 k = k + 1 = 2 + 1 = 3 huzzah A[k] = A[3] = huzzah 6 len(A[k] = len(huzzah) = 6 4 while statement A[k].startswith('h') and (len(A[k]) > 4) and (k < len(A)) is satisfied as True print ‘huzzah’ 4 k = k + 1 = 3 + 1 = 4 hi A[k] = A[4] = hi 2 len(A[k] = len(hi) = 2 5 while statement A[k].startswith('h') and (len(A[k]) > 4) and (k < len(A)) is not satisfied and now is False Execution is taken out of the loop
Final Output: hello hey there howdy huzzah 4.15 Exercise Answer: iteration k (k+1)*(k+1) comments 1 1 k is initially set to 1 4 (k+1)*(k+1) = (1+1)*(1+1) = (2)*(2) = 4 (k+1)*(k+1) does not satisfy if statement ‘(k+1)*(k+1) > 50, no command 2 k is incremented to 2 9 (k+1)*(k+1) = (2+1)*(2+1) = (3)*(3) = 9 (k+1)*(k+1) does not satisfy if statement ‘(k+1)*(k+1) > 50, no command 3 k is incremented to 3 16 (k+1)*(k+1) = (3+1)*(3+1) = (4)*(4) = 16 (k+1)*(k+1) does not satisfy if statement ‘(k+1)*(k+1) > 50, no command 4 k is incremented to 4 25 (k+1)*(k+1) = (4+1)*(4+1) = (5)*(5) = 25 (k+1)*(k+1) does not satisfy if statement ‘(k+1)*(k+1) > 50, no command 5 k is incremented to 5 36 (k+1)*(k+1) = (5+1)*(5+1) = (6)*(6) = 36 (k+1)*(k+1) does not satisfy if statement ‘(k+1)*(k+1) > 50, no command 6 k is incremented to 6 49 (k+1)*(k+1) = (6+1)*(6+1) = (7)*(7) = 49 (k+1)*(k+1) does not satisfy if statement ‘(k+1)*(k+1) > 50, no command 7 k is incremented to 7 64 (k+1)*(k+1) = (7+1)*(7+1) = (8)*(8) = 64 (k+1)*(k+1) does satisfy if statement ‘(k+1)*(k+1) > 50, print ‘7’ and break out of loop Final Output: 7 4.16 Exercise Answer: If the code in the while loop were to have a k = k – 1 line instead of a k = k + 1 line, every iteration would make k decrease by one. The while statement ‘while True’ is testing when the if statement ‘if k*k > 50’ is satisfied so that the code can run the break command. When k increases by one the last value returned is an 8 and we then print that value minus one since the value returned is the first value that makes k*k
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
over 50. When k decreases by one the returned value is -8 because k*k using k as -8 or 8 is the same, but when we print that value minus one we print -9 which is not what we are actually looking for. 4.23 Exercise Answer: The more accurate estimate I got with 1000000 trials was 8.003236. In general, the estimate is approximately 8, and the reason for this is that the chance of getting three heads in three coin flips is 1/8. The num_trials is the number of successful trials that had three heads, and the total is the number of trials total. So, for example, if we have 1000 num_trials/successes we should have 8000 as our total/total trials and our probability is again successes/total trials or num_trials/total which is 1000/8000 or 1/8. Estimate is just the inverse of the probability, so our estimates are approaching the inverse of the probability which is 8. 4.27 Exercise Answer: Line is a string type of variable. 4.28 Exercise Answer: Line is a list type of variable. 4.37 Exercise Answer: The things I changed in my file was the colors available, the number of walks, and the range of random starting points. Now I changed my available colors to blue, green, cyan, purple, and yellow. My number of walks is 15 and the range of the starting point coordinates x and y is from -8 to 8.
4.38 Exercise Answer: The starting k value is 0 but that would not allow us to descend from 10 to 1 so we should have the starting k value be 10. 4.39 Exercise Answer: To get the correct result we are looking for n should start at 1 so that 2*n when m starts at 1 is equal to 2*1 = 2. To make sure both m and n don’t go over 10 and 20 respectively we should say and not or in our if statement. 4.40 Exercise Answer: The else statement is not necessary and the x = x + 1 statement should be placed after the if statement.