Skip to content

Instantly share code, notes, and snippets.

@AnisahTiaraPratiwi
AnisahTiaraPratiwi / CodeStyle.py
Created March 20, 2021 10:19
This function to calculate the area of a rectangle is not very readable. Can you refactor it, and then call the function to calculate the area with base of 5 and height of 6? Tip: a function that calculates the area of a rectangle should probably be called rectangle_area, and if it's receiving base and height, that's what the parameters should b…
def rectangle_area(base, height):
z = base*height # the area is base*height
print("The area is " + str(z))
rectangle_area(5,6)
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Functions1.py
Created March 20, 2021 10:20
Question 1 This function converts miles to kilometers (km). Complete the function to return the result of the conversion Call the function to convert the trip distance from miles to kilometers Fill in the blank to print the result of the conversion Calculate the round-trip in kilometers by doubling the result, and fill in the blank to print the …
# 1) Complete the function to return the result of the conversion
def convert_distance(miles):
km = miles * 1.6 # approximately 1.6 km in 1 mile
return km
my_trip_miles = 55
# 2) Convert my_trip_miles to kilometers by calling the function above
my_trip_km = convert_distance(my_trip_miles)
# 3) Fill in the blank to print the result of the conversion
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Functions2.py
Created March 20, 2021 10:21
This function compares two numbers and returns them in increasing order. Fill in the blanks, so the print statement displays the result of the function call in order. Hint: if a function returns multiple values, don't forget to store these values in multiple variables
# This function compares two numbers and returns them
# in increasing order.
def order_numbers(number1, number2):
if number2 > number1:
return number1, number2
else:
return number2, number1
# 1) Fill in the blanks so the print statement displays the result
# of the function call
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Functions3.py
Created March 20, 2021 10:22
Let's revisit our lucky_number function. We want to change it, so that instead of printing the message, it returns the message. This way, the calling line can print the message, or do something else with it if needed. Fill in the blanks to complete the code to make it work.
def lucky_number(name):
number = len(name) * 9
print("Hello " + name + ". Your lucky number is " + str(number))
(lucky_number("Kay"))
(lucky_number("Cameron"))
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Conditionals.py
Created March 20, 2021 10:25
Question 2 Complete the script by filling in the missing parts. The function receives a name, then returns a greeting based on whether or not that name is "Taylor"
def greeting(name):
if name == "Taylor":
return "Welcome back Taylor!"
else:
return "Hello there, " + name
print(greeting("Taylor"))
print(greeting("John"))
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Conditionals1.py
Created March 20, 2021 10:26
Question 5 If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096 bytes of storage. A file made up of 4097 bytes will use 4096*2=8192 bytes of storage. Knowing this, can you fill in the gaps in the calculate_storage function below, which calculates the total number of bytes needed to…
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = filesize//4096
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = filesize%4096
# Depending on whether there's a remainder or not, return
# the total number of bytes required to allocate enough blocks
# to store your data.
if partial_block_remainder > 0:
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / BasicSyntax.py
Created March 20, 2021 10:28
Complete the function by filling in the missing parts. The color_translator function receives the name of a color, then prints its hexadecimal value. Currently, it only supports the three additive primary colors (red, green, blue), so it returns "unknown" for all other colors.
def color_translator(color):
if color == "red":
hex_color = "#ff0000"
elif color == "green":
hex_color = "#00ff00"
elif color == "blue":
hex_color = "#0000ff"
else:
hex_color = "unknown"
return hex_color
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / BasicSyntax1.py
Created March 20, 2021 10:29
Question 4 Students in a class receive their grades as Pass/Fail. Scores of 60 or more (out of 100) mean that the grade is "Pass". For lower scores, the grade is "Fail". In addition, scores above 95 (not included) are graded as "Top Score". Fill in this function so that it returns the proper grade.
def exam_grade(score):
if score > 95:
grade = "Top Score"
elif score >= 60:
grade = "Pass"
else:
grade = "Fail"
return grade
print(exam_grade(65)) # Should be Pass
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / BasicSyntax2.py
Created March 20, 2021 10:31
First Name Last Name
def format_name(first_name, last_name):
# code goes here
if first_name != '' and last_name != '':
return ("Name: " + last_name + ", " + first_name)
elif first_name != '' or last_name != '':
return ("Name: " + last_name + first_name)
else:
return ''
print(format_name("Ernest", "Hemingway"))
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / BasicSyntax3.py
Created March 20, 2021 10:32
The longest_word function is used to compare 3 words. It should return the word with the most number of characters (and the first in the list when they have the same length). Fill in the blank to make this happen.
def longest_word(word1, word2, word3):
if len(word1) >= len(word2) and len(word1) >= len(word3):
word = word1
elif len(word2) >+ len(word1) and len(word2) >= len(word3):
word = word2
else:
word = word3
return(word)
print(longest_word("chair", "couch", "table"))