aboutsummaryrefslogtreecommitdiff
path: root/challenge-270/pokgopun/python/ch-2.py
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-05-20 19:42:29 +1000
committerMichael Manring <michael@manring>2024-05-20 19:42:29 +1000
commit749da7ab7b6c68de590cec57f44243333f2895d9 (patch)
treecb1d3970b18f19bcfa0f4b6b512db27ff329fcf6 /challenge-270/pokgopun/python/ch-2.py
parent1a44e464db15d3896b42048a43320150afeb9d4e (diff)
downloadperlweeklychallenge-club-749da7ab7b6c68de590cec57f44243333f2895d9.tar.gz
perlweeklychallenge-club-749da7ab7b6c68de590cec57f44243333f2895d9.tar.bz2
perlweeklychallenge-club-749da7ab7b6c68de590cec57f44243333f2895d9.zip
pwc270 solution in python
Diffstat (limited to 'challenge-270/pokgopun/python/ch-2.py')
-rw-r--r--challenge-270/pokgopun/python/ch-2.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/challenge-270/pokgopun/python/ch-2.py b/challenge-270/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..ac6e3bbcb0
--- /dev/null
+++ b/challenge-270/pokgopun/python/ch-2.py
@@ -0,0 +1,105 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-270/
+"""
+
+Task 2: Distribute Elements
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are give an array of integers, @ints and two integers, $x and $y.
+
+ Write a script to execute one of the two options:
+Level 1:
+Pick an index i of the given array and do $ints[i] += 1
+
+Level 2:
+Pick two different indices i,j and do $ints[i] +=1 and $ints[j] += 1.
+
+ You are allowed to perform as many levels as you want to make every
+ elements in the given array equal. There is cost attach for each level,
+ for Level 1, the cost is $x and $y for Level 2.
+
+ In the end return the minimum cost to get the work done.
+
+Example 1
+
+Input: @ints = (4, 1), $x = 3 and $y = 2
+Output: 9
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 2)
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 3)
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 4)
+
+We perforned operation Level 1, 3 times.
+So the total cost would be 3 x $x => 3 x 3 => 9
+
+Example 2
+
+Input: @ints = (2, 3, 3, 3, 5), $x = 2 and $y = 1
+Output: 6
+
+Level 2: i=0, j=1, so $ints[0] += 1 and $ints[1] += 1
+@ints = (3, 4, 3, 3, 5)
+
+Level 2: i=0, j=2, so $ints[0] += 1 and $ints[2] += 1
+@ints = (4, 4, 4, 3, 5)
+
+Level 2: i=0, j=3, so $ints[0] += 1 and $ints[3] += 1
+@ints = (5, 4, 4, 4, 5)
+
+Level 2: i=1, j=2, so $ints[1] += 1 and $ints[2] += 1
+@ints = (5, 5, 5, 4, 5)
+
+Level 1: i=3, so $ints[3] += 1
+@ints = (5, 5, 5, 5, 5)
+
+We perforned operation Level 1, 1 time and Level 2, 4 times.
+So the total cost would be (1 x $x) + (3 x $y) => (1 x 2) + (4 x 1) => 6
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 26th May 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def distElem(ints: list, x: int, y: int):
+ if len(set(ints)) < 2:
+ return 0
+ ints.sort()
+ mx = ints[-1]
+ c = 0
+ while True:
+ ints = ints[:ints.index(mx)]
+ l = len(ints)
+ if l == 0:
+ break
+ if l == 1 or 2*x < y:
+ while ints[-1] < mx:
+ for i in range(l):
+ ints[i] += 1
+ c += x
+ else:
+ while ints[-1] < mx:
+ for i in range(-2,0):
+ ints[i] += 1
+ c += y
+ return c
+
+import unittest
+
+class TestDistElem(unittest.TestCase):
+ def test(self):
+ for cost, (ints, x, y) in {
+ 9: ([4, 1], 3, 2),
+ 6: ([2, 3, 3, 3, 5], 2, 1),
+ }.items():
+ self.assertEqual(distElem(ints,x,y),cost)
+
+unittest.main()