Skip to content

Commit

Permalink
feat(lastword): update tests and solution to be a function instead of…
Browse files Browse the repository at this point in the history
… program
  • Loading branch information
nprimo authored and HarryVasanth committed Jun 27, 2024
1 parent 52b72ff commit 97e9d5e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
13 changes: 13 additions & 0 deletions solutions/lastword.go
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"
}
16 changes: 0 additions & 16 deletions solutions/lastword/main.go

This file was deleted.

52 changes: 35 additions & 17 deletions tests/lastword_test/main.go
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")
}

0 comments on commit 97e9d5e

Please sign in to comment.