Skip to content

Commit

Permalink
Update bobify function (#8)
Browse files Browse the repository at this point in the history
update bobify to run in O(n) and reorder the tests
  • Loading branch information
tlkamp authored Dec 12, 2019
1 parent 3adf8c8 commit e12bb8a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
33 changes: 7 additions & 26 deletions internal/bobify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,24 @@ package internal
import (
"bufio"
"os"
"strings"
"unicode"
)

func Bobify(input string, caps bool) string {
cap := bool2int(!caps)
spaces := spaceIndices(input)
input = strings.ReplaceAll(strings.ToLower(input), " ", "")
cap := bool2int(!caps) // boolean inverted because 1 = true, and we're using modulus, so it will select the wrong character indicies if not inverted
runes := []rune(input)

// auto-invert the boolean
for i := 0; i < len(runes); i++ {
if i%2 == cap {
runes[i] = unicode.ToUpper(runes[i])
if unicode.IsSpace(runes[i]) {
cap = (cap + 1) % 2 // change the value of cap to flip indicies if we encounter a space while processing
continue
}
}

for i := 0; i < len(spaces); i++ {
s := spaces[i]
runes = append(runes, 0 /* use the zero value of the element type */)
copy(runes[s+1:], runes[s:])
runes[s] = rune(' ')
}

return string(runes)
}

func spaceIndices(input string) []int {
runes := []rune(input)
var ind []int

for i := 0; i < len(runes); i++ {
if unicode.IsSpace(runes[i]) {
ind = append(ind, i)
if i%2 == cap { // using cap this way allows us to keep code DRY and invert the boolean condition when we encounter spaces
runes[i] = unicode.ToUpper(runes[i])
}
}
return ind
return string(runes)
}

func bool2int(in bool) int {
Expand Down
48 changes: 46 additions & 2 deletions internal/bobify_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package internal

import (
"io/ioutil"
"log"
"os"
"testing"
)

Expand All @@ -16,14 +19,55 @@ func TestBobifyCaps(t *testing.T) {
want := "HeRpAdErP"
got := Bobify("herpaderp", true)
if got != want {
t.Errorf("Bobify(\"herpaderp\", true = %s; want %s", got, want)
t.Errorf("Bobify(\"herpaderp\", true) = %s; want %s", got, want)
}
}

func TestBobifySpaces(t *testing.T) {
want := "dO yOu EvEn LiFt BrO"
got := Bobify("do you even lift bro", false)
if got != want {
t.Errorf("Bobify(\"do you even lift bro\", true = %s; want %s", got, want)
t.Errorf("Bobify(\"do you even lift bro\", false) = %s; want %s", got, want)
}
}

func TestIsStdin(t *testing.T) {
mockStdin("data")
want := true
got := IsStdin()
if got != want {
t.Errorf("IsStdin(), true = %v; want %v", got, want)
}
}

func TestReadStdin(t *testing.T) {
mockData := "data"
mockStdin(mockData)
want := mockData
got, err := ReadStdin()
if err != nil {
t.Error(err)
}
if got != want {
t.Errorf("ReadStdin(), true = %v; want %v", got, want)
}
}

func mockStdin(content string) {
data := []byte(content)
tmpfile, err := ioutil.TempFile("", "mockbob")

if err != nil {
log.Fatal(err)
}

if _, err := tmpfile.Write(data); err != nil {
log.Fatal(err)
}

if _, err := tmpfile.Seek(0, 0); err != nil {
log.Fatal(err)
}

os.Stdin = tmpfile
}

0 comments on commit e12bb8a

Please sign in to comment.