Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Math 480 - Homework 2: Due 6pm on April 8, 2016
There are 6 problems and all have equal weights.
Problem 1: Truthiness
An expression e is truthy if bool(e)
is True; otherwise e is falsy.
Different programming languages may have different truthy and falsy values (I'm looking at you javascript).
1a. Give 10 different Python objects that are falsy. Your list might include e.g., []
and 0
. Also, by "different" we mean that a is b
evaluates to False.
1b. In Python "is" means "identical objects", whereas "==" can be much more subtle. Give 5 examples of Python objects a
and b
such that a==b
is true but a is b
is not true.
Problem 2: Control Flow
Write a function named fizz_buzz that accepts an integer N and for each integer m from 1 to N, prints 'Fizz' if m is divisible by 2, prints 'Buzz' if m is divisible by 3, prints 'FizzBuzz' if m is divisible by 2 and 3, and prints 'Moot' if none of the above.
eg. Calling fizz_buzz(7)
Prints
Problem 3: Set comprehensions
Convert the following sets written in English into sets (not lists!) in Python using set comprehensions.
3a.
3b. The set of primes less than 100 that are congruent to 1 modulo 4
3c. The set of positive integers such that
3d. The set of prime number years during this century (i.e., between 2000 and 2100).
Problem 4: Better and Worse
4a. Implement a "good" function that returns the Nth Fibonacci number.
4b. What makes your choice better than others? (Hint: read about how recursion in Python sucks.)
Problem 5: Fibonacci numbers
5a. Using your function above, generate a list of the first 100 Fibonacci numbers.
5b. Write code that computes how many of these Fibonacci numbers are prime.
5c. Do you think there are infinitely many prime Fibonacci numbers? (Back up your claim with data and/or internet searches.)
Solution 5c:
Problem 6: Python Dictionaries
6a. Search online and find as many names as you can for data structures that are like a Python "dictionary", but in other programming languages. For example, in Javascript the analogue of a Python dictionary is called a "Map".
6b. Give five different types of Python objects that can't be the keys of a Python dictionary, and five that can be keys. Your answer should consist of objects
obj
so thattype(obj)
are different.