Skip to content

Commit c4bc124

Browse files
committed
Logic-2 directory added
1 parent 27f82cf commit c4bc124

File tree

7 files changed

+150
-0
lines changed

7 files changed

+150
-0
lines changed

Logic-2/close_far.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Given three ints, a b c,
2+
# return True if one of b or c is "close"
3+
# (differing from a by at most 1),
4+
# while the other is "far",
5+
# differing from both other values by 2 or more.
6+
# Note: abs(num) computes the absolute value of a number.
7+
8+
9+
# close_far(1, 2, 10) → True
10+
# close_far(1, 2, 3) → False
11+
# close_far(4, 1, 3) → True
12+
13+
14+
def close_far(a, b, c):
15+
if abs(a-b) <= 1:
16+
if abs(a-c) >= 2 and abs(b-c) >= 2:
17+
return True
18+
19+
if abs(a-c) <= 1:
20+
if abs(a-b) >= 2 and abs(b-c) >= 2:
21+
return True
22+
23+
return False

Logic-2/lone_sum.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Given 3 int values, a b c, return their sum. However,
2+
# if one of the values is the same as another of the values,
3+
# it does not count towards the sum.
4+
5+
6+
# lone_sum(1, 2, 3) → 6
7+
# lone_sum(3, 2, 3) → 2
8+
# lone_sum(3, 3, 3) → 0
9+
10+
def lone_sum(a, b, c):
11+
if a== b == c:
12+
return 0
13+
elif a == b:
14+
return c
15+
elif b == c:
16+
return a
17+
elif a == c:
18+
return b
19+
else:
20+
return a+b+c

Logic-2/lucky_sum.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Given 3 int values, a b c, return their sum.
2+
# However, if one of the values is 13 then it does not count towards the sum and values to its right do not count.
3+
# So for example, if b is 13, then both b and c do not count.
4+
5+
6+
# lucky_sum(1, 2, 3) → 6
7+
# lucky_sum(1, 2, 13) → 3
8+
# lucky_sum(1, 13, 3) → 1
9+
10+
def lucky_sum(a, b, c):
11+
if a == 13:
12+
return 0
13+
elif b == 13:
14+
return a
15+
elif c == 13:
16+
return a+b
17+
else:
18+
return a+b+c

Logic-2/make_bricks.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# We want to make a row of bricks that is goal inches long.
2+
# We have a number of small bricks (1 inch each) and big bricks (5 inches each).
3+
# Return True if it is possible to make the goal by choosing from the given bricks.
4+
# This is a little harder than it looks and can be done without any loops.
5+
# See also: Introduction to MakeBricks
6+
7+
8+
# make_bricks(3, 1, 8) → True
9+
# make_bricks(3, 1, 9) → False
10+
# make_bricks(3, 2, 10) → True
11+
12+
def make_bricks(small, big, goal):
13+
if goal >= big*5:
14+
remainder = goal - (big * 5)
15+
else:
16+
remainder = goal % 5
17+
18+
if small >= remainder:
19+
return True
20+
21+
return False

Logic-2/make_chocolate.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# We want make a package of goal kilos of chocolate.
2+
# We have small bars (1 kilo each) and big bars (5 kilos each).
3+
# Return the number of small bars to use,
4+
# assuming we always use big bars before small bars.
5+
# Return -1 if it can't be done.
6+
7+
8+
# make_chocolate(4, 1, 9) → 4
9+
# make_chocolate(4, 1, 10) → -1
10+
# make_chocolate(4, 1, 7) → 2
11+
12+
def make_chocolate(small, big, goal):
13+
big_bar = 5 * big
14+
15+
if goal >= big_bar:
16+
remainder = goal - big_bar
17+
else:
18+
remainder = goal % 5
19+
20+
if remainder <= small:
21+
return remainder
22+
23+
return -1

Logic-2/no_teen_sum.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Given 3 int values, a b c, return their sum.
2+
# However, if any of the values is a teen -- in the range 13..19 inclusive --
3+
# then that value counts as 0, except 15 and 16 do not count as a teens.
4+
# Write a separate helper "def fix_teen(n):"that takes in an int value and returns that value fixed for the teen rule.
5+
# In this way, you avoid repeating the teen code 3 times (i.e. "decomposition").
6+
# Define the helper below and at the same indent level as the main no_teen_sum().
7+
8+
9+
# no_teen_sum(1, 2, 3) → 6
10+
# no_teen_sum(2, 13, 1) → 3
11+
# no_teen_sum(2, 1, 14) → 3
12+
13+
def no_teen_sum(a, b, c):
14+
#return fix_teen(a) + fix_teen(b) + fix_teen(c)
15+
16+
def fix_teen(n):
17+
if 13 <= n <= 19 and n != 15 and n != 16:
18+
return 0
19+
return n
20+
21+
return fix_teen(a) + fix_teen(b) + fix_teen(c)
22+
23+
# def fix_teen(n):
24+
# if 13 <= n <= 19 and n != 15 and n != 16:
25+
# return 0

Logic-2/round_sum.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# For this problem, we'll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more,
2+
# so 15 rounds up to 20. Alternately,
3+
# round down to the previous multiple of 10 if its rightmost digit is less than 5, so 12 rounds down to 10.
4+
# Given 3 ints, a b c, return the sum of their rounded values.
5+
# To avoid code repetition, write a separate helper "def round10(num):" and call it 3 times.
6+
# Write the helper entirely below and at the same indent level as round_sum().
7+
8+
9+
# round_sum(16, 17, 18) → 60
10+
# round_sum(12, 13, 14) → 30
11+
# round_sum(6, 4, 4) → 10
12+
13+
14+
def round_sum(a, b, c):
15+
return round10(a) + round10(b) + round10(c)
16+
17+
def round10(n):
18+
if n % 10 >= 5:
19+
return n + (10 - n % 10)
20+
return n - (n % 10)

0 commit comments

Comments
 (0)