CS 111
Spring 2018

Old version

This is the CS 111 site as it appeared on May 10, 2018.

Problem Set 6 FAQ

If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.

General questions

  1. Do we need to include docstrings in all of our functions?

    Yes.

Problem 3

  1. I’m stuck on the weave function. Do you have any suggestions?

    First, make sure that you are using an index-based loop. In the hints, we’ve given you a suggested template that includes the template of the necessary loop. It will allow you to consider one index i at a time, and to use i to access the character at position i in both s1 and s2.

    Because s1 and s2 may have different lengths, we have used the length of the shorter string when constructing the range of index values that the loop will consider. Then, after the loop has completed, you will need to take care of any “extra” characters in the longer of the two strings – concatenating them to the result.

    To figure out the logic for handling the “extra” characters, try considering concrete cases. For example, consider this call:

    weave('extra', 'LETTERS')
    

    The length of the shorter string ('extra') is 5, so your loop will be responsible for weaving together the first 5 characters of each string. At the conclusion of the loop, result will have a value of 'eLxEtTrTaE'. The code that you put after the loop will need to concatenate the extra characters from the second string ('RS') to give a final result of 'eLxEtTrTaERS'.

    Here are some questions that may help you to determine the logic:

    • How can I determine whether s1 or s2 has extra letters?

    • When one of the strings has extra letters, how can I access them? In the example above, the extra letters begin at position 5 of s2. How is that value of 5 related to other values that I already know or can compute?

    • How can I construct a general expression that gives me the extra characters for any case in which s1 has extra letters? for any case in which s2 has extra letters?

Problem 4

  1. How do I count the number of darts that hit the circle?

    In both of your functions, you will want to have a variable that accumulates the total number of hits. This means that whenever throw_dart() returns a True value, you will want to increment the value of your accumulator variable by 1. If you are unsure how to do this, check out the lecture notes. We have seen several examples of accumulator variables in lecture.

  2. What’s the difference between est_pi1 and est_pi2?

    In both functions, you will estimate pi by simulating the throwing of darts at a square and keeping track of the number of darts that hit a circle inscribed in the square.

    In est_pi1, you should take a integer n as a parameter, and your estimate should be obtained by throwing exactly n darts at the square.

    In est_pi2, you should take a number error as a parameter, and you should obtain an estimate of pi that is within error of Python’s built-in value of pi (the value given by math.pi). Because of randomness, you won’t know ahead of time how many darts you need to throw to get an estimate that is within the specified margin. Instead, you need to check after every dart throw if your current value of pi is close enough. If it is, you should stop and return how many throws it took to get there. Otherwise, you should continue looping so that you can keep throwing darts.

  3. We haven’t done much printing in the code that we’ve written thus far. Can you remind me of how to print multiple things on the same line?

    Remember that the print() function can take more than one parameter, separated by commas. For example, the code

    a = 10
    b = 20
    print('foo', a, 'bar', b, 'baz')
    

    will output

    foo 10 bar 20 baz
    
  4. What condition should I use for my loop in est_pi2?

    You should stop throwing darts if the absolute difference between your estimate of pi and Python’s built-in value is less than the parameter error.

    Note that you may find it easier to check for this condition inside the loop, and to exit the loop using either a break statement or a return statement. If you take this approach, you should use a loop condition that would cause the loop to continue looping forever. We discussed this type of loop in lecture.

  5. What should I initialize my estimate of pi to in est_pi2?

    If you take the approach outlined in question 4 above, you shouldn’t need to assign a value to the variable that you’re using for the estimate until after the first dart is thrown. If you take an alternative approach in which your estimate is used as part of the loop condition, you could use the area of the square (4) as your initial estimate.

  6. Can one of the pi_est functions call the other one?

    No, neither of these functions should call the other.

Problem 6

  1. How should I go about solving this problem?

    To make things easier, you may want to start by just focusing on writing the loop that generates and displays the first n Fibonacci numbers, where n is the value input by the user. You can always assume that n will be at least 2. Therefore, one way that you could start is by displaying the first 2 Fibonacci numbers, and then you could begin your loop.

    One approach to generating the numbers involves using copy to update the values of registers, The difference here is that you have 2 registers to update, since the next Fibonacci number is derived from the two previous Fibonacci numbers. Therefore, if you set aside 2 registers to represent the old (n-1) Fibonacci number and the older (n-2) Fibonacci number, you can always compute the current (n) Fibonacci number by just adding old to older. Then, you can use copy to make the old Fibonacci number the older one, and the current Fibonacci number the old one.

    At the beginning, you know that n is going to be at least 2. Therefore, you can set both old and older to 1 since the 0th and 1st Fibonacci numbers are simply 1. You could also print these right away, since you are guaranteed that n >= 2. If you do this, how would you need to modify n so that you only generate n Fibonacci numbers?

    Once you get the program to generate and print the n Fibonacci numbers, you can then work on getting it to do so inside of a function, and to have the function also compute and return the sum of the numbers.