aboutsummaryrefslogtreecommitdiff
path: root/challenge-249/packy-anderson/python
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2023-12-27 14:57:27 -0600
committerLuis Mochan <mochan@fis.unam.mx>2023-12-27 14:57:27 -0600
commitf9c88dbbb1e13bfb63f40d0dd3b8630850655d7b (patch)
treecb922fb2f44a7ec7bd10cd13b3db9eb2d4fd14b5 /challenge-249/packy-anderson/python
parent23f378718ed93b9b8d9e51fe18f07a55f61dc9c1 (diff)
parent4ece120b2f4c7b25b00eaad66524fb5d99fa58c3 (diff)
downloadperlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.tar.gz
perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.tar.bz2
perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.zip
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club into challenges
Diffstat (limited to 'challenge-249/packy-anderson/python')
-rwxr-xr-xchallenge-249/packy-anderson/python/ch-1.py47
-rwxr-xr-xchallenge-249/packy-anderson/python/ch-2.py36
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-249/packy-anderson/python/ch-1.py b/challenge-249/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..228bb47da3
--- /dev/null
+++ b/challenge-249/packy-anderson/python/ch-1.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def equalPairs(nums):
+ pairs = []
+ num_count = Counter()
+ # count how many of each int we have
+ for num in nums:
+ num_count[num] += 1
+
+ # first, make sure we have even numbers of each integer
+ for k, v in dict(num_count).items():
+ if v % 2 == 0: # it's even, we can make pairs
+ continue
+ else:
+ return pairs # we have an odd number, no pairs
+
+ # now make pairs from those integers
+ for k, v in dict(num_count).items():
+ count = v # the values k, v are read-only
+ while count > 0:
+ pairs.append( [k, k] )
+ count -= 2
+
+ return pairs
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(nums):
+ print(f'Input: @ints = ({comma_join(nums)})')
+ pairs = equalPairs(nums)
+ if len(pairs) == 0:
+ print('Output: ()')
+ else:
+ pairs = [ f'({x[0]}, {x[1]})' for x in pairs ]
+ print(f"Output: { ', '.join(pairs) }")
+
+print('Example 1:')
+solution([3, 2, 3, 2, 2, 2])
+
+print('\nExample 2:')
+solution([1, 2, 3, 4])
+
+print('\nExample 3:')
+solution([1, 2, 3, 4, 4, 3, 2, 1]) \ No newline at end of file
diff --git a/challenge-249/packy-anderson/python/ch-2.py b/challenge-249/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..8c3750f195
--- /dev/null
+++ b/challenge-249/packy-anderson/python/ch-2.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+def diStringMatch(str):
+ permutation = []
+ # first, generate the list of integers
+ # we're making permutations of
+ nums = list(range(len(str)+1))
+ # now let's generate our permutation
+ for c in str:
+ if c == 'D':
+ # take the largest number available
+ permutation.append( nums.pop(-1) )
+ else:
+ # take the smallest number available
+ permutation.append( nums.pop(0) )
+ # add last remaining number
+ permutation.append( nums[0] )
+
+ return permutation
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(str):
+ print(f'Input: $str = ({comma_join(str)})')
+ permutations = diStringMatch(str)
+ print(f'Output: ({comma_join(permutations)})')
+
+print('Example 1:')
+solution("IDID")
+
+print('\nExample 2:')
+solution("III")
+
+print('\nExample 3:')
+solution("DDI") \ No newline at end of file