diff options
| author | Michael Firkins <michael@firkins> | 2023-11-02 18:01:55 +1100 |
|---|---|---|
| committer | Michael Firkins <michael@firkins> | 2023-11-02 18:01:55 +1100 |
| commit | d44f394b07c8ad6cc7eb8811aed4a22aa0486fa2 (patch) | |
| tree | 2acdf334c0219c741db689264772860af384b932 /challenge-241/pokgopun/python/ch-2.py | |
| parent | 4080d3b82992cbcb1745f45428669413cc80861d (diff) | |
| download | perlweeklychallenge-club-d44f394b07c8ad6cc7eb8811aed4a22aa0486fa2.tar.gz perlweeklychallenge-club-d44f394b07c8ad6cc7eb8811aed4a22aa0486fa2.tar.bz2 perlweeklychallenge-club-d44f394b07c8ad6cc7eb8811aed4a22aa0486fa2.zip | |
pwc241 solution in python
Diffstat (limited to 'challenge-241/pokgopun/python/ch-2.py')
| -rw-r--r-- | challenge-241/pokgopun/python/ch-2.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-241/pokgopun/python/ch-2.py b/challenge-241/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..dbaa1480d2 --- /dev/null +++ b/challenge-241/pokgopun/python/ch-2.py @@ -0,0 +1,47 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-241/ +""" + +Task 2: Prime Order + +Submitted by: [43]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of unique positive integers greater than 2. + + Write a script to sort them in ascending order of the count of their + prime factors, tie-breaking by ascending value. + +Example 1 + +Input: @int = (11, 8, 27, 4) +Output: (11, 4, 8, 27)) + +Prime factors of 11 => 11 +Prime factors of 4 => 2, 2 +Prime factors of 8 => 2, 2, 2 +Prime factors of 27 => 3, 3, 3 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 5th November + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from sympy.ntheory import factorint + +def sortByFactorCount(tup: tuple): + return tuple( + sorted( + sorted(tup), key = lambda x: sum(factorint(x).values()) + ) + ) + +for inpt,otpt in { + (11, 8, 27, 4): (11, 4, 8, 27), + (11, 27, 8, 4): (11, 4, 8, 27), + }.items(): + print(sortByFactorCount(inpt)==otpt) + |
