From f0d97c322ad8201eaf3c5a95e2fb74eba1ceaa82 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 2 Sep 2024 13:51:02 +1000 Subject: pwc285 solution in python --- challenge-285/pokgopun/python/ch-1.py | 50 +++++++++++++++++++++++ challenge-285/pokgopun/python/ch-2.py | 75 +++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 challenge-285/pokgopun/python/ch-1.py create mode 100644 challenge-285/pokgopun/python/ch-2.py (limited to 'challenge-285/pokgopun/python') diff --git a/challenge-285/pokgopun/python/ch-1.py b/challenge-285/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..fcebb07e89 --- /dev/null +++ b/challenge-285/pokgopun/python/ch-1.py @@ -0,0 +1,50 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-285/ +""" + +Task 1: No Connection + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of routes, @routes. + + Write a script to find the destination with no further outgoing + connection. + +Example 1 + +Input: @routes = (["B","C"], ["D","B"], ["C","A"]) +Output: "A" + +"D" -> "B" -> "C" -> "A". +"B" -> "C" -> "A". +"C" -> "A". +"A". + +Example 2 + +Input: @routes = (["A","Z"]) +Output: "Z" + +Task 2: Making Change +""" +### solution by pokgopun@gmail.com + +def nc(routes: tuple): + srcs = tuple(e[0] for e in routes) + for src, dst in routes: + if dst not in srcs: + return dst + return None + +import unittest + +class TestNc(unittest.TestCase): + def test(self): + for inpt, otpt in { + (("B","C"), ("D","B"), ("C","A")): "A", + (("A","Z"),): "Z", + }.items(): + self.assertEqual(nc(inpt),otpt) + +unittest.main() diff --git a/challenge-285/pokgopun/python/ch-2.py b/challenge-285/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..813b8cf378 --- /dev/null +++ b/challenge-285/pokgopun/python/ch-2.py @@ -0,0 +1,75 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-285/ +""" + +Task 2: Making Change + +Submitted by: [50]David Ferrone + __________________________________________________________________ + + Compute the number of ways to make change for given amount in cents. By + using the coins e.g. Penny, Nickel, Dime, Quarter and Half-dollar, in + how many distinct ways can the total value equal to the given amount? + Order of coin selection does not matter. +A penny (P) is equal to 1 cent. +A nickel (N) is equal to 5 cents. +A dime (D) is equal to 10 cents. +A quarter (Q) is equal to 25 cents. +A half-dollar (HD) is equal to 50 cents. + +Example 1 + +Input: $amount = 9 +Ouput: 2 + +1: 9P +2: N + 4P + +Example 2 + +Input: $amount = 15 +Ouput: 6 + +1: D + 5P +2: D + N +3: 3N +4: 2N + 5P +5: N + 10P +6: 15P + +Example 3 + +Input: $amount = 100 +Ouput: 292 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 8th September + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def mc(amount: int): + c = 0 + for a50 in range(0,amount+1,50): + for a25 in range(0,amount-a50+1,25): + for a10 in range(0,amount-a50-a25+1,10): + for a5 in range(0,amount-a50-a25-a10+1,5): + for a1 in range(0,amount-a50-a25-a10-a5+1,1): + if a50 + a25 + a10 + a5 + a1 == amount: + c += 1 + return c + +import unittest + +class TestMc(unittest.TestCase): + def test(self): + for inpt, otpt in { + 9: 2, + 15: 6, + 100: 292, + }.items(): + self.assertEqual(mc(inpt),otpt) + +unittest.main() -- cgit From 8fc715b18240d9f1d24e4d07bf545dbd7d33aad9 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 2 Sep 2024 23:39:36 +1000 Subject: pwc285 - minor enhancement --- challenge-285/pokgopun/python/ch-1.py | 12 ++++++++---- challenge-285/pokgopun/python/ch-2.py | 5 +---- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'challenge-285/pokgopun/python') diff --git a/challenge-285/pokgopun/python/ch-1.py b/challenge-285/pokgopun/python/ch-1.py index fcebb07e89..5ae0781134 100644 --- a/challenge-285/pokgopun/python/ch-1.py +++ b/challenge-285/pokgopun/python/ch-1.py @@ -31,10 +31,14 @@ Task 2: Making Change ### solution by pokgopun@gmail.com def nc(routes: tuple): - srcs = tuple(e[0] for e in routes) - for src, dst in routes: - if dst not in srcs: - return dst + dct = dict() + for src,dst in routes: + if dct.get(src,True): + dct[src] = False + dct.setdefault(dst,True) + for k,v in dct.items(): + if v: + return k return None import unittest diff --git a/challenge-285/pokgopun/python/ch-2.py b/challenge-285/pokgopun/python/ch-2.py index 813b8cf378..54396ce3f7 100644 --- a/challenge-285/pokgopun/python/ch-2.py +++ b/challenge-285/pokgopun/python/ch-2.py @@ -55,10 +55,7 @@ def mc(amount: int): for a50 in range(0,amount+1,50): for a25 in range(0,amount-a50+1,25): for a10 in range(0,amount-a50-a25+1,10): - for a5 in range(0,amount-a50-a25-a10+1,5): - for a1 in range(0,amount-a50-a25-a10-a5+1,1): - if a50 + a25 + a10 + a5 + a1 == amount: - c += 1 + c += (amount-a50-a25-a10)//5 + 1 return c import unittest -- cgit