diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-30 10:14:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-30 10:14:57 +0100 |
| commit | 895cab0470802e456ceb7d29b2960cf7f36b7c8d (patch) | |
| tree | 8d2f76e387cb9e26ce5f02c87bb1492c4e60b370 | |
| parent | 9f4f6cad2cb6a6eef4d8a0ac9f9b3454addf1597 (diff) | |
| parent | 09be60a52dbe06ea28f8bb835b31e37d3ea93fa5 (diff) | |
| download | perlweeklychallenge-club-895cab0470802e456ceb7d29b2960cf7f36b7c8d.tar.gz perlweeklychallenge-club-895cab0470802e456ceb7d29b2960cf7f36b7c8d.tar.bz2 perlweeklychallenge-club-895cab0470802e456ceb7d29b2960cf7f36b7c8d.zip | |
Merge pull request #8776 from oWnOIzRi/week236
add solutions week 236 in python
| -rw-r--r-- | challenge-236/steven-wilson/python/ch-01.py | 56 | ||||
| -rw-r--r-- | challenge-236/steven-wilson/python/ch-02.py | 34 |
2 files changed, 90 insertions, 0 deletions
diff --git a/challenge-236/steven-wilson/python/ch-01.py b/challenge-236/steven-wilson/python/ch-01.py new file mode 100644 index 0000000000..8bea4269ad --- /dev/null +++ b/challenge-236/steven-wilson/python/ch-01.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +JUICE_PRICE = 5 + + +def correct_change_to_each(transactions): + """ Can correct change be given to each customer + >>> correct_change_to_each([5, 5, 5, 10, 20]) + True + >>> correct_change_to_each([5, 5, 10, 10, 20]) + False + >>> correct_change_to_each([5, 5, 5, 20]) + True + """ + in_hand = [] + for transaction in transactions: + if transaction > JUICE_PRICE: + change = transaction - JUICE_PRICE + bills = correct_change(change, in_hand) + if bills == []: + return False + else: + for bill in bills: + in_hand.remove(bill) + in_hand.append(transaction) + return True + + +def correct_change(change, bills): + """ Returns the bills required to give correct change or empty list + if not possible + >>> correct_change(5, [5, 5, 5]) + [5] + >>> correct_change(15, [5, 5, 10]) + [5, 10] + >>> correct_change(15, [10, 10]) + [] + >>> correct_change(15, [5, 5, 5]) + [5, 5, 5] + >>> correct_change(10, []) + [] + """ + give = [] + bills.sort(reverse=True) + for bill in bills: + if (change - bill + sum(give)) >= 0: + give.append(bill) + if (sum(give) == change): + return sorted(give) + return [] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/challenge-236/steven-wilson/python/ch-02.py b/challenge-236/steven-wilson/python/ch-02.py new file mode 100644 index 0000000000..f854671e81 --- /dev/null +++ b/challenge-236/steven-wilson/python/ch-02.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + + +def array_loops(elements): + ''' determine how many loops are in the given array + >>> array_loops([4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10]) + 3 + >>> array_loops([0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19]) + 6 + >>> array_loops([9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]) + 1 + ''' + loops = [] + seen = [] + for index, value in enumerate(elements): + if index in seen: + continue + start = index + loop = [] + index = elements[index] + while(start != index): + loop.append(index) + seen.append(index) + index = elements[index] + loop.append(start) + seen.append(start) + loops.append(loop) + return len(loops) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() |
