Last active
January 8, 2019 17:06
-
-
Save camckin10/ced02e032a11a92ffc4e50cdac83abce to your computer and use it in GitHub Desktop.
WarmUp Exercises from CodingBat Python(Python3)
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
#EXERCISE #1 | |
#diff21--Given an int n, return the absolute difference between n and 21, except return double the absolute difference if | |
#n is over 21. | |
#first solution try--only got 1 out of 12 correct | |
def diff21(n): | |
if n < 21: | |
return n *2 | |
else: | |
return n - 21 | |
#2nd solution try--all tests correct | |
def diff21(n): | |
#if n is less than or equal to 21 | |
if n <= 21: | |
#return 21 - n , which would be absolute difference | |
return 21-n | |
else: | |
#if not less than/equal to 21, return n *2 | |
return (21-n) * 2 | |
#EXERCISE #2 | |
#parrot_trouble-We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. | |
#We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return True if we are in trouble. | |
#current time--range(0,23) . # <7 or >20--Talking parrot, True b/c parrot is in trouble . #False if >7 and <20 | |
#1st solution try--correct for more than half the tests. :) 6 out of 10 | |
def parrot_trouble(talking, hour): | |
if hour > 7 and hour <20: | |
return True | |
else: | |
return False | |
#2nd solution try--correct for more than half the tests. :) 6 out of 10 | |
def parrot_trouble(talking, hour): | |
if talking: | |
return True | |
elif talking and hour >7: | |
return True | |
elif talking or hour <20: | |
return True | |
else: | |
return False | |
#EXERCISE #3 | |
#makes10--Given 2 ints,a and b, return True is on of them is 10 or their sum is 10. | |
#1st solution try: --correct for more than half the tests | |
def makes10(a,b): | |
if a + b == 10: | |
return True | |
elif a - b == 10: | |
return True | |
else: | |
return False | |
#2nd solution try--correct for more than half the tests | |
def makes10(a,b): | |
if a + b == 10: | |
return True | |
elif a - b == 10: | |
return True | |
elif b + a == 10: | |
return True | |
elif b - a == 10: | |
return True | |
else: | |
return False | |
#3rd solition try--correct for 6 out of 9 tests | |
#also, much shorter code than 2nd solution! | |
def makes10(a,b): | |
if a + b and a - b == 10: | |
return True | |
elif a == 10 or b == 10: | |
return True | |
else: | |
return False | |
#EXERCISE #4 | |
#near_hundred--Given an int n, return True if it is within 10 or 100 or 200. | |
#Note:abs(num) computes the absolute value of a number. n = 10 | |
#1st solution try--correct for more than half; 14 out of 20 tests correct | |
def near_hundred(n): | |
if n < 90 and 190: | |
return False | |
else: | |
return True | |
#2nd solution try--correct for more than half; 11 out of 20 tests correct | |
def near_hundred(n): | |
n = 10 | |
if (n - 100) <= 10 and (n - 200) <= 10: | |
return False | |
else: | |
return True | |
#EXERCISE #5 | |
#pos_neg--Given 2 int values. return True if one is negative and one is positive. Except if parameter "negative" is True | |
#return True only if both are negative. | |
#1st solution try--correct for 8 out of 19 tests; missed 11 | |
def pos_neg(a,b,negative): | |
if a == b or a > b: | |
return True | |
elif negative == "negative": | |
return True | |
else: | |
return False | |
#2nd solution try | |
def pos_neg(a,b,negative): | |
if negative: | |
return True | |
#elif -a or -b: | |
#return True | |
#elif a and b == negative: | |
#return True | |
else: | |
return False | |
#EXERCISE #6 | |
#not_string--Given a string, return a new string with "not" in front of it. | |
#However, if string already has not, return unchanged | |
#site solution | |
#def not_string(str): | |
#if len(str) >= 3 and str[:3] == "not": | |
#return str | |
#return "not " + str | |
# str[:3] goes from the start of the string up to but not | |
# including index 3 | |
#1st solution try | |
def not_string(str): | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment