diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-08-06 20:07:18 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-06 20:07:18 +0100 |
| commit | 2c2a625d442d2ad347b3384b970f9a0f305b298c (patch) | |
| tree | f9f3fb91892c89bb7061a28c0dc446b709da0e69 | |
| parent | 2a5c3638192a903ede793501fa166ad32432fd3b (diff) | |
| parent | abe5b1817a10ba5fbb9be518ec1cb839fa834f64 (diff) | |
| download | perlweeklychallenge-club-2c2a625d442d2ad347b3384b970f9a0f305b298c.tar.gz perlweeklychallenge-club-2c2a625d442d2ad347b3384b970f9a0f305b298c.tar.bz2 perlweeklychallenge-club-2c2a625d442d2ad347b3384b970f9a0f305b298c.zip | |
Merge pull request #8501 from pokgopun/pwc228
pwc228 solution in python
| -rw-r--r-- | challenge-228/pokgopun/go/ch-1.go | 61 | ||||
| -rw-r--r-- | challenge-228/pokgopun/go/ch-2.go | 71 | ||||
| -rw-r--r-- | challenge-228/pokgopun/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-228/pokgopun/python/ch-2.py | 39 |
4 files changed, 196 insertions, 0 deletions
diff --git a/challenge-228/pokgopun/go/ch-1.go b/challenge-228/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..d6e5868e85 --- /dev/null +++ b/challenge-228/pokgopun/go/ch-1.go @@ -0,0 +1,61 @@ +// https://theweeklychallenge.org/blog/perl-weekly-challenge-228/ +/* +Task 1: Unique Sum +Submitted by: Mohammad S Anwar +You are given an array of integers. + +Write a script to find out the sum of unique elements in the given array. + +Example 1 +Input: @int = (2, 1, 3, 2) +Output: 4 + +In the given array we have 2 unique elements (1, 3). +Example 2 +Input: @int = (1, 1, 1, 1) +Output: 0 + +In the given array no unique element found. +Example 3 +Input: @int = (2, 1, 3, 4) +Output: 10 + +In the given array every element is unique. +*/ + +package main + +import "fmt" + +func main() { + var su SumUniq + for _, data := range []struct { + input []int + output int + }{ + {[]int{2, 1, 3, 2}, 4}, + {[]int{1, 1, 1, 1}, 0}, + {[]int{2, 1, 3, 4}, 10}, + } { + fmt.Println(su.Cal(data.input) == data.output) + } +} + +type SumUniq struct { + m map[int]int + k, v int + val int +} + +func (su SumUniq) Cal(s []int) int { + su.m = make(map[int]int, len(s)) + for _, su.v = range s { + su.m[su.v]++ + } + for su.k, su.v = range su.m { + if su.v == 1 { + su.val += su.k + } + } + return su.val +} diff --git a/challenge-228/pokgopun/go/ch-2.go b/challenge-228/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9ba2668ba4 --- /dev/null +++ b/challenge-228/pokgopun/go/ch-2.go @@ -0,0 +1,71 @@ +// https://theweeklychallenge.org/blog/perl-weekly-challenge-228/ +/* +Task 2: Empty Array +Submitted by: Mohammad S Anwar +You are given an array of integers in which all elements are unique. + +Write a script to perform the following operations until the array is empty and return the total count of operations. + + +If the first element is the smallest then remove it otherwise move it to the end. + +Example 1 +Input: @int = (3, 4, 2) +Ouput: 5 + +Operation 1: move 3 to the end: (4, 2, 3) +Operation 2: move 4 to the end: (2, 3, 4) +Operation 3: remove element 2: (3, 4) +Operation 4: remove element 3: (4) +Operation 5: remove element 4: () +Example 2 +Input: @int = (1, 2, 3) +Ouput: 3 + +Operation 1: remove element 1: (2, 3) +Operation 2: remove element 2: (3) +Operation 3: remove element 3: () +*/ + +package main + +import "fmt" + +func main() { + for _, data := range []struct { + input []int + output int + }{ + {[]int{3, 4, 2}, 5}, + {[]int{1, 2, 3}, 3}, + } { + fmt.Println(CountEmptyOp(data.input).Count() == data.output) + } +} + +type CountEmptyOp []int + +func (ceo CountEmptyOp) min() int { + if len(ceo) == 0 { + return 0 + } + min := ceo[0] + for _, v := range ceo[1:] { + if v < min { + min = v + } + } + return min +} + +func (ceo CountEmptyOp) Count() int { + count := 0 + for len(ceo) > 0 { + count += 1 + if ceo[0] != ceo.min() { + ceo = append(ceo, ceo[0]) + } + ceo = ceo[1:] + } + return count +} diff --git a/challenge-228/pokgopun/python/ch-1.py b/challenge-228/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..c9f2c99c32 --- /dev/null +++ b/challenge-228/pokgopun/python/ch-1.py @@ -0,0 +1,25 @@ +### Task 1: Unique Sum +### Submitted by: Mohammad S Anwar +### You are given an array of integers. +### +### Write a script to find out the sum of unique elements in the given array. +### +### Example 1 +### Input: @int = (2, 1, 3, 2) +### Output: 4 +### +### In the given array we have 2 unique elements (1, 3). +### Example 2 +### Input: @int = (1, 1, 1, 1) +### Output: 0 +### +### In the given array no unique element found. +### Example 3 +### Input: @int = (2, 1, 3, 4) +### Output: 10 +### +### In the given array every element is unique. + +def sumOfUniqElem(tup): return sum(tuple(filter(lambda x: tup.count(x)==1, tup))) + +for tup, s in ((2,1,3,2), 4), ((1,1,1,1), 0), ((2,1,3,4), 10): assert sumOfUniqElem(tup)==s diff --git a/challenge-228/pokgopun/python/ch-2.py b/challenge-228/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..d4f65de3ba --- /dev/null +++ b/challenge-228/pokgopun/python/ch-2.py @@ -0,0 +1,39 @@ +### Task 2: Empty Array +### Submitted by: Mohammad S Anwar +### You are given an array of integers in which all elements are unique. +### +### Write a script to perform the following operations until the array is empty and return the total count of operations. +### +### +### If the first element is the smallest then remove it otherwise move it to the end. +### +### Example 1 +### Input: @int = (3, 4, 2) +### Ouput: 5 +### +### Operation 1: move 3 to the end: (4, 2, 3) +### Operation 2: move 4 to the end: (2, 3, 4) +### Operation 3: remove element 2: (3, 4) +### Operation 4: remove element 3: (4) +### Operation 5: remove element 4: () +### Example 2 +### Input: @int = (1, 2, 3) +### Ouput: 3 +### +### Operation 1: remove element 1: (2, 3) +### Operation 2: remove element 2: (3) +### Operation 3: remove element 3: () + +def cntEmptyOp(lst): + #print(lst,end=" ") + cnt = 0 + while len(lst)!=0: + cnt += 1 + if lst[0]!=min(lst): + lst.append(lst[0]) + lst.pop(0) + #print(cnt) + return cnt + +for lst, cnt in ([3,4,2], 5), ([1,2,3], 3): assert cntEmptyOp(lst)==cnt + |
