-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve the "Reverse all the words in a sentence" problem
- Loading branch information
Showing
7 changed files
with
128 additions
and
22 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
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
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,30 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package epi | ||
|
||
import "testing" | ||
|
||
func TestRandStr(t *testing.T) { | ||
for _, test := range []struct { | ||
n int | ||
t string | ||
}{ | ||
{10, "abc"}, | ||
} { | ||
if got := len(randStr(test.n, test.t)); got != test.n { | ||
t.Errorf("len(randStr(%d, %q) = %d; want %d", test.n, test.t, got, test.n) | ||
} | ||
} | ||
} | ||
|
||
func benchRandStr(b *testing.B, size int) { | ||
for i := 0; i < b.N; i++ { | ||
randStr(size, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
} | ||
} | ||
|
||
func BenchmarkRandStr1e2(b *testing.B) { benchRandStr(b, 1e2) } | ||
func BenchmarkRandStr1e4(b *testing.B) { benchRandStr(b, 1e4) } | ||
func BenchmarkRandStr1e6(b *testing.B) { benchRandStr(b, 1e6) } |
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
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,31 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package epi | ||
|
||
// reverseBytes reverse elements of a[s:e] in a. | ||
func reverseBytes(a []byte, s, e int) { | ||
for e > s { | ||
a[s], a[e] = a[e], a[s] | ||
s++ | ||
e-- | ||
} | ||
} | ||
|
||
// ReverseWords returns a new string containing the words from s in reverse order. | ||
func ReverseWords(s string) string { | ||
r := []byte(s) | ||
|
||
reverseBytes(r, 0, len(s)-1) // Reverse whole sentence. | ||
p := 0 | ||
for q := p; q < len(r); q++ { // Reverse each world in the reversed sentence. | ||
if r[q] == ' ' { | ||
reverseBytes(r, p, q-1) // q-1 exclude the ' ' character from reversal. | ||
p = q + 1 | ||
} | ||
} | ||
reverseBytes(r, p, len(r)-1) // Reverse the last world. | ||
|
||
return string(r) | ||
} |
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,43 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package epi | ||
|
||
import "testing" | ||
|
||
func TestReverseWords(t *testing.T) { | ||
for _, test := range []struct { | ||
in string | ||
want string | ||
}{ | ||
{}, | ||
{"a", "a"}, | ||
{"b ", " b"}, | ||
{"a b c", "c b a"}, | ||
{"a b c d", "d c b a"}, | ||
{"Alice like Bob", "Bob like Alice"}, | ||
{"The quick brown fox jumps over the lazy dog", "dog lazy the over jumps fox brown quick The"}, | ||
|
||
// Test more then one space. | ||
{"aa bb", "bb aa"}, | ||
} { | ||
if got := ReverseWords(test.in); got != test.want { | ||
t.Errorf("ReverseWorlds(%q) = %q; want %q", test.in, got, test.want) | ||
} | ||
} | ||
} | ||
|
||
func benchReverseWords(b *testing.B, size int) { | ||
b.StopTimer() | ||
for i := 0; i < b.N; i++ { | ||
s := randStr(size, "The quick brown fox jumps over the lazy dog") // Pangram. | ||
b.StartTimer() | ||
ReverseWords(s) | ||
b.StopTimer() | ||
} | ||
} | ||
|
||
func BenchmarkReverseWords1e1(b *testing.B) { benchReverseWords(b, 1e1) } | ||
func BenchmarkReverseWords1e2(b *testing.B) { benchReverseWords(b, 1e2) } | ||
func BenchmarkReverseWords1e3(b *testing.B) { benchReverseWords(b, 1e3) } |
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