diff --git a/solutions/lastword.go b/solutions/lastword.go new file mode 100644 index 00000000..62700247 --- /dev/null +++ b/solutions/lastword.go @@ -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" +} diff --git a/solutions/lastword/main.go b/solutions/lastword/main.go deleted file mode 100644 index 711b1340..00000000 --- a/solutions/lastword/main.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strings" -) - -func main() { - if len(os.Args) == 2 { - words := strings.Fields(os.Args[1]) - if len(words) > 0 { - fmt.Println(words[len(words)-1]) - } - } -} diff --git a/tests/lastword_test/main.go b/tests/lastword_test/main.go index b340804c..6c9dd03a 100644 --- a/tests/lastword_test/main.go +++ b/tests/lastword_test/main.go @@ -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") }