Chapter 15 Mini-challenges

15.1 Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz.” For numbers which are multiples of both three and five output “FizzBuzz.”


foo = FizzBuzz()
foo.fizzBuzz(15)
## ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']

15.2 Fibonacci Extended

The Fibonacci sequence starts with a one or a zero, followed by a one, and proceeds based on the rule that each number is equal to the sum of the preceding two numbers, e.g. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

We can generalize this so that the sum of the preceding n numbers form the next term. e.g. with n=3, 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, …

Write a function that takes two inputs: n and m, and returns m values from an n-Fibonacci sequence.

n = 3 # depth of sum
m = 8 # Until the mth term
fiboExt(n,m)
## [0, 0, 1, 1, 2, 4, 7, 13]

15.3 Points Distance

A Cartesian plane is defined by two perpendicular number lines: the x-axis, which is horizontal, and the y-axis, which is vertical. Using these axes, we can describe any point in the plane using an ordered pair of numbers.

Write a function which takes two different points as a list [x1, y2, x2, y2], and returns the distance between these two points.

The distance formula is the Pythagorean Theorem:

\[distance = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}\]

get_distance([2, 1, 5, 5])
## 5

15.4 Emirp Numbers

An emirp number is a prime number, that when reversed, is still a prime number, i.e. emirp, get it? :)

e.g. 13 is an emirp, since both 13 and 31 are prime numbers.

Write two functions. is_prime() checks if a number is prime, is_emirp() checks if the reverse is also prime. If both conditions are met then it returns True. You can of course check that a number is prime inside is_emirp(), but having two functions will help to compartmentalize our code.

e.g.: Input: 13 Output: true (13 and 31 are prime numbers)

Input: 23 Output: false (23 is a prime number, but 32 is not)

Bonus 1: Exclude palindromic primes since the definition of an emirp is meaningless here.

Bonus 2: return a sentence, dependent on the result of

## 23 is not an emirp number.
is_emirp(13)
## True

15.5 Summations Calculator

Create a function that takes 3 inputs, a lower bound, an upper bound and the expression. Calculate the sum of that range based on the given expression and output the result.

For Example:

Input: [‘2,’ ‘4,’ ’*2’]

Output: 18 (22 + 32 + 4*2)

Input: w = [‘1,’ ‘5,’ ‘%2’]

Output: 3 (1%2 + 2%2 + 3%2 + 4%2 + 5%2)

summation(['2', '4', '*2'])
## 18
summation(['1', '5', '%2'])
## 3