Created
June 15, 2016 08:54
-
-
Save G-Goldstein/08f02a5b11fa0b93652f042e9c080915 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
# Challenge one: What does this function do? How do you use it? | |
def welcome(name): | |
return 'Hello ' + name | |
# Solution: | |
### When called and a name passed, this function returns a greeting string for that name. Importantly, this function doesn't print anything to the console, it just creates a string that we can then print later, or do other things with. | |
person = 'Graham' | |
greeting = welcome(person) | |
print(greeting) | |
## Outputs "Hello Graham" | |
# or: | |
print(welcome('Graham')) | |
## This also outputs "Hello Graham". Here, we're passing the name directly into the function, and passing the function's output directly to the print function. | |
# Challenge two: Write a function to double a number. Test it. | |
# Lee & Andrew W's solutions: | |
def calc(number): | |
return 2 * number | |
# Usage | |
print(calc(8)) | |
## As above, we're passing the result of calc(8) directly into the print function. It'll print out 16 to the console. | |
# Multiple parameters | |
def total_wheels(bikes, cars, trucks): | |
bike_wheels = bikes * 2 | |
car_wheels = cars * 4 | |
truck_wheels = trucks * 18 | |
return bike_wheels + car_wheels + truck_wheels | |
print(total_wheels(3, 1, 0)) | |
## This prints the number 10, which is the total number of wheels for 3 bikes and a car. | |
# Named parameters | |
print(total_wheels(trucks=1, bikes=2, cars=0)) | |
## Notice that the parameters are passed in a different order to which they're declared. As long as they're named, this is fine. | |
# Conditions | |
def calculate_pay(hours_worked, hourly_rate): | |
# Working weeks are 40 hours and overtime is paid at time and a half. | |
if hours_worked <= 40: | |
return hours_worked * hourly_rate | |
else: | |
overtime = 40 - hours_worked | |
return (40 + 1.5 * overtime) * hourly_rate | |
# Else clause isn't required: | |
def calculate_pay(hours_worked, hourly_rate): | |
# Working weeks are 40 hours and overtime is paid at time and a half. | |
if hours_worked <= 40: | |
return hours_worked * hourly_rate | |
overtime = 40 - hours_worked | |
return (40 + 1.5 * overtime) * hourly_rate | |
# Challenge three: Write a function that takes two numbers and returns the largest. | |
# Andrew C | |
def highest(numb1, number2): | |
if numb1 > numb2: | |
return numb1 | |
if numb2 > numb1: | |
return numb2 | |
print(highest(5, 10)) | |
## Andrew's taken my poor requirements very literally. If numb1 and numb2 are the same, the function won't hit a return statement at all. In that case, the function will return a special Python type 'None'. Let this be a lesson to me about specifying what I want! | |
# Lee | |
def largest(number1, number2): | |
if number1 >= number2: | |
return number1 | |
elif number1 <= number2: | |
return number2 | |
print largest(10, 20) | |
## Lee's solution will always hit a return statement as he uses >=, which is 'greater than or equal to'. In the case that the numbers are the same, it'll return the number that they both are. | |
# Andrew W | |
def high_number(number1, number2): | |
if number1 > number2: | |
return number1 | |
else: | |
return number2 | |
print(high_number(40, 5)) | |
## Andrew's solution is very elegant. After one comparison, we don't need to check the reverse so we can assume number2 >= number1. | |
# Challenge four: Write a function that takes three numbers and returns the largest. | |
# 'and' | |
if number1 > number2 and number1 > number3: # Good | |
if number1 > number2 and number3: # Bad | |
## Python uses 'and' and 'or' to combine logical statements. The Good example here works, but watch out for the Bad example, which will behave strangely. | |
# Lee | |
def largest(number1, number2, number3): | |
if number1 > number2 and number1 > number3: | |
return number1 | |
elif number2 > number1 and number2 > number3: | |
return number2 | |
elif number3 > number1 and number3 > number2: | |
return number3 | |
print largest(1, 2, 3) | |
#Sonal | |
def largest(num1, num2, num3): | |
if num1 > num2 and num1 > num3: | |
return num1 | |
elif num2 > num1 and num2 > num3: | |
return num2 | |
elif num3 > num1 and num3 > num2: | |
return num3 | |
print (largest(10, 20, 30)) | |
#andrew w | |
def high_number(number1, number2, number3): | |
if number1 > number2 and number1 > number3: | |
return number1 | |
elif number2 > number3: | |
return number2 | |
else: return number3 | |
print(high_number(45, 41, 66)) | |
## Here, Andrew W uses the information gained from earlier comparisons to reduce the number of checks to make later on. | |
# Andrew C | |
def highest(numb1, numb2): | |
if numb1 > numb2: | |
return numb1 | |
if numb2 > numb1: | |
return numb2 | |
def higher(numba, numbb, numbc): | |
highestnumb = highest(numba, numbb) | |
highestnumb = highest(highestnumb, numbc) | |
print(higher(5, 3, 60)) | |
## Andrew C reuses our earlier function, which finds the highest of two numbers, to help perform the work for the 'highest of three' function. This is good practice in any sort of programming. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment