-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution for cw-grasshopper-summation and cw-returning-strings
- Loading branch information
Showing
7 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |