Created
January 19, 2019 20:14
-
-
Save MarketaP/9a6b4b16235b7fc78c05cbbc787b12ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Homework 2 | |
We will be submitting this assignment on Canvas. | |
It's due before Jan 24 11:59pm. | |
""" | |
############################################################################### | |
# Write a function called fibonacci that takes an int as an argument called n | |
# and returns a list of the fibonacci sequence that is n long. | |
# The fibonacci sequence is a sequence where each number in the | |
# sequence is the sum of the last two numbers. For this problem assume the | |
# first two numbers are 1. | |
# Example: fibonacci(5) should yield [1, 1, 2, 3, 5] | |
# Example: fibonacci(0) should yield [] | |
# For more on the sequence: | |
# https://en.wikipedia.org/wiki/Fibonacci_number | |
############################################################################### | |
def fibonnacci(n): | |
'''Returns Fibonnacci sequence that is n long''' | |
my_list = [1, 1] | |
for i in range(1, n-1): | |
next_number = my_list[i - 1] + my_list[i] | |
my_list.append(next_number) | |
print my_list | |
fibonnacci(5) | |
############################################################################### | |
# We have already mentioned that functions are variables. | |
# We have also shown function composition: f(g()). | |
# We can also write functions that take an uncalled function as an argument: | |
# f(g) where g is a function name. | |
# There are more functions than needed below. | |
# Generalize the behavior of the functions into one function so that the | |
# single function can run a function passed to it any number of times. | |
# | |
# This is an exercise in refactoring. Refactoring often seeks to remove | |
# repeated or unecessary code. There is a principle in programming called | |
# DRY (Do not Repeat Yourself). One way to DRY is to combine similar | |
# functions into one generalized function. | |
############################################################################### | |
def do_twice(func): | |
"""Run the input function twice.""" | |
func() | |
func() | |
def do_thrice(func): | |
"""Run the input function three times.""" | |
func() | |
func() | |
func() | |
# Write new function here. | |
def do_ntimes(n, func): | |
for _ in range(n): | |
func() | |
############################################################################### | |
# This is a classic problem given during job interviews to check if a | |
# a progammer knows his or her basics. | |
# | |
# Write a short program that prints each number from 1 to 100 on a new line. | |
# For each multiple of 3, print "Fizz" instead of the number. | |
# For each multiple of 5, print "Buzz" instead of the number. | |
# For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of | |
# the number. | |
############################################################################### | |
def FizzBuzz(number): | |
for i in range(1, number + 1): | |
if i % 15 == 0: | |
print("FizzBuzz") | |
elif i % 5 == 0: | |
print("Buzz") | |
elif i % 3 == 0: | |
print("Fizz") | |
else: | |
print(i) | |
FizzBuzz(100) | |
def FizzBuzz2(number): | |
for i in range(1, number + 1): | |
string = "" | |
if i % 3 == 0: | |
string = string + "Fizz" | |
if i % 5 == 0: | |
string = string + "Buzz" | |
print(string if string is not "" else i) | |
FizzBuzz2(100) | |
############################################################################### | |
# Write a list comprehension that will give a list of the means of each | |
# original nested list. | |
# Hint: you will need to write a mean function. We wrote one during class. | |
# It's in the slides. | |
############################################################################### | |
example_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] | |
# Expected result is [2.0, 5.0, 8.0] | |
def mean(some_list): | |
return sum(some_list)/len(some_list) | |
mean_list = [mean(a) for a in example_data] | |
print(mean_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment