aboutsummaryrefslogtreecommitdiff
path: root/challenge-285/packy-anderson/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-285/packy-anderson/python')
-rwxr-xr-xchallenge-285/packy-anderson/python/ch-1.py32
-rwxr-xr-xchallenge-285/packy-anderson/python/ch-2.py68
2 files changed, 100 insertions, 0 deletions
diff --git a/challenge-285/packy-anderson/python/ch-1.py b/challenge-285/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..18d8eb136f
--- /dev/null
+++ b/challenge-285/packy-anderson/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+
+def noConnection(routes):
+ starts = set([ x[0] for x in routes ])
+ ends = set([ x[1] for x in routes ])
+ noConnection = ends - starts
+ if len(noConnection) == 0:
+ return 'no terminal destinations'
+ if len(noConnection) > 1:
+ return 'multiple terminal destinations'
+ return list(noConnection)[0]
+
+def tupleJoin(listVar):
+ return ', '.join([
+ f'[{x[0]}, {x[1]}]' for x in listVar
+ ])
+
+def solution(routes):
+ print(f'Input: @routes = ({ tupleJoin(routes) })')
+ print(f'Output: {noConnection(routes)}')
+
+print('Example 1:')
+solution([["B","C"], ["D","B"], ["C","A"]])
+
+print('\nExample 2:')
+solution([["A","Z"], ])
+
+print('\nBad Input: multiple terminal destination')
+solution([["A","B"], ["C", "D"]])
+
+print('\nBad Input: no terminal destinations')
+solution([["A","Z"], ["Z", "A"]])
diff --git a/challenge-285/packy-anderson/python/ch-2.py b/challenge-285/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..01cc096f29
--- /dev/null
+++ b/challenge-285/packy-anderson/python/ch-2.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+
+from functools import cache
+
+# establish the values of the coins
+types = { 50: 'HD', 25: 'Q', 10: 'D', 5: 'N', 1: 'P' }
+
+# make a sorted list of coin values
+values = sorted(types.keys(), reverse=True)
+
+def formatCoin(coin, count):
+ return f"{count}{coin}" if count > 1 else coin
+
+@cache
+def makeChange(amount, exclude = 0):
+ output = []
+ for value in values:
+ # if this type of coin is worth more
+ # than the total, we can't use it
+ if value > amount:
+ continue
+
+ # if we're excluding this coin value or greater, skip it
+ if exclude and value >= exclude:
+ continue
+
+ coin = types[value] # get the coin name
+
+ if value == 1:
+ # pennies are easy!
+ output.append( formatCoin(coin, amount) )
+ else:
+ # loop from max number of this coin we can use down to 1
+ for count in range(int(amount / value), 0, -1):
+ # figure out how much change we need after we've used
+ # $count many of coin $coin
+ left = amount - (count * value)
+
+ if left > 0: # we need more change
+ # make a recursive call to get the combinations
+ # for that remaining amount, excluding any coins
+ # of equal or greater value to $value
+ combinations = makeChange(left, value)
+ for combo in combinations:
+ this_coin = formatCoin(coin, count)
+ output.append(" + ".join([this_coin, combo]))
+ else: # this is exact change!
+ output.append( formatCoin(coin, count) )
+ return output
+
+def solution(amount):
+ print(f'Input: $amount = {amount}')
+ ways = makeChange(amount)
+ print(f'Output: {len(ways)}')
+ print('')
+ i = 0
+ for c in ways:
+ i += 1
+ print(f'{i}: {c}')
+
+print('Example 1:')
+solution(9)
+
+print('\nExample 2:')
+solution(15)
+
+print('\nExample 3:')
+solution(100)