From 147033d8bc8edd26a4c9f74fb5f0933543b6799f Mon Sep 17 00:00:00 2001 From: Richard Varadi Date: Tue, 28 Feb 2023 22:42:40 +0100 Subject: [PATCH 1/4] Add primitives --- 02-primitives/01-numbers/.README.md | 7 +++++ 02-primitives/01-numbers/.exercise_test.go | 30 ++++++++++++++++++++++ 02-primitives/01-numbers/README.md | 1 + 02-primitives/01-numbers/exercise.go | 9 +++++++ 02-primitives/01-numbers/exercise.yaml | 14 ++++++++++ 02-primitives/01-numbers/exercise_test.go | 1 + 02-primitives/02-strings/.README.md | 0 02-primitives/02-strings/.exercise_test.go | 0 02-primitives/02-strings/README.md | 0 02-primitives/02-strings/exercise.go | 0 02-primitives/02-strings/exercise.yaml | 0 02-primitives/02-strings/exercise_test.go | 0 12 files changed, 62 insertions(+) create mode 100644 02-primitives/01-numbers/.README.md create mode 100644 02-primitives/01-numbers/.exercise_test.go create mode 100644 02-primitives/01-numbers/README.md create mode 100644 02-primitives/01-numbers/exercise.go create mode 100644 02-primitives/01-numbers/exercise.yaml create mode 100644 02-primitives/01-numbers/exercise_test.go create mode 100644 02-primitives/02-strings/.README.md create mode 100644 02-primitives/02-strings/.exercise_test.go create mode 100644 02-primitives/02-strings/README.md create mode 100644 02-primitives/02-strings/exercise.go create mode 100644 02-primitives/02-strings/exercise.yaml create mode 100644 02-primitives/02-strings/exercise_test.go diff --git a/02-primitives/01-numbers/.README.md b/02-primitives/01-numbers/.README.md new file mode 100644 index 0000000..4ad7829 --- /dev/null +++ b/02-primitives/01-numbers/.README.md @@ -0,0 +1,7 @@ +# Numbers - Calculate the area of shapes + +Write a small function with multiple input parameters and return the area of {{index . "shape"}}. The formula: {{index . "formula"}}. + +Please be aware that you have to specify input parameters based on the given formula. Every parameter should be float64 and you have to return a float64 number. + +In the file `exercise.go` near the placeholder `// INSERT YOUR CODE HERE`. \ No newline at end of file diff --git a/02-primitives/01-numbers/.exercise_test.go b/02-primitives/01-numbers/.exercise_test.go new file mode 100644 index 0000000..cb2c7f9 --- /dev/null +++ b/02-primitives/01-numbers/.exercise_test.go @@ -0,0 +1,30 @@ +package numbers + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +const r float64 = 3.4 +const a float64 = 3.6 +const b float64 = 4.2 +const h float64 = 6.9 + + +func TestArea(t *testing.T) { + switch {{index . "shape"}} { + case "Circle": + assert.Equal(t, shape(r), 36.32) + case "Ellipse": + assert.Equal(t, shape(a, b), 47.5) + case "Trapezoid": + assert.Equal(t, shape(a, b, h), 26.91) + case "Pentagon": + assert.Equal(t, shape(a), 22.297) + case "Hexagon": + assert.Equal(t, shape(a), 33.67) + case "Octagon": + assert.Equal(t, shape(a), 62.58) + } +} diff --git a/02-primitives/01-numbers/README.md b/02-primitives/01-numbers/README.md new file mode 100644 index 0000000..90c149f --- /dev/null +++ b/02-primitives/01-numbers/README.md @@ -0,0 +1 @@ +# PLEASE RUN make generate diff --git a/02-primitives/01-numbers/exercise.go b/02-primitives/01-numbers/exercise.go new file mode 100644 index 0000000..1e03af1 --- /dev/null +++ b/02-primitives/01-numbers/exercise.go @@ -0,0 +1,9 @@ +package numbers + +//go:generate go run ../../exercises-cli.go -student-id=$STUDENT_ID generate + +// area will give back the area of the given shape. +// Please define the input params. +func area() float64 { + // INSERT YOUR CODE HERE +} diff --git a/02-primitives/01-numbers/exercise.yaml b/02-primitives/01-numbers/exercise.yaml new file mode 100644 index 0000000..a62b274 --- /dev/null +++ b/02-primitives/01-numbers/exercise.yaml @@ -0,0 +1,14 @@ +name: numbers +input: +- shape: "Circle" + formula: "A = pi * r^2" +- shape: "Ellipse" + formula: "A = a * b * pi" +- shape: "Trapezoid" + formula: "A = (a + b) * h / 2" +- shape: "Pentagon" + formula: "A = a^2 * sqrt{25 + 10sqrt{5}} / 4" +- shape: "Hexagon" + formula: "A = 3/2 * sqrt{3} * a^2" +- shape: "Octagon" + formula: "A = 2 * (1 + sqrt{2}) * a^2" \ No newline at end of file diff --git a/02-primitives/01-numbers/exercise_test.go b/02-primitives/01-numbers/exercise_test.go new file mode 100644 index 0000000..4cf09ae --- /dev/null +++ b/02-primitives/01-numbers/exercise_test.go @@ -0,0 +1 @@ +// PLEASE RUN make generate diff --git a/02-primitives/02-strings/.README.md b/02-primitives/02-strings/.README.md new file mode 100644 index 0000000..e69de29 diff --git a/02-primitives/02-strings/.exercise_test.go b/02-primitives/02-strings/.exercise_test.go new file mode 100644 index 0000000..e69de29 diff --git a/02-primitives/02-strings/README.md b/02-primitives/02-strings/README.md new file mode 100644 index 0000000..e69de29 diff --git a/02-primitives/02-strings/exercise.go b/02-primitives/02-strings/exercise.go new file mode 100644 index 0000000..e69de29 diff --git a/02-primitives/02-strings/exercise.yaml b/02-primitives/02-strings/exercise.yaml new file mode 100644 index 0000000..e69de29 diff --git a/02-primitives/02-strings/exercise_test.go b/02-primitives/02-strings/exercise_test.go new file mode 100644 index 0000000..e69de29 From 2e3b0315f0ca33ff7beea1047396b0d8c1d5bb88 Mon Sep 17 00:00:00 2001 From: Richard Varadi Date: Wed, 1 Mar 2023 22:33:54 +0100 Subject: [PATCH 2/4] Add string modification --- 02-primitives/01-numbers/.exercise_test.go | 14 +++++----- 02-primitives/01-numbers/README.md | 8 +++++- 02-primitives/01-numbers/exercise.go | 3 +-- 02-primitives/01-numbers/exercise_test.go | 31 +++++++++++++++++++++- 02-primitives/02-strings/.README.md | 3 +++ 02-primitives/02-strings/.exercise_test.go | 11 ++++++++ 02-primitives/02-strings/README.md | 3 +++ 02-primitives/02-strings/exercise.go | 9 +++++++ 02-primitives/02-strings/exercise.yaml | 20 ++++++++++++++ 02-primitives/02-strings/exercise_test.go | 11 ++++++++ 10 files changed, 102 insertions(+), 11 deletions(-) diff --git a/02-primitives/01-numbers/.exercise_test.go b/02-primitives/01-numbers/.exercise_test.go index cb2c7f9..b02505b 100644 --- a/02-primitives/01-numbers/.exercise_test.go +++ b/02-primitives/01-numbers/.exercise_test.go @@ -13,18 +13,18 @@ const h float64 = 6.9 func TestArea(t *testing.T) { - switch {{index . "shape"}} { + switch "{{index . "shape"}}"{ case "Circle": - assert.Equal(t, shape(r), 36.32) + assert.Equal(t, area(r, a, b, h), 36.32) case "Ellipse": - assert.Equal(t, shape(a, b), 47.5) + assert.Equal(t, area(r, a, b, h), 47.5) case "Trapezoid": - assert.Equal(t, shape(a, b, h), 26.91) + assert.Equal(t, area(r, a, b, h), 26.91) case "Pentagon": - assert.Equal(t, shape(a), 22.297) + assert.Equal(t, area(r, a, b, h), 22.297) case "Hexagon": - assert.Equal(t, shape(a), 33.67) + assert.Equal(t, area(r, a, b, h), 33.67) case "Octagon": - assert.Equal(t, shape(a), 62.58) + assert.Equal(t, area(r, a, b, h), 62.58) } } diff --git a/02-primitives/01-numbers/README.md b/02-primitives/01-numbers/README.md index 90c149f..370dda6 100644 --- a/02-primitives/01-numbers/README.md +++ b/02-primitives/01-numbers/README.md @@ -1 +1,7 @@ -# PLEASE RUN make generate +# Numbers - Calculate the area of shapes + +Write a small function with multiple input parameters and return the area of Circle. The formula: A = pi * r^2. + +Please be aware that you have to specify input parameters based on the given formula. Every parameter should be float64 and you have to return a float64 number. + +In the file `exercise.go` near the placeholder `// INSERT YOUR CODE HERE`. \ No newline at end of file diff --git a/02-primitives/01-numbers/exercise.go b/02-primitives/01-numbers/exercise.go index 1e03af1..c29821c 100644 --- a/02-primitives/01-numbers/exercise.go +++ b/02-primitives/01-numbers/exercise.go @@ -3,7 +3,6 @@ package numbers //go:generate go run ../../exercises-cli.go -student-id=$STUDENT_ID generate // area will give back the area of the given shape. -// Please define the input params. -func area() float64 { +func area(r, a, b, h float64) float64 { // INSERT YOUR CODE HERE } diff --git a/02-primitives/01-numbers/exercise_test.go b/02-primitives/01-numbers/exercise_test.go index 4cf09ae..1226bb2 100644 --- a/02-primitives/01-numbers/exercise_test.go +++ b/02-primitives/01-numbers/exercise_test.go @@ -1 +1,30 @@ -// PLEASE RUN make generate +package numbers + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +const r float64 = 3.4 +const a float64 = 3.6 +const b float64 = 4.2 +const h float64 = 6.9 + + +func TestArea(t *testing.T) { + switch "Circle"{ + case "Circle": + assert.Equal(t, area(r, a, b, h), 36.32) + case "Ellipse": + assert.Equal(t, area(r, a, b, h), 47.5) + case "Trapezoid": + assert.Equal(t, area(r, a, b, h), 26.91) + case "Pentagon": + assert.Equal(t, area(r, a, b, h), 22.297) + case "Hexagon": + assert.Equal(t, area(r, a, b, h), 33.67) + case "Octagon": + assert.Equal(t, area(r, a, b, h), 62.58) + } +} diff --git a/02-primitives/02-strings/.README.md b/02-primitives/02-strings/.README.md index e69de29..1722d9b 100644 --- a/02-primitives/02-strings/.README.md +++ b/02-primitives/02-strings/.README.md @@ -0,0 +1,3 @@ +# Strings - Change subnet number in the given CIDR address + +Write a small function, and change the subnet number of the {{index . "cidr"}} CIDR address to {{index . "subnet"}} in the file `exercise.go` near the placeholder `// INSERT YOUR CODE HERE`. Do not use additional variables, or modify the existing code. \ No newline at end of file diff --git a/02-primitives/02-strings/.exercise_test.go b/02-primitives/02-strings/.exercise_test.go index e69de29..8998bd9 100644 --- a/02-primitives/02-strings/.exercise_test.go +++ b/02-primitives/02-strings/.exercise_test.go @@ -0,0 +1,11 @@ +package strings + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestReplaceSubnet(t *testing.T) { + assert.Equal(t, replaceSubnet("{{index . "cidr"}}", "{{index . "subnet"}}"), "{{index . "address"}}/{{index . "subnet"}}") +} diff --git a/02-primitives/02-strings/README.md b/02-primitives/02-strings/README.md index e69de29..3d9efd5 100644 --- a/02-primitives/02-strings/README.md +++ b/02-primitives/02-strings/README.md @@ -0,0 +1,3 @@ +# Strings - Change subnet number in the given CIDR address + +Write a small function, and change the subnet number of the 192.168.0.1/10 CIDR address to 11 in the file `exercise.go` near the placeholder `// INSERT YOUR CODE HERE`. Do not use additional variables, or modify the existing code. \ No newline at end of file diff --git a/02-primitives/02-strings/exercise.go b/02-primitives/02-strings/exercise.go index e69de29..4996717 100644 --- a/02-primitives/02-strings/exercise.go +++ b/02-primitives/02-strings/exercise.go @@ -0,0 +1,9 @@ +package strings + +//go:generate go run ../../exercises-cli.go -student-id=$STUDENT_ID generate + +// replaceSubnet will replace the subnet part of a CIDR address +// Example -> 10.0.0.1/10 -> 10.0.0.1/14 +func replaceSubnet(address, newSubnet string) string { + // INSERT YOUR CODE HERE +} diff --git a/02-primitives/02-strings/exercise.yaml b/02-primitives/02-strings/exercise.yaml index e69de29..702bcb8 100644 --- a/02-primitives/02-strings/exercise.yaml +++ b/02-primitives/02-strings/exercise.yaml @@ -0,0 +1,20 @@ +name: "strings" +input: +- cidr: "192.168.0.1/10" + address: "192.168.0.1" + subnet: "11" +- cidr: "162.198.0.1/24" + address: "162.198.0.1" + subnet: "12" +- cidr: "152.178.2.34/4" + address: "152.178.2.34" + subnet: "30" +- cidr: "222.111.8.5/14" + address: "222.111.8.5" + subnet: "18" +- cidr: "12.18.10.1/32" + address: "12.18.10.1" + subnet: "25" +- cidr: "133.98.45.4/28" + address: "133.98.45.4" + subnet: "10" \ No newline at end of file diff --git a/02-primitives/02-strings/exercise_test.go b/02-primitives/02-strings/exercise_test.go index e69de29..2e37dfe 100644 --- a/02-primitives/02-strings/exercise_test.go +++ b/02-primitives/02-strings/exercise_test.go @@ -0,0 +1,11 @@ +package strings + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestReplaceSubnet(t *testing.T) { + assert.Equal(t, replaceSubnet("192.168.0.1/10", "11"), "192.168.0.1/11") +} From 37db79a775fbf9113f22b60ff26be773cdf5fa67 Mon Sep 17 00:00:00 2001 From: Richard Varadi Date: Thu, 2 Mar 2023 07:51:42 +0100 Subject: [PATCH 3/4] Fix string exercise --- 02-primitives/01-numbers/README.md | 8 +----- 02-primitives/01-numbers/exercise_test.go | 31 +--------------------- 02-primitives/02-strings/.README.md | 8 ++++-- 02-primitives/02-strings/.exercise_test.go | 4 +-- 02-primitives/02-strings/README.md | 4 +-- 02-primitives/02-strings/exercise.go | 4 +-- 02-primitives/02-strings/exercise_test.go | 12 +-------- Makefile | 2 ++ 8 files changed, 16 insertions(+), 57 deletions(-) diff --git a/02-primitives/01-numbers/README.md b/02-primitives/01-numbers/README.md index 370dda6..90c149f 100644 --- a/02-primitives/01-numbers/README.md +++ b/02-primitives/01-numbers/README.md @@ -1,7 +1 @@ -# Numbers - Calculate the area of shapes - -Write a small function with multiple input parameters and return the area of Circle. The formula: A = pi * r^2. - -Please be aware that you have to specify input parameters based on the given formula. Every parameter should be float64 and you have to return a float64 number. - -In the file `exercise.go` near the placeholder `// INSERT YOUR CODE HERE`. \ No newline at end of file +# PLEASE RUN make generate diff --git a/02-primitives/01-numbers/exercise_test.go b/02-primitives/01-numbers/exercise_test.go index 1226bb2..4cf09ae 100644 --- a/02-primitives/01-numbers/exercise_test.go +++ b/02-primitives/01-numbers/exercise_test.go @@ -1,30 +1 @@ -package numbers - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -const r float64 = 3.4 -const a float64 = 3.6 -const b float64 = 4.2 -const h float64 = 6.9 - - -func TestArea(t *testing.T) { - switch "Circle"{ - case "Circle": - assert.Equal(t, area(r, a, b, h), 36.32) - case "Ellipse": - assert.Equal(t, area(r, a, b, h), 47.5) - case "Trapezoid": - assert.Equal(t, area(r, a, b, h), 26.91) - case "Pentagon": - assert.Equal(t, area(r, a, b, h), 22.297) - case "Hexagon": - assert.Equal(t, area(r, a, b, h), 33.67) - case "Octagon": - assert.Equal(t, area(r, a, b, h), 62.58) - } -} +// PLEASE RUN make generate diff --git a/02-primitives/02-strings/.README.md b/02-primitives/02-strings/.README.md index 1722d9b..c383fc5 100644 --- a/02-primitives/02-strings/.README.md +++ b/02-primitives/02-strings/.README.md @@ -1,3 +1,7 @@ -# Strings - Change subnet number in the given CIDR address +# Strings - Change the network mask in the given CIDR address -Write a small function, and change the subnet number of the {{index . "cidr"}} CIDR address to {{index . "subnet"}} in the file `exercise.go` near the placeholder `// INSERT YOUR CODE HERE`. Do not use additional variables, or modify the existing code. \ No newline at end of file +Write a small function, and change the network mask of the {{index . "cidr"}} CIDR address to {{index . "subnet"}} in the file `exercise.go` near the placeholder `// INSERT YOUR CODE HERE`. Do not use additional variables, or modify the existing code. + +Hint: Use strings.Split to split strings on an arbitrary delimiter. + +CIDR notation is a compact representation of an IP address and its associated network mask. The notation was invented by Phil Karn in the 1980s. CIDR notation specifies an IP address, a slash ('/') character, and a decimal number. The decimal number is the count of consecutive leading 1-bits (from left to right) in the network mask. \ No newline at end of file diff --git a/02-primitives/02-strings/.exercise_test.go b/02-primitives/02-strings/.exercise_test.go index 8998bd9..6c5bd45 100644 --- a/02-primitives/02-strings/.exercise_test.go +++ b/02-primitives/02-strings/.exercise_test.go @@ -6,6 +6,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestReplaceSubnet(t *testing.T) { - assert.Equal(t, replaceSubnet("{{index . "cidr"}}", "{{index . "subnet"}}"), "{{index . "address"}}/{{index . "subnet"}}") +func TestReplaceNetMask(t *testing.T) { + assert.Equal(t, replaceNetMask("{{index . "cidr"}}", "{{index . "subnet"}}"), "{{index . "address"}}/{{index . "subnet"}}") } diff --git a/02-primitives/02-strings/README.md b/02-primitives/02-strings/README.md index 3d9efd5..90c149f 100644 --- a/02-primitives/02-strings/README.md +++ b/02-primitives/02-strings/README.md @@ -1,3 +1 @@ -# Strings - Change subnet number in the given CIDR address - -Write a small function, and change the subnet number of the 192.168.0.1/10 CIDR address to 11 in the file `exercise.go` near the placeholder `// INSERT YOUR CODE HERE`. Do not use additional variables, or modify the existing code. \ No newline at end of file +# PLEASE RUN make generate diff --git a/02-primitives/02-strings/exercise.go b/02-primitives/02-strings/exercise.go index 4996717..d63c449 100644 --- a/02-primitives/02-strings/exercise.go +++ b/02-primitives/02-strings/exercise.go @@ -2,8 +2,8 @@ package strings //go:generate go run ../../exercises-cli.go -student-id=$STUDENT_ID generate -// replaceSubnet will replace the subnet part of a CIDR address +// replaceNetMask will replace the subnet part of a CIDR address // Example -> 10.0.0.1/10 -> 10.0.0.1/14 -func replaceSubnet(address, newSubnet string) string { +func replaceNetMask(address, newSubnet string) string { // INSERT YOUR CODE HERE } diff --git a/02-primitives/02-strings/exercise_test.go b/02-primitives/02-strings/exercise_test.go index 2e37dfe..4cf09ae 100644 --- a/02-primitives/02-strings/exercise_test.go +++ b/02-primitives/02-strings/exercise_test.go @@ -1,11 +1 @@ -package strings - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestReplaceSubnet(t *testing.T) { - assert.Equal(t, replaceSubnet("192.168.0.1/10", "11"), "192.168.0.1/11") -} +// PLEASE RUN make generate diff --git a/Makefile b/Makefile index b608fe8..0c63933 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ # exercise subdirs EXERCISE_DIRS=\ 01-getting-started \ + 02-primitives \ + 03-variables \ 07-switch \ 19-structs \ 22-goroutines From 6c442812265bdb685cf39f75b7bfc40ae0f692b1 Mon Sep 17 00:00:00 2001 From: Richard Varadi Date: Mon, 20 Mar 2023 10:33:14 +0100 Subject: [PATCH 4/4] Change cidrs --- 02-primitives/02-strings/exercise.yaml | 36 +++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/02-primitives/02-strings/exercise.yaml b/02-primitives/02-strings/exercise.yaml index 702bcb8..19c9d0b 100644 --- a/02-primitives/02-strings/exercise.yaml +++ b/02-primitives/02-strings/exercise.yaml @@ -1,20 +1,20 @@ name: "strings" input: -- cidr: "192.168.0.1/10" - address: "192.168.0.1" - subnet: "11" -- cidr: "162.198.0.1/24" - address: "162.198.0.1" - subnet: "12" -- cidr: "152.178.2.34/4" - address: "152.178.2.34" - subnet: "30" -- cidr: "222.111.8.5/14" - address: "222.111.8.5" - subnet: "18" -- cidr: "12.18.10.1/32" - address: "12.18.10.1" - subnet: "25" -- cidr: "133.98.45.4/28" - address: "133.98.45.4" - subnet: "10" \ No newline at end of file +- cidr: "192.168.1.1/24" + address: "192.168.1.1" + subnet: "26" +- cidr: "10.0.0.138/12" + address: "10.0.0.138" + subnet: "24" +- cidr: "10.0.1.1/16" + address: "10.0.1.1" + subnet: "24" +- cidr: "192.168.254.254/24" + address: "192.168.254.254" + subnet: "32" +- cidr: "127.0.0.0/1" + address: "127.0.0.0" + subnet: "16" +- cidr: "152.66.115.203/32" + address: "152.66.115.203" + subnet: "32" \ No newline at end of file