From 5b21193086ff20b64b19f33112cd57a2a3a0077b Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Tue, 20 Aug 2024 02:12:17 -0400 Subject: Challenge 283 solutions by Packy Anderson * Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir 1 Blog post --- challenge-283/packy-anderson/python/ch-2.py | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 challenge-283/packy-anderson/python/ch-2.py (limited to 'challenge-283/packy-anderson/python/ch-2.py') diff --git a/challenge-283/packy-anderson/python/ch-2.py b/challenge-283/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..e8ccd8b72c --- /dev/null +++ b/challenge-283/packy-anderson/python/ch-2.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +from collections import Counter + +def digitCountValue(ints): + bag = Counter(ints) # count occurrences + explain = [] + passes = True + for digit in range(len(ints)): + times = ints[digit] + occurs = bag[digit] + otimes = ( + "0 times" if occurs == 0 else + f"{occurs} times" if occurs >= 2 else + "1 time" + ) + if times == occurs: + explain.append( + f"$ints[{digit}] = {times}, the digit {digit} " + + f"occurs {otimes}" + ) + else: + passes = False + ttimes = ( + "0 times" if times == 0 else + f"{times} times" if times >= 2 else + "1 time" + ) + explain.append( + f"$ints[{digit}] = {times}, the digit {digit} " + + f"occurs {otimes} rather than {ttimes}" + ) + return passes, "\n".join(explain) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + passes, explain = digitCountValue(ints) + print(f'Output: {passes}\n\n{explain}') + +print('Example 1:') +solution([1, 2, 1, 0]) + +print('\nExample 2:') +solution([0, 3, 0]) + +print('\nExample 3:') +solution([0, 1, 2, 2]) -- cgit