aboutsummaryrefslogtreecommitdiff
path: root/challenge-236
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-09-26 10:58:18 +0100
committerGitHub <noreply@github.com>2023-09-26 10:58:18 +0100
commit5e568a2e617979c516f302f10111a1df64460f9f (patch)
tree48072ebc0515a88cb58d8432547f9016631a4fe9 /challenge-236
parent9d294363046bc3197d2ad769473630aa0e72a933 (diff)
parent513c5dc73b6f666d3474fbb3c2aca07480612bbc (diff)
downloadperlweeklychallenge-club-5e568a2e617979c516f302f10111a1df64460f9f.tar.gz
perlweeklychallenge-club-5e568a2e617979c516f302f10111a1df64460f9f.tar.bz2
perlweeklychallenge-club-5e568a2e617979c516f302f10111a1df64460f9f.zip
Merge pull request #8769 from pokgopun/pwc236
pwc236 soultion
Diffstat (limited to 'challenge-236')
-rw-r--r--challenge-236/pokgopun/go/ch-1.go110
-rw-r--r--challenge-236/pokgopun/go/ch-2.go92
-rw-r--r--challenge-236/pokgopun/python/ch-1.py77
-rw-r--r--challenge-236/pokgopun/python/ch-2.py80
4 files changed, 359 insertions, 0 deletions
diff --git a/challenge-236/pokgopun/go/ch-1.go b/challenge-236/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..c2f503dee3
--- /dev/null
+++ b/challenge-236/pokgopun/go/ch-1.go
@@ -0,0 +1,110 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-236/
+/*#
+
+Task 1: Exact Change
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are asked to sell juice each costs $5. You are given an array of
+ bills. You can only sell ONE juice to each customer but make sure you
+ return exact change back. You only have $5, $10 and $20 notes. You do
+ not have any change in hand at first.
+
+ Write a script to find out if it is possible to sell to each customers
+ with correct change.
+
+Example 1
+
+Input: @bills = (5, 5, 5, 10, 20)
+Output: true
+
+From the first 3 customers, we collect three $5 bills in order.
+From the fourth customer, we collect a $10 bill and give back a $5.
+From the fifth customer, we give a $10 bill and a $5 bill.
+Since all customers got correct change, we output true.
+
+Example 2
+
+Input: @bills = (5, 5, 10, 10, 20)
+Output: false
+
+From the first two customers in order, we collect two $5 bills.
+For the next two customers in order, we collect a $10 bill and give back a $5 bi
+ll.
+For the last customer, we can not give the change of $15 back because we only ha
+ve two $10 bills.
+Since not every customer received the correct change, the answer is false.
+
+Example 3
+
+Input: @bills = (5, 5, 5, 20)
+Output: true
+
+Task 2: Array Loops
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "maps"
+)
+
+type cash struct {
+ objCnt map[int]int
+ objs []int
+}
+
+func newCash() (c cash) {
+ c.objs = []int{20, 10, 5}
+ c.objCnt = make(map[int]int)
+ return c
+}
+
+func (c *cash) add(m map[int]int) {
+ for _, v := range c.objs {
+ c.objCnt[v] += m[v]
+ }
+}
+
+func (c *cash) deduct(n int) bool {
+ defer maps.DeleteFunc(c.objCnt, func(k, v int) bool {
+ return k < 0
+ })
+ for _, v := range c.objs {
+ c.objCnt[-v] = min(n/v, c.objCnt[v])
+ n -= v * c.objCnt[-v]
+ }
+ if n != 0 {
+ return false
+ }
+ for _, v := range c.objs {
+ c.objCnt[v] -= c.objCnt[-v]
+ }
+ return true
+}
+
+func sellable(s []int) bool {
+ c := newCash()
+ for _, v := range s {
+ c.add(map[int]int{v: 1})
+ if c.deduct(v-5) == false {
+ return false
+ }
+ }
+ return true
+}
+func main() {
+ for _, data := range []struct {
+ input []int
+ output bool
+ }{
+ {[]int{5, 5, 5, 10, 20}, true},
+ {[]int{5, 5, 10, 10, 20}, false},
+ {[]int{5, 5, 5, 20}, true},
+ } {
+ fmt.Println(sellable(data.input) == data.output)
+ }
+}
diff --git a/challenge-236/pokgopun/go/ch-2.go b/challenge-236/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..2c7302c95a
--- /dev/null
+++ b/challenge-236/pokgopun/go/ch-2.go
@@ -0,0 +1,92 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-236/
+/*#
+
+Task 2: Array Loops
+
+Submitted by: [46]Mark Anderson
+
+ You are given an array of unique integers.
+
+ Write a script to determine how many loops are in the given array.
+
+ To determine a loop: Start at an index and take the number at
+ array[index] and then proceed to that index and continue this until
+ you end up at the starting index.
+
+Example 1
+
+Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10)
+Output: 3
+
+To determine the 1st loop, start at index 0, the number at that index is 4, proc
+eed to index 4, the number at that index is 15, proceed to index 15 and so on un
+til you're back at index 0.
+
+Loops are as below:
+[4 15 1 6 13 5 0]
+[3 8 7 18 9 16 12 17 2]
+[14 11 19 10]
+
+Example 2
+
+Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19)
+Output: 6
+
+Loops are as below:
+[0]
+[1]
+[13 9 14 17 18 15 5 8 2]
+[7 11 4 6 10 16 3]
+[12]
+[19]
+
+Example 3
+
+Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17)
+Output: 1
+
+Loop is as below:
+[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 1st October
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import "fmt"
+
+func main() {
+ for _, data := range []struct {
+ input []int
+ output int
+ }{
+ {[]int{4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10}, 3},
+ {[]int{0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19}, 6},
+ {[]int{9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17}, 1},
+ } {
+ fmt.Println(countLoop(data.input) == data.output)
+ }
+}
+
+func countLoop(s []int) (count int) {
+ for i := range s {
+ j := i
+ for {
+ if j < i {
+ break
+ }
+ j = s[j]
+ if j == i {
+ count++
+ break
+ }
+ }
+ }
+ return count
+}
diff --git a/challenge-236/pokgopun/python/ch-1.py b/challenge-236/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..18cd3ead0a
--- /dev/null
+++ b/challenge-236/pokgopun/python/ch-1.py
@@ -0,0 +1,77 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-236/
+"""
+
+Task 1: Exact Change
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are asked to sell juice each costs $5. You are given an array of
+ bills. You can only sell ONE juice to each customer but make sure you
+ return exact change back. You only have $5, $10 and $20 notes. You do
+ not have any change in hand at first.
+
+ Write a script to find out if it is possible to sell to each customers
+ with correct change.
+
+Example 1
+
+Input: @bills = (5, 5, 5, 10, 20)
+Output: true
+
+From the first 3 customers, we collect three $5 bills in order.
+From the fourth customer, we collect a $10 bill and give back a $5.
+From the fifth customer, we give a $10 bill and a $5 bill.
+Since all customers got correct change, we output true.
+
+Example 2
+
+Input: @bills = (5, 5, 10, 10, 20)
+Output: false
+
+From the first two customers in order, we collect two $5 bills.
+For the next two customers in order, we collect a $10 bill and give back a $5 bi
+ll.
+For the last customer, we can not give the change of $15 back because we only ha
+ve two $10 bills.
+Since not every customer received the correct change, the answer is false.
+
+Example 3
+
+Input: @bills = (5, 5, 5, 20)
+Output: true
+
+Task 2: Array Loops
+"""
+### solution by pokgopun@gmail.com
+
+def sellable(pays):
+ bills = dict.fromkeys((20, 10, 5), 0)
+ #print(bills)
+ for pay in pays:
+ #print(f'pay = {pay}')
+ if pay not in bills: return False
+ bills[pay] += 1
+ pay -= 5
+ for bill in bills:
+ if bill > pay: continue
+ if bills[bill] > 0:
+ count = pay // bill
+ if count > bills[bill]:
+ pay -= bill*bills[bill]
+ bills[bill] = 0
+ continue
+ else:
+ bills[bill] -= count
+ pay -= bill*count
+ continue
+ #print(bills)
+ if pay > 0: return False
+ return True
+
+for inpt, otpt in {
+ (5, 5, 5, 10, 20): True,
+ (5, 5, 10, 10, 20): False,
+ (5, 5, 5, 20): True,
+ }.items():
+ print(sellable(inpt)==otpt)
diff --git a/challenge-236/pokgopun/python/ch-2.py b/challenge-236/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..9740efff95
--- /dev/null
+++ b/challenge-236/pokgopun/python/ch-2.py
@@ -0,0 +1,80 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-236/
+"""
+
+Task 2: Array Loops
+
+Submitted by: [46]Mark Anderson
+
+ You are given an array of unique integers.
+
+ Write a script to determine how many loops are in the given array.
+
+ To determine a loop: Start at an index and take the number at
+ array[index] and then proceed to that index and continue this until
+ you end up at the starting index.
+
+Example 1
+
+Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10)
+Output: 3
+
+To determine the 1st loop, start at index 0, the number at that index is 4, proc
+eed to index 4, the number at that index is 15, proceed to index 15 and so on un
+til you're back at index 0.
+
+Loops are as below:
+[4 15 1 6 13 5 0]
+[3 8 7 18 9 16 12 17 2]
+[14 11 19 10]
+
+Example 2
+
+Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19)
+Output: 6
+
+Loops are as below:
+[0]
+[1]
+[13 9 14 17 18 15 5 8 2]
+[7 11 4 6 10 16 3]
+[12]
+[19]
+
+Example 3
+
+Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17)
+Output: 1
+
+Loop is as below:
+[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 1st October
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def countLoop(tup):
+ count = 0
+ for i in range(len(tup)):
+ j = i
+ #lst = []
+ while True:
+ j = tup[j]
+ #lst.append(j)
+ if j < i: break
+ if j == i:
+ count += 1
+ #print(lst)
+ break
+ return count
+
+for inpt, otpt in {
+ (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10): 3,
+ (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19): 6,
+ (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17): 1,
+ }.items():
+ print(countLoop(inpt)==otpt)