Logic and Problem Solving Worksheet
Problem 1
Write a function sum_checker
that takes three integer arguments and returns bool True
if any two of the arguments sum to the third argument, otherwise returns bool False
.
sum_checker(1, 3, 2)
returnsTrue
sum_checker(1, 0, 2)
returnsFalse
sum_checker(100, 50, 50)
returnsTrue
def sum_checker(a, b, c):
#your code here
return
assert sum_checker(1, 0, 1) == True
assert sum_checker(0, 0, 0) == True
assert sum_checker(100, 50, 51) == False
assert sum_checker(2, 4, 6) == True
assert sum_checker(1, 2, 5) == False
assert sum_checker(0, 1, 1) == True
assert sum_checker(50, 24, 26) == True
Problem 2
Write a function spacing_checker
that takes a string argument that will be in the format:
'A B'
where the string starts with A
and ends in B
and has some unknown number of spaces between A
and B
.
Return an int corresponding to how many spaces are between A
and B
.
Hint: Use a loop to build strings using concatenation. Check each string for equality with the input argument.
def spacing_checker(str_arg):
# your code here
return
assert spacing_checker("A B") == 2
assert spacing_checker("A B") == 4
assert spacing_checker("AB") == 0
assert spacing_checker("A B") == 6
for j in range(60000,60003):
assert spacing_checker("A" + " " * j + "B") == j # huge j!
Problem 3
Write a function next_square
that takes a single argument, which might be an int or a float. Your function should return an int: the next largest perfect square:
next_square(1)
returns4
next_square(5.5)
returns9
next_square(-100)
returns0
next_square(63.5)
returns64
# define your function!
assert next_square(1) == 4
assert next_square(2) == 4
assert next_square(4) == 9
assert next_square(25) == 36
assert next_square(35.7) == 36
assert next_square(36.1) == 49
Problem 4
Write a function realistic_pizza_party
similar to the pizza_party
problem from lecture. Your function should take one argument: a number of people, and return the minimum number of pizzas needed in order to either:
- Have everyone get the same number of slices
- Have everyone get at least three slices
Examples:
realistic_pizza_party(3)
returns2
- One person gets 6 slices, the other two get 5
realistic_pizza_party(4)
returns1
- Each person gets 2 slices
# define your function here
assert realistic_pizza_party(3) == 2
assert realistic_pizza_party(5) == 2
assert realistic_pizza_party(7) == 3
assert realistic_pizza_party(9) == 4
assert realistic_pizza_party(14) == 6
Problem 5
Write a function int_digit_sum
that takes a single integer argument and returns the sum of the integer’s digits.
Examples:
int_digit_sum(12)
returns3
int_digit_sum(121)
returns4
int_digit_sum(500000)
returns5
If you’ve read ahead in the notes, you might have found a way to do this problem in a very simple way, by converting the input argument to a string.
Try to do this problem without strings. Modulus and division with a loop will do the trick.
# define your function here
assert int_digit_sum(100) == 1
assert int_digit_sum(101) == 2
assert int_digit_sum(1000002) == 3
assert int_digit_sum(127) == 10
assert int_digit_sum(1027) == 10