aboutsummaryrefslogtreecommitdiff
path: root/challenge-237/pokgopun/python/ch-2.py
diff options
context:
space:
mode:
authorMichael Firkins <michael@firkins>2023-10-02 21:05:31 +1100
committerMichael Firkins <michael@firkins>2023-10-03 13:47:59 +1100
commit98b832f1ff2919defd3e4fa528b60362a6caf0fc (patch)
treef729edb6c340ce902c2a490634ff932fc874a124 /challenge-237/pokgopun/python/ch-2.py
parent9a50910d2d97f98fc942e1a5275016a462be8abb (diff)
downloadperlweeklychallenge-club-98b832f1ff2919defd3e4fa528b60362a6caf0fc.tar.gz
perlweeklychallenge-club-98b832f1ff2919defd3e4fa528b60362a6caf0fc.tar.bz2
perlweeklychallenge-club-98b832f1ff2919defd3e4fa528b60362a6caf0fc.zip
pwc237 solution in python
Diffstat (limited to 'challenge-237/pokgopun/python/ch-2.py')
-rw-r--r--challenge-237/pokgopun/python/ch-2.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-237/pokgopun/python/ch-2.py b/challenge-237/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..7f52133712
--- /dev/null
+++ b/challenge-237/pokgopun/python/ch-2.py
@@ -0,0 +1,62 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-237/
+"""
+
+Task 2: Maximise Greatness
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to permute the give array such that you get the maximum
+ possible greatness.
+
+ To determine greatness, nums[i] < perm[i] where 0 <= i < nums.length
+
+Example 1
+
+Input: @nums = (1, 3, 5, 2, 1, 3, 1)
+Output: 4
+
+One possible permutation: (2, 5, 1, 3, 3, 1, 1) which returns 4 greatness as bel
+ow:
+nums[0] < perm[0]
+nums[1] < perm[1]
+nums[3] < perm[3]
+nums[4] < perm[4]
+
+Example 2
+
+Input: @ints = (1, 2, 3, 4)
+Output: 3
+
+One possible permutation: (2, 3, 4, 1) which returns 3 greatness as below:
+nums[0] < perm[0]
+nums[1] < perm[1]
+nums[2] < perm[2]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 8th October
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import permutations
+
+def maxGreat(tup):
+ l, m = len(tup), 0
+ for pm in permutations(tup):
+ g = tuple( pm[i] > tup[i] for i in range(l) ).count(True)
+ if m < g : m = g
+ return m
+
+for inpt,otpt in {
+ (1, 3, 5, 2, 1, 3, 1): 4,
+ (1, 2, 3, 4): 3,
+ }.items():
+ print(maxGreat(inpt)==otpt)
+
+