Last active
December 17, 2015 08:29
-
-
Save oscarryz/5580621 to your computer and use it in GitHub Desktop.
wordwrap Kata (not working)
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
package main | |
import ( | |
"fmt" | |
//"unicode" | |
) | |
func main() { | |
fmt.Println("0000") | |
fmt.Println(WordWrap("999999999", 2)) | |
} | |
func WordWrap(input string, columnSize int) string { | |
if len(input) <= columnSize || columnSize == 0 { | |
return input | |
} | |
result := "" | |
index, inputLength := 0, len(input) | |
for { | |
cut := index + columnSize | |
//don't go beyond the inputLength | |
if cut > inputLength { | |
cut = inputLength | |
} | |
// get the next chunk | |
next := input[index:cut] | |
// increment the index | |
// if there is a space in the cut, take it too | |
if cut < inputLength && input[cut:cut+1] == " " { | |
index += 1 | |
// except if that space would fit perfect | |
// in the next cut | |
if len(input[cut:]) == columnSize { | |
index -= 1 | |
} | |
} | |
index += columnSize | |
result += next | |
if index < inputLength && next != " " { | |
result += "\n" | |
} | |
if index > len(input) { | |
return result | |
} | |
} | |
} |
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
package main | |
import ( | |
"strings" | |
"testing" | |
) | |
func TestSingleWord(t *testing.T) { | |
// 0 | |
assertEquals("", 0, "", t) | |
assertEquals("1", 0, "1", t) | |
assertEquals("22", 0, "22", t) | |
assertEquals("999999999", 0, "999999999", t) | |
// 1 | |
assertEquals("", 1, "", t) | |
assertEquals("1", 1, "1", t) | |
assertEquals("22", 1, "2\n2", t) | |
assertEquals("333", 1, "3\n3\n3", t) | |
assertEquals("999999999", 1, "9\n9\n9\n9\n9\n9\n9\n9\n9", t) | |
//2 | |
assertEquals("", 2, "", t) | |
assertEquals("1", 2, "1", t) | |
assertEquals("22", 2, "22", t) | |
assertEquals("333", 2, "33\n3", t) | |
assertEquals("4444", 2, "44\n44", t) | |
assertEquals("55555", 2, "55\n55\n5", t) | |
assertEquals("999999999", 2, "99\n99\n99\n99\n9", t) | |
//3 | |
assertEquals("", 3, "", t) | |
assertEquals("1", 3, "1", t) | |
assertEquals("22", 3, "22", t) | |
assertEquals("333", 3, "333", t) | |
assertEquals("4444", 3, "444\n4", t) | |
assertEquals("55555", 3, "555\n55", t) | |
assertEquals("999999999", 3, "999\n999\n999", t) | |
//9 | |
assertEquals("999999999", 9, "999999999", t) | |
assertEquals("0000000000", 9, "000000000\n0", t) | |
} | |
func TestCompoundWords(t *testing.T) { | |
//0: 1 1 | |
assertEquals("1 1", 0, "1 1", t) | |
//1: 1 1 | |
assertEquals("1 1", 1, "1\n1", t) | |
//1: 1 1 1 | |
assertEquals("1 1 1", 1, "1\n1\n1", t) | |
//1: 1 22 | |
assertEquals("1 22", 1, "1\n2\n2", t) | |
//1: 1 22 1 | |
assertEquals("1 22 1", 1, "1\n2\n2\n1", t) | |
//1: 22 22 1 | |
assertEquals("22 22 1", 1, "2\n2\n2\n2\n1", t) | |
//1: 999999999 999999999 999999999 | |
assertEquals("999999999 999999999 999999999", 1, | |
"9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9\n9", t) | |
//2: 1 1 | |
assertEquals("1 1", 2, "1 \n1", t) | |
//2: 1 1 1 | |
assertEquals("1 1 1", 2, "1 \n1 \n1", t) | |
//2: 1 22 | |
assertEquals("1 22", 2, "1 \n22", t) | |
//2: 1 22 1 | |
assertEquals("1 22 1", 2, "1 \n22\n 1", t) | |
//2: 1 22 22 | |
assertEquals("1 22 22", 2, "1 \n22\n22", t) | |
//2: 22 22 1 | |
assertEquals("22 22 1", 2, "22\n22\n 1", t) | |
//2: 22 22 22 | |
assertEquals("22 22 22", 2, "22\n22\n22", t) | |
//2: 22 1 22 | |
assertEquals("22 1 22", 2, "22\n1 \n22", t) | |
//2: 22 1 1 22 | |
assertEquals("22 1 1 22", 2, "22\n1 \n1 \n22", t) | |
} | |
func TesetYouWirteAClass(t *testing.T) { | |
assertEquals("You write a clas called Wrapper", 9, "You\nwrite a\nclass\ncalled\nWrapper", t) | |
assertEquals("You write a class called Wrapper", 14, "You write a\nclass called\nWrapper", t) | |
} | |
func TestEmpty(t *testing.T) { | |
assertEquals("", 1, "", t) | |
} | |
func TestDontWrapShortWord(t *testing.T) { | |
assertEquals("word", 4, "word", t) | |
} | |
func TestSplitLongWord(t *testing.T) { | |
assertEquals("word", 2, "wo\nrd", t) //# 3. simple regex with . | |
assertEquals("word", 3, "wor\nd", t) | |
assertEquals("wordword", 3, "wor\ndwo\nrd", t) | |
} | |
func TestWrapPreviousSpace(t *testing.T) { | |
assertEquals("word word", 5, "word \nword", t) //# 4. first regex with \S, second with \s | |
assertEquals("word word", 6, "word \nword", t) | |
} | |
func TestWrapExactSpace(t *testing.T) { | |
assertEquals("word word", 4, "word\nword", t) //# 5. add (?=\S) to first regex | |
} | |
func TestWrapLastSpace(t *testing.T) { | |
assertEquals("word word word", 6, "word \nword \nword", t) | |
assertEquals("word word word", 11, "word word \nword", t) | |
} | |
func assertEquals(input string, columnSize int, expected string, t *testing.T) { | |
obtained := WordWrap(input, columnSize) | |
if expected != obtained { | |
t.Errorf(`WordWrap("%v", %d) Expected "%v", got "%v"`, | |
strings.Replace(input, " ", "·", -1), | |
columnSize, | |
strings.Replace(expected, " ", "·", -1), | |
strings.Replace(obtained, " ", "·", -1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment