|
| 1 | +# Medium boolean logic puzzles -- if else and or not |
| 2 | + |
| 3 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 4 | +# |
| 5 | +# We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) |
| 6 | +# and big bricks (5 inches each). Return True if it is possible to make the goal by choosing from the given bricks. |
| 7 | +# This is a little harder than it looks and can be done without any loops. See also: Introduction to MakeBricks |
| 8 | +# |
| 9 | +# |
| 10 | +# make_bricks(3, 1, 8) → True |
| 11 | +# make_bricks(3, 1, 9) → False |
| 12 | +# make_bricks(3, 2, 10) → True |
| 13 | + |
| 14 | +def make_bricks(small, big, goal): |
| 15 | + resto = goal |
| 16 | + resto -= 5 * min(big, goal / 5) |
| 17 | + return resto - small <= 0 |
| 18 | + |
| 19 | + |
| 20 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 21 | +# |
| 22 | +# Given 3 int values, a b c, return their sum. However, |
| 23 | +# if one of the values is the same as another of the values, it does not count towards the sum. |
| 24 | +# |
| 25 | +# |
| 26 | +# lone_sum(1, 2, 3) → 6 |
| 27 | +# lone_sum(3, 2, 3) → 2 |
| 28 | +# lone_sum(3, 3, 3) → 0 |
| 29 | + |
| 30 | +def lone_sum(a, b, c): |
| 31 | + sum = 0 |
| 32 | + sum += a if a not in [b, c] else 0 |
| 33 | + sum += b if b not in [a, c] else 0 |
| 34 | + sum += c if c not in [a, b] else 0 |
| 35 | + return sum |
| 36 | + |
| 37 | + |
| 38 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 39 | +# Given 3 int values, a b c, return their sum. However, |
| 40 | +# if one of the values is 13 then it does not count towards the sum and values to its right do not count. |
| 41 | +# So for example, if b is 13, then both b and c do not count. |
| 42 | +# |
| 43 | +# |
| 44 | +# lucky_sum(1, 2, 3) → 6 |
| 45 | +# lucky_sum(1, 2, 13) → 3 |
| 46 | +# lucky_sum(1, 13, 3) → 1 |
| 47 | + |
| 48 | +def lucky_sum(a, b, c): |
| 49 | + sum = 0 |
| 50 | + list = [a, b, c, 13] |
| 51 | + for n in list[:list.index(13)]: |
| 52 | + sum += n |
| 53 | + return sum |
| 54 | + |
| 55 | + |
| 56 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 57 | +# |
| 58 | +# Given 3 int values, a b c, return their sum. However, |
| 59 | +# if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, |
| 60 | +# except 15 and 16 do not count as a teens. |
| 61 | +# Write a separate helper "def fix_teen(n):"that takes in an int value and returns that value fixed for the teen rule. |
| 62 | +# In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). |
| 63 | +# Define the helper below and at the same indent level as the main no_teen_sum(). |
| 64 | +# |
| 65 | +# |
| 66 | +# no_teen_sum(1, 2, 3) → 6 |
| 67 | +# no_teen_sum(2, 13, 1) → 3 |
| 68 | +# no_teen_sum(2, 1, 14) → 3 |
| 69 | + |
| 70 | +def no_teen_sum(a, b, c): |
| 71 | + def fix_teen(n): |
| 72 | + return n if n not in [13, 14, 17, 18, 19] else 0 |
| 73 | + |
| 74 | + return fix_teen(a) + fix_teen(b) + fix_teen(c) |
| 75 | + |
| 76 | + |
| 77 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 78 | +# |
| 79 | +# For this problem, we'll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, |
| 80 | +# so 15 rounds up to 20. Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, |
| 81 | +# so 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded values. To avoid code repetition, |
| 82 | +# write a separate helper "def round10(num):" and call it 3 times. |
| 83 | +# Write the helper entirely below and at the same indent level as round_sum(). |
| 84 | +# |
| 85 | +# |
| 86 | +# round_sum(16, 17, 18) → 60 |
| 87 | +# round_sum(12, 13, 14) → 30 |
| 88 | +# round_sum(6, 4, 4) → 10 |
| 89 | + |
| 90 | +def round_sum(a, b, c): |
| 91 | + def round10(num): |
| 92 | + return (num + 5) / 10 * 10 |
| 93 | + |
| 94 | + return round10(a) + round10(b) + round10(c) |
| 95 | + |
| 96 | + |
| 97 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 98 | +# |
| 99 | +# Given three ints, a b c, return True if one of b or c is "close" (differing from a by at most 1), |
| 100 | +# while the other is "far", differing from both other values by 2 or more. |
| 101 | +# Note: abs(num) computes the absolute value of a number. |
| 102 | +# |
| 103 | +# |
| 104 | +# close_far(1, 2, 10) → True |
| 105 | +# close_far(1, 2, 3) → False |
| 106 | +# close_far(4, 1, 3) → True |
| 107 | + |
| 108 | +def close_far(a, b, c): |
| 109 | + return (abs(abs(b) - abs(c)) >= 2) and \ |
| 110 | + ((abs(abs(b) - abs(a)) <= 1 and abs(abs(c) - abs(a)) >= 2) |
| 111 | + or (abs(abs(c) - abs(a)) <= 1 and abs(abs(b) - abs(a)) >= 2)) |
| 112 | + |
| 113 | + |
| 114 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 115 | +# |
| 116 | +# We want make a package of goal kilos of chocolate. |
| 117 | +# We have small bars (1 kilo each) and big bars (5 kilos each). |
| 118 | +# Return the number of small bars to use, assuming we always use big bars before small bars. |
| 119 | +# Return -1 if it can't be done. |
| 120 | +# |
| 121 | +# |
| 122 | +# make_chocolate(4, 1, 9) → 4 |
| 123 | +# make_chocolate(4, 1, 10) → -1 |
| 124 | +# make_chocolate(4, 1, 7) → 2 |
| 125 | + |
| 126 | +def make_chocolate(small, big, goal): |
| 127 | + if small + big * 5 < goal: |
| 128 | + return -1 |
| 129 | + elif goal % 5 <= small and big * 5 <= goal: |
| 130 | + return goal - (big * 5) |
| 131 | + elif goal % 5 <= small and big * 5 > goal: |
| 132 | + return goal - abs(goal / 5) * 5 |
| 133 | + else: |
| 134 | + return -1 |
| 135 | + |
| 136 | +# ---------------------------------------------------------------------------------------------------------------------- |
0 commit comments