## Exercises

1. The absolute value of a number $$x$$ is defined to be the number $$x$$ itself if $$x \ge 0$$, whereas it is the opposite of $$x$$ if $$x < 0$$. Thus:
• the absolute value of 3 is 3;
• the absolute value of -3 is -(-3), which is 3;
• the absolute value of -5.7 is -(-5.7), which is 5.7
• the absolute value of 0 is 0.

The absolute value is important enough that R provides the abs() function to compute it. Thus:

abs(-3.7)
## [1] 3.7

Write a function called absolute() that computes the absolute value of any given number x. Your code should make no reference to R’s abs().

Small Bonus: Write the function so that it follows the vector-in, vector-out principle, that is: when it is given a vector of numbers it returns the vector of their absolute values, like this:

vec <- c(-3, 5, -2.7)
absolute(vec)
## [1] 3.0 5.0 2.7
2. Write a function called pattern() that, when given any character $$x$$ and any positive number $$n$$, will print the following pattern to the console:

• a line with just one $$x$$,
• another line with two $$x$$’s,
• another line with three $$x$$’s,
• and so on until …
• a line with $$n$$ $$x$$’s, and then
• another line with $$n-1$$ $$x$$’s,
• and so on until …
• a line with just one $$x$$.

The function should take two arguments:

• char: the character to repeat. The default value should be "*".
• n: the number of characters in the longest, middle line. The default value should be 3.

Typical examples of use should be as follows:

pattern()
## *
## **
## ***
## **
## *
pattern(char = "y", n = 5)
## y
## yy
## yyy
## yyyy
## yyyyy
## yyyy
## yyy
## yy
## y
3. Write a function called beerTune() that prints out the complete lyrics to the song Ninety-Nine Bottles of Beer on the Wall. You’ll recall that the song goes like this:

99 bottles of beer on the wall,

99 bottles of beer!

Take one down and pass it around:

98 bottles of beer on the wall.

98 bottles of beer on the wall,

98 bottles of beer!

Take one down and pass it around:

97 bottles of beer on the wall.

1 bottle of beer on the wall.

1 bottle of beer!

Take it down and pass it around:

No more bottles of beer on the wall.

Make sure to get the lyrics exactly right. For example, it’s “1 bottle”, not “1 bottles”.

4. Recall the function madhavaPI() from Section 3.4.1. Use this function to write a function called madhavaGraph() that will do the following: given a number $$n$$, the function uses ggplot2 to produce a line graph of the first $$n$$ approximations to $$\pi$$ using the initial terms of the Madhava series. The plot should be a line graph similar to the one produced by the collatz() functions from this Chapter. The function should take a single argument n, whose default value is 10. It should validate the input: if the number entered is not at least 1, then the function should explain to the user that the he/she must enter a positive number, and then stop.

Here is an example of how the function should work:

madhavaGraph(n = -3)
## You need to enter a positive integer.  Try again!

Here is another example:

madhavaGraph(n = 20)

The output should be as in Figure 4.5.

5. (*) The Subtraction Game. In this game there are two players, A and B, and a pile of $$n$$ pebbles. The players take turns removing pebbles from the pile. On each turn, a player can take either one or two pebbles. The players who takes the last pebble wins the game.

It turns out that one of the players has a winning strategy. If the initial number of pebbles is a multiple of 3, then player who goes second has a winning strategy, whereas if the initial number of pebbles is not a multiple of 3 then the player who goes second has the winning strategy.

The idea for the winning strategy comes from the following observations:
• If there are 3 pebbles left and it’s the other player’s turn, then you will win. Why? Because after the other player removes pebbles there will be either 1 or 2 pebbles left. In either case you will be able to take the last pebble.
• If there are 6 pebbles left and it’s the other player’s turn, then you will win. Why? Because after the other player removes pebbles there will be either 4 or 5 pebbles left. In either case on your turn you will be able to reduce the number of pebbles to 3. The game is now in the state of the previous bullet item, where we know that you will win.
• If there are 9 pebbles left and it’s the other player’s turn, then you will win. Why? Because after the other player removes pebbles there will be either 7 or 8 pebbles left. In either case on your turn you will be able to reduce the number of pebbles to 6. The game is now in the state of the previous bullet item, where we know that you will win.
• And so on, for 12 left, 15 left, 18 left, etc.

As long as the number of pebbles left is a multiple of 3 and it’s the other player’s turn, you will win!

In this problem your task is to write a function called subtraction() that will play the Subtraction Game with a user. The function should take two parameters:
• n: the number of pebbles to begin with. The default value should be 12.
• userFirst: a logical parameter that indicates whether the user or the computer plays first. The default value should be TRUE, meaning that the user goes first.

Each time the computer plays it should announce how many pebbles it removed, and how many are left. When there are no pebbles left the computer should say who won and then quit the function.

The function should play optimally for the computer:
• if at the outset the computer has a winning strategy, then the computer should follow it and win.
• if at the outset the user has a winning strategy then the computer watch for opportunities to win if the user departs from her winning strategy.