diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-17 11:09:21 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-17 11:09:21 +0000 |
| commit | 01db8d4cbc09cc29293da7d5124f8e3e43eb1f2c (patch) | |
| tree | 2dce36121e6ec2c4e096b414dc1b2b7a13f50dc6 /challenge-252/packy-anderson/python | |
| parent | 857d573334deaa24866cb695f1e3758a85c0b9d9 (diff) | |
| parent | 29fc5f28a3b1b684f8dcf1dd95af51da468dd0ad (diff) | |
| download | perlweeklychallenge-club-01db8d4cbc09cc29293da7d5124f8e3e43eb1f2c.tar.gz perlweeklychallenge-club-01db8d4cbc09cc29293da7d5124f8e3e43eb1f2c.tar.bz2 perlweeklychallenge-club-01db8d4cbc09cc29293da7d5124f8e3e43eb1f2c.zip | |
Merge pull request #9419 from packy/master
Challenge 252 solutions by Packy Anderson
Diffstat (limited to 'challenge-252/packy-anderson/python')
| -rwxr-xr-x | challenge-252/packy-anderson/python/ch-1.py | 69 | ||||
| -rwxr-xr-x | challenge-252/packy-anderson/python/ch-2.py | 33 |
2 files changed, 102 insertions, 0 deletions
diff --git a/challenge-252/packy-anderson/python/ch-1.py b/challenge-252/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..16a9fd6188 --- /dev/null +++ b/challenge-252/packy-anderson/python/ch-1.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def specialElementIndices(n): + return list( filter(lambda x: n % x == 0, range(1, n+1)) ) + +def english_list (strlist): + # given a list, join it in a way that makes sense + # to english speakers + last = strlist.pop(-1) # last element in array + if (len(strlist) == 0): + # using an array in a scalar context returns + # the number of elements in the array + + # there was only one element in the list + return last + + joined = ',\n'.join(strlist) + if (len(strlist) > 1): + # if there's more than element, add an Oxford comma + joined += ',' + + return f'{joined} and\n{last}' + +def specialNumberSquareSum(ints): + n = len(ints) + + # find the list of indices for "special" numbers + specialIndices = specialElementIndices(n) + count = len(specialIndices) + explain_list = [ + f"$ints[{x}] since {x} divides {n}" + for x in specialIndices + ] + explain = ( + "There are exactly $count special elements " + + "in the given array:\n" + english_list(explain_list) + ) + + # find the special numbers themselves + special = [ ints[x - 1] for x in specialIndices ] + + # find the sum of the squares + sumval = sum([ x ** 2 for x in special ]) + + explain += '\nHence, the sum of the squares of all special ' + explain += 'elements of given array:\n' + explain += ' + '.join(map(lambda x: f'{x} * {x}', special)) + explain += f' = {sumval}' + + return ( + sumval, + explain + ) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + (sumval, explain) = specialNumberSquareSum(ints) + print(f'Output: {sumval}') + print('') + print(explain) + +print('Example 1:') +solution([1, 2, 3, 4]) + +print('\nExample 2:') +solution([2, 7, 1, 19, 18, 3])
\ No newline at end of file diff --git a/challenge-252/packy-anderson/python/ch-2.py b/challenge-252/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..b68a6502c2 --- /dev/null +++ b/challenge-252/packy-anderson/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def uniqueSumZero(n): + zero_sum_list = [] + x = 1 + while n > 0: + if (n % 2 == 1): # n is odd + zero_sum_list.append(0) + n -= 1 + else: # n is even + zero_sum_list.append(x * -1) + zero_sum_list.append(x) + x += 1 + n -= 2 + zero_sum_list.sort() + return zero_sum_list + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(n): + print(f'Input: $n = {n}') + zero_sum_list = uniqueSumZero(n) + print(f'Output: ({comma_join(zero_sum_list)})') + +print('Example 1:') +solution(5) + +print('\nExample 2:') +solution(3) + +print('\nExample 3:') +solution(1)
\ No newline at end of file |
