Skip to content

Commit

Permalink
Add solution for cw-grasshopper-summation and cw-returning-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
skosovsky committed Dec 22, 2023
1 parent 7a892c5 commit e345f86
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cw-grasshopper-summation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Grasshopper - Summation

Write a program that finds the summation of every number from `1` to num. The number will always be a positive integer greater than `0`. Your function only needs to return the result, what is shown between parentheses in the example below is how you reach that result and it's not part of it, see the sample tests.

For example (Input -> Output):
`2 -> 3 (1 + 2)`
`8 -> 36 (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)`
2 changes: 2 additions & 0 deletions cw-grasshopper-summation/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module github.com/skosovsky/algo/cw-grasshopper-summation
go 1.21.5
23 changes: 23 additions & 0 deletions cw-grasshopper-summation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
)

func main() {
fmt.Println(summation(213)) //nolint:gomnd
}

func summation(n int) int {
var sum int

for i := n; i > 0; i-- {
sum += i
}

return sum
}

func summationClever(n int) int {
return n * (n + 1) / 2
}
52 changes: 52 additions & 0 deletions cw-grasshopper-summation/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import "testing"

func Test_summation(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
args args
want int
}{
{
name: "Example 1",
args: args{2},
want: 3,
},
{
name: "Example 2",
args: args{1},
want: 1,
},
{
name: "Example 3",
args: args{8},
want: 36,
},
{
name: "Example 4",
args: args{22},
want: 253,
},
{
name: "Example 5",
args: args{100},
want: 5050,
},
{
name: "Example 6",
args: args{213},
want: 22791,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := summation(tt.args.n); got != tt.want {
t.Errorf("summation() = %v, want %v", got, tt.want)
}
})
}
}
5 changes: 5 additions & 0 deletions cw-returning-strings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Returning Strings

Make a function that will return a greeting statement that uses an input; your program should return, `"Hello, <name> how are you doing today?"`.

[Make sure you type the exact thing I wrote or the program may not execute properly]
2 changes: 2 additions & 0 deletions cw-returning-strings/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module github.com/skosovsky/algo/cw-returning-strings
go 1.21.5
14 changes: 14 additions & 0 deletions cw-returning-strings/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"
)

func main() {
fmt.Println(greet("Bob"))
}

func greet(name string) string {
hello := fmt.Sprintf("Hello, %s how are you doing today?", name)
return hello
}

0 comments on commit e345f86

Please sign in to comment.