Skip to content

Instantly share code, notes, and snippets.

View funky_case.py
def fUnKyCaSe(text):
"""Returns TEXT in fUnKyCaSe
>>> fUnKyCaSe("wats up")
'wAtS Up'
"""
def toggle_case(letter, should_up_case):
return letter.upper() if should_up_case else letter.lower()
def up_down(text, should_up_case):
if len(text) == 1:
View prime_factors.py
def is_prime(n):
"""Return True iff N is prime.
>>> is_prime(1)
False
>>> is_prime(2)
True
>>> is_prime(8)
False
>>> is_prime(21)
False
View sum_multiples.py
""""
Write a function that sums up the multiples of a given number between a given start and end.
If the start or end numbers are also multiples, it should include them in the sum.
For example, if the range is 1-12 and the divisor is 4, the function should add 4+8+12, returning 24.
"""
def sum_multiples(start, end, divisor):
"""
>>> sum_multiples(1, 12, 4)
24
View count_multiples.py
"""Write a function that will count the amount of multiples
of a given divisor between a given start number and a given end number.
It should include the start or end if they are a multiple.
For example, if the range is 1 to 12 and the divisor is 3,
the function should return 4, since 3, 6, 9, and 12 can all be evenly divided by 3.
"""
def count_multiples(start, end, divisor):
"""
View count_evens.py
"""Write a function that will count the number of even numbers between a given start number and a given end number.
It should include the start or end if they are even.
"""
def count_evens(start, end):
"""
>>> count_evens(2, 2)
1
>>> count_evens(-2, 52)
28
>>> count_evens(237, 500)
View product_of_numbers.py
"""
This function should return the product
of all the numbers from 1 to the given end number
(including the end number).
It is mostly written for you, but it has several bugs!
Tips for debugging:
* Read through the code and trace it on paper for small inputs
* Try running the tests and observe the output
* If you have a hunch, try changing the code and seeing how the output changes
View grade_assigner.py
"""
Write a function named assign_grade that:
* takes 1 argument, an integer representing the score.
* returns a grade for the score, either "A" (90-100), "B" (80-89), "C" (70-79), "D" (65-69), or "F" (0-64).
Call that function for a few different scores and log the result to make sure it works.
"""
def assign_grade(score):
"""
>>> assign_grade(95)
View world_translator.py
"""
Write a function named hello_world that:
* takes 1 argument, a language code (e.g. "es", "pt", "en")
* returns "Hello, World" for the given language
It should return English if an invalid language code is specified.
"""
def hello_world(language_code):
"""
>>> hello_world("en")
'Hello, World'
View lesser_num.py
"""
Write a function named lesser_num that:
* takes 2 arguments, both numbers.
* returns whichever number is the smaller (lesser) number.
Use a conditional to implement the function.
"""
def lesser_num(num1, num2):
"""
>>> lesser_num(45, 10)
10
View greater_num.py
"""
Write a function named greater_num that:
* takes 2 arguments, both numbers.
* returns whichever number is the greater (higher) number.
Use an if/else to implement the function.
"""
def greater_num(num1, num2):
"""
>>> greater_num(45, 10)
45