aboutsummaryrefslogtreecommitdiff
path: root/challenge-243/jeanluc2020/python/ch-2.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-2.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-2.py')
-rwxr-xr-xchallenge-243/jeanluc2020/python/ch-2.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-243/jeanluc2020/python/ch-2.py b/challenge-243/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..7c49e6d055
--- /dev/null
+++ b/challenge-243/jeanluc2020/python/ch-2.py
@@ -0,0 +1,51 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK2
+#
+# Task 2: Floor Sum
+# =================
+#
+# You are given an array of positive integers (>=1).
+#
+# Write a script to return the sum of floor(nums[i] / nums[j]) where
+# 0 <= i,j < nums.length. The floor() function returns the integer part
+# of the division.
+#
+## Example 1
+##
+## Input: @nums = (2, 5, 9)
+## Output: 10
+##
+## floor(2 / 5) = 0
+## floor(2 / 9) = 0
+## floor(5 / 9) = 0
+## floor(2 / 2) = 1
+## floor(5 / 5) = 1
+## floor(9 / 9) = 1
+## floor(5 / 2) = 2
+## floor(9 / 2) = 4
+## floor(9 / 5) = 1
+#
+## Example 2
+##
+## Input: @nums = (7, 7, 7, 7, 7, 7, 7)
+## Output: 49
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Sum up the calculated floor(nums[i]/nums[$j]) for all combinations.
+
+def floor_sum(nums: list):
+ result = 0
+ print("Input: (", ", ".join(str(x) for x in nums), ")")
+ for i in range(len(nums)):
+ for j in range(len(nums)):
+ result += int(nums[i] / nums[j])
+ print("Output:", str(result))
+
+floor_sum( [ 2, 5, 9 ] );
+floor_sum( [ 7, 7, 7, 7, 7, 7, 7 ] );
+