aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos <gramos@gramos.me>2021-04-11 12:57:23 +0200
committerGuillermo Ramos <gramos@gramos.me>2021-04-11 13:04:06 +0200
commita0e6407010a317bb1916a0511b6c25db68080fa4 (patch)
tree6b070b39d4816b83a30c4a733f7a521df6a27b07
parentf229dc0bbcf4d4f9f65580150c95be2416398a30 (diff)
downloadperlweeklychallenge-club-a0e6407010a317bb1916a0511b6c25db68080fa4.tar.gz
perlweeklychallenge-club-a0e6407010a317bb1916a0511b6c25db68080fa4.tar.bz2
perlweeklychallenge-club-a0e6407010a317bb1916a0511b6c25db68080fa4.zip
Challenge 107 (Go)
-rw-r--r--challenge-107/guillermo-ramos/go/ch-1.go32
-rw-r--r--challenge-107/guillermo-ramos/go/ch107_test.go31
-rw-r--r--challenge-107/guillermo-ramos/go/go.mod3
-rwxr-xr-xchallenge-107/guillermo-ramos/go/run_tests.sh3
4 files changed, 69 insertions, 0 deletions
diff --git a/challenge-107/guillermo-ramos/go/ch-1.go b/challenge-107/guillermo-ramos/go/ch-1.go
new file mode 100644
index 0000000000..4a3953c85b
--- /dev/null
+++ b/challenge-107/guillermo-ramos/go/ch-1.go
@@ -0,0 +1,32 @@
+package ch107
+
+import "fmt"
+
+func IsSelfDescriptive(n int) bool {
+ nStr := fmt.Sprint(n)
+
+ digits := make(map[rune]int)
+ for _, r := range nStr {
+ digits[r] += 1
+ }
+
+ for i, r := range nStr {
+ digit := int(r) - '0'
+ appears := digits[rune('0'+i)]
+ if digit != appears {
+ return false
+ }
+ }
+ return true
+}
+
+func SelfDescriptiveNumbers(count int) []int {
+ res := make([]int, 0, count)
+ for i := 0; count > 0; i++ {
+ if IsSelfDescriptive(i) {
+ res = append(res, i)
+ count--
+ }
+ }
+ return res
+}
diff --git a/challenge-107/guillermo-ramos/go/ch107_test.go b/challenge-107/guillermo-ramos/go/ch107_test.go
new file mode 100644
index 0000000000..5ada2e7a2a
--- /dev/null
+++ b/challenge-107/guillermo-ramos/go/ch107_test.go
@@ -0,0 +1,31 @@
+package ch107
+
+import "testing"
+
+func TestIsSelfDescriptive(t *testing.T) {
+ expectedMap := map[int]bool{
+ 1210: true,
+ 1211: false,
+ }
+ for input, expected := range expectedMap {
+ res := IsSelfDescriptive(input)
+ if res != expected {
+ t.Errorf("IsSelfDescriptive(%d) returned '%v' but '%v' was expected",
+ input, res, expected)
+ }
+ }
+}
+
+func TestSelfDescriptiveNumbers(t *testing.T) {
+ input := 3
+ expected := []int{1210, 2020, 21200}
+ res := SelfDescriptiveNumbers(input)
+ equals := len(expected) == len(res)
+ for i := 0; i < len(res) && equals; i++ {
+ equals = expected[i] == res[i]
+ }
+ if !equals {
+ t.Errorf("SelfDescriptiveNumbers(%d) returned '%v' but '%v' was expected",
+ input, res, expected)
+ }
+}
diff --git a/challenge-107/guillermo-ramos/go/go.mod b/challenge-107/guillermo-ramos/go/go.mod
new file mode 100644
index 0000000000..fb854c7861
--- /dev/null
+++ b/challenge-107/guillermo-ramos/go/go.mod
@@ -0,0 +1,3 @@
+module example.com/ch107
+
+go 1.16
diff --git a/challenge-107/guillermo-ramos/go/run_tests.sh b/challenge-107/guillermo-ramos/go/run_tests.sh
new file mode 100755
index 0000000000..20e7774e09
--- /dev/null
+++ b/challenge-107/guillermo-ramos/go/run_tests.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+go test -v