diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2023-12-27 14:57:27 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2023-12-27 14:57:27 -0600 |
| commit | f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b (patch) | |
| tree | cb922fb2f44a7ec7bd10cd13b3db9eb2d4fd14b5 /challenge-249/pokgopun/python/ch-1.py | |
| parent | 23f378718ed93b9b8d9e51fe18f07a55f61dc9c1 (diff) | |
| parent | 4ece120b2f4c7b25b00eaad66524fb5d99fa58c3 (diff) | |
| download | perlweeklychallenge-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/pokgopun/python/ch-1.py')
| -rw-r--r-- | challenge-249/pokgopun/python/ch-1.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-249/pokgopun/python/ch-1.py b/challenge-249/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..cef9601557 --- /dev/null +++ b/challenge-249/pokgopun/python/ch-1.py @@ -0,0 +1,57 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-249/ +""" + +Task 1: Shortest Distance + +Submitted by: [66]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers with even number of elements. + + Write a script to divide the given array into equal pairs such that: +a) Each element belongs to exactly one pair. +b) The elements present in a pair are equal. + +Example 1 + +Input: @ints = (3, 2, 3, 2, 2, 2) +Output: (2, 2), (3, 3), (2, 2) + +There are 6 elements in @ints. +They should be divided into 6 / 2 = 3 pairs. +@ints is divided into the pairs (2, 2), (3, 3), and (2, 2) satisfying all the co +nditions. + +Example 2 + +Input: @ints = (1, 2, 3, 4) +Output: () + +There is no way to divide @ints 2 pairs such that the pairs satisfy every condit +ion. + +Task 2: DI String Match +""" +### solution by pokgopun@gmail.com + +from itertools import chain + +def equalPairs(tup: tuple): + return tuple( + chain.from_iterable( + ( (e,e) for c in range(tup.count(e)//2 ) ) + for e in set(tup) + ) + ) + +import unittest + +class TestEqualPairs(unittest.TestCase): + def test1(self): + for inpt,otpt in { + (3, 2, 3, 2, 2, 2): ((2, 2), (3, 3), (2, 2)), + (1, 2, 3, 4): (), + }.items(): + self.assertEqual(sorted(equalPairs(inpt)),sorted(otpt)) + +unittest.main() |
