-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lastword): update tests and solution to be a function instead of…
… program
- Loading branch information
1 parent
52b72ff
commit 97e9d5e
Showing
3 changed files
with
48 additions
and
33 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,13 @@ | ||
package solutions | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func LastWord(s string) string { | ||
words := strings.Fields(s) | ||
if len(words) > 0 { | ||
return words[len(words)-1] + "\n" | ||
} | ||
return "\n" | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,25 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/01-edu/go-tests/lib/challenge" | ||
"github.com/01-edu/go-tests/lib/chars" | ||
"github.com/01-edu/go-tests/lib/random" | ||
"log" | ||
"reflect" | ||
student "student" | ||
) | ||
|
||
func main() { | ||
args := []string{ | ||
"FOR PONY", | ||
"this ... is sparta, then again, maybe not", | ||
" ", | ||
" lorem,ipsum ", | ||
} | ||
|
||
args = append(args, random.StrSlice(chars.Words)...) | ||
var testCases = []struct { | ||
in string | ||
want string | ||
}{ | ||
{ | ||
in: " ", | ||
want: "\n", | ||
}, | ||
{ | ||
in: "FOR PONY", | ||
want: "PONY\n", | ||
}, | ||
{ | ||
in: "this ... is sparta, then again, maybe not", | ||
want: "not\n", | ||
}, | ||
{ | ||
in: " lorem,ipsum ", | ||
want: "lorem,ipsum\n", | ||
}, | ||
} | ||
|
||
for _, v := range args { | ||
challenge.Program("lastword", v) | ||
func main() { | ||
for _, tc := range testCases { | ||
got := student.LastWord(tc.in) | ||
if !reflect.DeepEqual(got, tc.want) { | ||
log.Fatalf("%s(%q) == %q instead of %q\n", | ||
"RevConcatAlternate", | ||
tc.in, | ||
got, | ||
tc.want, | ||
) | ||
} | ||
} | ||
|
||
challenge.Program("lastword", "a", "b") | ||
challenge.Program("lastword") | ||
} |