aboutsummaryrefslogtreecommitdiff
path: root/challenge-201
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2023-01-25 13:49:51 +0100
committerMariano Spadaccini <spadacciniweb@gmail.com>2023-01-25 13:49:51 +0100
commitc1b02fea9075670a72565851f89aaca77292ee85 (patch)
treed560d826de8c615634718f918ff7b4ae6bef1171 /challenge-201
parent2ca314cd4cfa147739729b900d2b254834eb6689 (diff)
downloadperlweeklychallenge-club-c1b02fea9075670a72565851f89aaca77292ee85.tar.gz
perlweeklychallenge-club-c1b02fea9075670a72565851f89aaca77292ee85.tar.bz2
perlweeklychallenge-club-c1b02fea9075670a72565851f89aaca77292ee85.zip
PWC 201 go
Diffstat (limited to 'challenge-201')
-rw-r--r--challenge-201/spadacciniweb/go/ch-1.go67
-rw-r--r--challenge-201/spadacciniweb/go/ch-2.go121
2 files changed, 188 insertions, 0 deletions
diff --git a/challenge-201/spadacciniweb/go/ch-1.go b/challenge-201/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..f16ac28222
--- /dev/null
+++ b/challenge-201/spadacciniweb/go/ch-1.go
@@ -0,0 +1,67 @@
+/*
+Task 1: Missing Numbers
+Submitted by: Mohammad S Anwar
+
+You are given an array of unique numbers.
+Write a script to find out all missing numbers in the range 0..$n where $n is the array size.
+
+Example 1
+Input: @array = (0,1,3)
+Output: 2
+
+The array size i.e. total element count is 3, so the range is 0..3.
+The missing number is 2 in the given array.
+
+Example 2
+Input: @array = (0,1)
+Output: 2
+
+The array size is 2, therefore the range is 0..2.
+The missing number is 2.
+*/
+
+package main
+
+import (
+ "fmt"
+ "os"
+ "strconv"
+)
+
+func main() {
+ arrStr := os.Args[1:]
+ arrInt := make([]int, 0)
+ for i := 0; i <= len(arrStr)-1; i++ {
+ value, _ := strconv.Atoi(arrStr[i])
+ arrInt = append(arrInt, value)
+ }
+
+ missing := make([]int, 0)
+ for i := 0; i <= len(arrInt) ; i++ {
+ isPresent := contains(arrInt, i)
+ if !isPresent {
+ missing = append(missing, i)
+ }
+ }
+
+ if len(missing) > 1 {
+ fmt.Print("The missing are:")
+ } else {
+ fmt.Print("The missing is:")
+ }
+
+ for _, value := range missing {
+ fmt.Printf(" %d", value)
+ }
+
+ fmt.Println()
+}
+
+func contains(a []int, e int) bool {
+ for _, v := range a {
+ if v == e {
+ return true
+ }
+ }
+ return false
+}
diff --git a/challenge-201/spadacciniweb/go/ch-2.go b/challenge-201/spadacciniweb/go/ch-2.go
new file mode 100644
index 0000000000..7cf5341e79
--- /dev/null
+++ b/challenge-201/spadacciniweb/go/ch-2.go
@@ -0,0 +1,121 @@
+/*
+Task 2: Penny Piles
+Submitted by: Robbie Hatley
+
+You are given an integer, $n > 0.
+Write a script to determine the number of ways of putting $n pennies in a row of piles of ascending heights from left to right.
+
+Example
+Input: $n = 5
+Output: 7
+
+Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles:
+ 1 1 1 1 1
+ 1 1 1 2
+ 1 2 2
+ 1 1 3
+ 2 3
+ 1 4
+ 5
+*/
+
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ var totPenny int
+ var maxPiles int
+ var remainingPenny int
+ //var piles = make([]int, 0)
+ var piles [][]int
+
+ fmt.Print("Please enter a integer number > 0: ")
+ fmt.Scanf("%d", &totPenny)
+ maxPiles = totPenny
+
+ for currPiles := maxPiles; currPiles > 0; currPiles-- {
+ seedData := make([]int, currPiles)
+ for i := range seedData {
+ seedData[i] = 1
+ }
+ remainingPenny = totPenny - sum(seedData)
+
+ remainingCombinations := make([]int, remainingPenny+1)
+ for i := range remainingCombinations {
+ remainingCombinations[i] = i
+ }
+ results := permutations(remainingCombinations, len(seedData))
+ for i := range results {
+ if (sum(results[i]) != remainingPenny || (len(results[i]) > 0 && !checkAscendingOrder(results[i])) ) {
+ continue
+ }
+ data := make([]int, len(seedData))
+ for k := range seedData {
+ data[k] = seedData[k] + results[i][k]
+ }
+ piles = append(piles, data)
+ }
+ }
+ fmt.Printf("Since n=%d, there are %d ways of stacking %d pennies in ascending piles:", totPenny, len(piles), totPenny);
+ for i:=0; i<=len(piles)-1; i++ {
+ fmt.Printf("\n\t")
+ for _, value := range piles[i] {
+ fmt.Printf(" %d", value)
+ }
+ }
+ fmt.Println()
+}
+
+func checkAscendingOrder(arr []int) bool {
+ var sortedArray = true
+ for i:=0; i<=len(arr)-2; i++ {
+ if arr[i] > arr[i+1]{
+ sortedArray = false
+ break
+ }
+ }
+ return sortedArray
+}
+
+func sum(array[] int) int {
+ result := 0
+ for _,
+ v := range array {
+ result += v
+ }
+ return result
+}
+
+func permutations(elements []int, length int) [][]int {
+ var results [][]int
+ var perm = make([]int, length) // to write each permutation
+ used := make([]bool, len(elements)) // used[i] indicates whether i-th element is already used
+ var withRepetition = true
+
+ var dfs func(currentIndex, depth int)
+ dfs = func(currentIndex, depth int) {
+ perm[length-depth] = elements[currentIndex]
+ if depth == 1 {
+ result := make([]int, length)
+ copy(result, perm)
+ results = append(results, result)
+ return
+ }
+ used[currentIndex] = true
+ for i := range elements {
+ if !withRepetition && used[i] {
+ continue
+ }
+ dfs(i, depth-1)
+ }
+ used[currentIndex] = false
+ }
+ // start from all elements
+ for i := range elements {
+ dfs(i, length)
+ }
+ return results
+}