aboutsummaryrefslogtreecommitdiff
path: root/challenge-237/jeanluc2020/python/ch-2.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-03 12:20:37 +0100
committerGitHub <noreply@github.com>2023-10-03 12:20:37 +0100
commit100b1847395389d1f7fcc80957f463208f5f25e1 (patch)
tree5f2f09d375238338c566d5cc80e568b4df7f4a7b /challenge-237/jeanluc2020/python/ch-2.py
parent67d6c85ec412b5c79e17237f421d85e033ddf871 (diff)
parent9aefa08d8034cd60341e0b551089ec34dda7fdca (diff)
downloadperlweeklychallenge-club-100b1847395389d1f7fcc80957f463208f5f25e1.tar.gz
perlweeklychallenge-club-100b1847395389d1f7fcc80957f463208f5f25e1.tar.bz2
perlweeklychallenge-club-100b1847395389d1f7fcc80957f463208f5f25e1.zip
Merge pull request #8806 from jeanluc2020/jeanluc-237
Add solution 237
Diffstat (limited to 'challenge-237/jeanluc2020/python/ch-2.py')
-rwxr-xr-xchallenge-237/jeanluc2020/python/ch-2.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-237/jeanluc2020/python/ch-2.py b/challenge-237/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..0125a8b508
--- /dev/null
+++ b/challenge-237/jeanluc2020/python/ch-2.py
@@ -0,0 +1,65 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-237/#TASK2
+#
+# Task 2: Maximise Greatness
+# ==========================
+#
+# You are given an array of integers.
+#
+# Write a script to permute the given 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 below:
+## 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]
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Calculate all permutations and check the greatness for each permutation
+# Keep the maximum
+
+from itertools import permutations
+
+def greatness(nums: list, perm: list) -> int:
+ greatness = 0
+ indices = list(range(len(nums)))
+ for i in indices:
+ if nums[i] < perm[i]:
+ greatness+=1
+ return greatness
+
+def maximise_greatness(ints: list):
+ print("Input: (" + ', '.join(str(x) for x in ints) + ")")
+ max = 0
+ for perm in permutations(ints):
+ current = greatness(ints, perm)
+ if current > max:
+ max = current
+ print(f"Output: {max}")
+
+
+maximise_greatness([1, 3, 5, 2, 1, 3, 1])
+maximise_greatness([1, 2, 3, 4])