# How to Think Like a Computer Scientist: The PreTeXt Interactive Edition

## Section16.8Programming Exercises

### Checkpoint16.8.1.

Write a recursive function to compute the factorial of a number.

### Checkpoint16.8.2.

Write a recursive function to reverse a list.

### Checkpoint16.8.3.

Modify the recursive tree program using one or all of the following ideas:
• Modify the thickness of the branches so that as the branchLen gets smaller, the line gets thinner.
• Modify the color of the branches so that as the branchLen gets very short it is colored like a leaf.
• Modify the angle used in turning the turtle so that at each branch point the angle is selected at random in some range. For example choose the angle between 15 and 45 degrees. Play around to see what looks good.
• Modify the branchLen recursively so that instead of always subtracting the same amount you subtract a random amount in some range.
If you implement all of the above ideas you will have a very realistic looking tree.

### Checkpoint16.8.4.

Find or invent an algorithm for drawing a fractal mountain. Hint: One approach to this uses triangles again.

### Checkpoint16.8.5.

Write a recursive function to compute the Fibonacci sequence. How does the performance of the recursive function compare to that of an iterative version?

### Checkpoint16.8.6.

Implement a solution to the Tower of Hanoi using three stacks to keep track of the disks.

### Checkpoint16.8.7.

Using the turtle graphics module, write a recursive program to display a Hilbert curve.

### Checkpoint16.8.8.

Using the turtle graphics module, write a recursive program to display a Koch snowflake.

### Checkpoint16.8.9.

Write a program to solve the following problem: You have two jugs: a 4-gallon jug and a 3-gallon jug. Neither of the jugs have markings on them. There is a pump that can be used to fill the jugs with water. How can you get exactly two gallons of water in the 4-gallon jug?

### Checkpoint16.8.10.

Generalize the problem above so that the parameters to your solution include the sizes of each jug and the final amount of water to be left in the larger jug.

### Checkpoint16.8.11.

Write a program that solves the following problem: Three missionaries and three cannibals come to a river and find a boat that holds two people. Everyone must get across the river to continue on the journey. However, if the cannibals ever outnumber the missionaries on either bank, the missionaries will be eaten. Find a series of crossings that will get everyone safely to the other side of the river.

### Checkpoint16.8.12.

Modify the Tower of Hanoi program using turtle graphics to animate the movement of the disks. Hint: You can make multiple turtles and have them shaped like rectangles.

### Checkpoint16.8.13.

Pascal's triangle is a number triangle with numbers arranged in staggered rows such that
a_{nr} = {n! \over{r! (n-r)!}}
This equation is the equation for a binomial coefficient. You can build Pascal's triangle by adding the two numbers that are diagonally above a number in the triangle. An example of Pascal's triangle is shown below.
        1
1   1
1   2   1
1   3   3   1
1   4   6   4   1

Write a program that prints out Pascal's triangle. Your program should accept a parameter that tells how many rows of the triangle to print.
{"ft_lang_label":"__label__en","ft_lang_prob":0.87751675,"math_prob":0.87230855,"size":3073,"snap":"2022-40-2023-06","text_gpt3_token_len":700,"char_repetition_ratio":0.14858261,"word_repetition_ratio":0.06395349,"special_character_ratio":0.22583795,"punctuation_ratio":0.12995246,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9837149,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-29T00:09:04Z\",\"WARC-Record-ID\":\"<urn:uuid:290b359f-09af-425a-b254-78ea20a8bd26>\",\"Content-Length\":\"102421\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8de56e59-48d3-49b3-9e27-82135c4bbd5d>\",\"WARC-Concurrent-To\":\"<urn:uuid:88ac53c1-ffcf-4e39-a7b6-7a11a0f898f7>\",\"WARC-IP-Address\":\"134.122.31.165\",\"WARC-Target-URI\":\"https://runestone.academy/ns/books/published/httlacs/intro-recursion_programming-exercises.html\",\"WARC-Payload-Digest\":\"sha1:EZSNOYN62BYTACLLUHUHBRPIID4LZSHQ\",\"WARC-Block-Digest\":\"sha1:MIQQLPYIGSEQ65BWWNOAUNBHNWQPRBDS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499695.59_warc_CC-MAIN-20230128220716-20230129010716-00412.warc.gz\"}"}