Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: math480-2016
Views: 988
%md # Math 480 - Homework 2: Due 6pm on April 8, 2016

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.

# Solution 1a: [] # Solution 1b: [0.0 == 0, 0.0 is 0]
[True, False]

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

Moot Fizz Buzz Fizz Moot FizzBuzz Moot
# Solution 2

Problem 3: Set comprehensions

Convert the following sets written in English into sets (not lists!) in Python using set comprehensions.

  • 3a. X={a:0<a<100 and a is even}X = \{a : 0 < a < 100 \text{ and }a \text{ is even}\}

  • 3b. The set of primes less than 100 that are congruent to 1 modulo 4

  • 3c. The set of positive integers n<1000n<1000 such that n3+13(mod1)n^3 + 1 \equiv 3 \pmod{1}

  • 3d. The set of prime number years during this century (i.e., between 2000 and 2100).

# Solution 3a:
# Solution 3b:
# Solution 3c:
# Solution 3d:

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.)

# Solution 4a: # fib(n)... # return nth Fibonacci number
%md #### Solution 4b:

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 5a:
# Solution 5b:
%md #### Solution 5c:

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 that type(obj) are different.

%md #### Solution 6a: e1f980c1-57d1-40de-a761-ebdfdad48a28︠ # Solution 6b: ︠106c1fa9-73fc-416c-b183-afd1b7ef75b9︠