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..b02505b --- /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, 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/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..c29821c --- /dev/null +++ b/02-primitives/01-numbers/exercise.go @@ -0,0 +1,8 @@ +package numbers + +//go:generate go run ../../exercises-cli.go -student-id=$STUDENT_ID generate + +// area will give back the area of the given shape. +func area(r, a, b, h float64) 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..c383fc5 --- /dev/null +++ b/02-primitives/02-strings/.README.md @@ -0,0 +1,7 @@ +# Strings - Change the network mask in the given CIDR address + +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 new file mode 100644 index 0000000..6c5bd45 --- /dev/null +++ b/02-primitives/02-strings/.exercise_test.go @@ -0,0 +1,11 @@ +package strings + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +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 new file mode 100644 index 0000000..90c149f --- /dev/null +++ b/02-primitives/02-strings/README.md @@ -0,0 +1 @@ +# PLEASE RUN make generate diff --git a/02-primitives/02-strings/exercise.go b/02-primitives/02-strings/exercise.go new file mode 100644 index 0000000..d63c449 --- /dev/null +++ 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 + +// replaceNetMask will replace the subnet part of a CIDR address +// Example -> 10.0.0.1/10 -> 10.0.0.1/14 +func replaceNetMask(address, newSubnet string) string { + // INSERT YOUR CODE HERE +} diff --git a/02-primitives/02-strings/exercise.yaml b/02-primitives/02-strings/exercise.yaml new file mode 100644 index 0000000..19c9d0b --- /dev/null +++ b/02-primitives/02-strings/exercise.yaml @@ -0,0 +1,20 @@ +name: "strings" +input: +- 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 diff --git a/02-primitives/02-strings/exercise_test.go b/02-primitives/02-strings/exercise_test.go new file mode 100644 index 0000000..4cf09ae --- /dev/null +++ b/02-primitives/02-strings/exercise_test.go @@ -0,0 +1 @@ +// 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