aboutsummaryrefslogtreecommitdiff
path: root/challenge-243/jeanluc2020/python/ch-1.py
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-11-20 15:40:49 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-11-20 15:40:49 +0800
commit72dc5412e640e26601ffdebc4f0247db4c4bc7fe (patch)
treed405aa98925dfed0b745e5ec331e9498005b1de7 /challenge-243/jeanluc2020/python/ch-1.py
parent35d79358f3d12607da66ffefefed5e93c469d396 (diff)
parentfda51162ad0e1e8b4489fd2c73ef7dfdc8f0df5e (diff)
downloadperlweeklychallenge-club-72dc5412e640e26601ffdebc4f0247db4c4bc7fe.tar.gz
perlweeklychallenge-club-72dc5412e640e26601ffdebc4f0247db4c4bc7fe.tar.bz2
perlweeklychallenge-club-72dc5412e640e26601ffdebc4f0247db4c4bc7fe.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-243/jeanluc2020/python/ch-1.py')
-rwxr-xr-xchallenge-243/jeanluc2020/python/ch-1.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-243/jeanluc2020/python/ch-1.py b/challenge-243/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..c557de5347
--- /dev/null
+++ b/challenge-243/jeanluc2020/python/ch-1.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK1
+#
+# Task 1: Reverse Pairs
+# =====================
+#
+# You are given an array of integers.
+#
+# Write a script to return the number of reverse pairs in the given
+# array.
+#
+# A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length
+# and b) nums[i] > 2 * nums[j].
+#
+## Example 1
+##
+## Input: @nums = (1, 3, 2, 3, 1)
+## Output: 2
+##
+## (1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1
+## (3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1
+#
+## Example 2
+##
+## Input: @nums = (2, 4, 3, 5, 1)
+## Output: 3
+##
+## (1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1
+## (2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1
+## (3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Walk the array twice (once from 0, once from the first index + 1)
+# Check the condition in each case, counting the occurrences where
+# the condition holds true.
+
+def reverse_pairs(nums: list):
+ print("Input: (", ", ".join(str(x) for x in nums), ")")
+ result = 0
+ for i in range(len(nums)):
+ for j in range(i+1, len(nums)):
+ if nums[i] > 2 * nums[j]:
+ result += 1
+ print("Output:", str(result))
+
+reverse_pairs( [ 1, 3, 2, 3, 1 ] );
+reverse_pairs( [ 2, 4, 3, 5, 1 ] );
+