Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more solutions for go #19

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bd8a9f0
Add problem 1 of chapter 4
motomux Feb 25, 2017
7c0f393
Add problem 2 of chapter 4
motomux Feb 25, 2017
c662de0
Add problem 3 of chapter 4
motomux Feb 25, 2017
a473293
Add problem 4 of chapter 4
motomux Feb 25, 2017
81f5d51
Add problem 5 of chapter 4
motomux Feb 25, 2017
f5470f8
Add problem 6 of chapter 4
motomux Feb 25, 2017
6861c66
Update tests for chapter 1 & 3 so it works with golang >= 1.10
pietromenna Sep 11, 2018
c3be13f
Merge pull request #1 from motomux/chapter4/problem1/new
rokii Feb 24, 2019
7006a29
Merge pull request #2 from motomux/chapter4/problem2/new
rokii Feb 24, 2019
e5ffb2b
Merge pull request #3 from motomux/chapter4/problem3/new
rokii Feb 24, 2019
f08b5a7
Merge pull request #4 from motomux/chapter4/problem4/new
rokii Feb 24, 2019
3d346c2
Merge pull request #5 from motomux/chapter4/problem5/new
rokii Feb 24, 2019
eb05402
Merge pull request #6 from motomux/chapter4/problem6/new
rokii Feb 24, 2019
6bd12bf
Merge pull request #7 from pietromenna/master
rokii Feb 24, 2019
a8dcaac
add problem 1 - 2nd question of charter 1
Feb 24, 2019
b4eb8e7
add problem 2 - 2nd solution of charter 1
Feb 25, 2019
c6a08c1
add problem 3 - solution 3 of charter 1
Feb 26, 2019
29182e5
add problem 4 - solution B of charter 1
Feb 26, 2019
60cae53
add problem 5 - solution B of charter 1
Feb 28, 2019
473d6d1
add problem 6 - solution B of charter 1
Mar 10, 2019
a448209
add more case
Jul 17, 2019
0bee346
add o(1) space for 1.8
Jul 17, 2019
4be92a6
add case for 2.4
Aug 2, 2019
dd6f316
add case for 2.5
Aug 2, 2019
6ecba03
add case
Aug 10, 2019
23843e7
add case4.8
Aug 10, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/chapter1/problem1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ func IsUnique(input string) bool {
}
return true
}

// Using no additional data structures. Assumes the string only contains alphabet.
func IsUniqueB(input string) bool {
seen := 0
for _, r := range input {
rBit := 1 << uint32(r-'A')
if (seen & rBit) != 0 {
return false
}

seen |= rBit

}
return true
}
21 changes: 20 additions & 1 deletion src/chapter1/problem1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,32 @@ func TestIsUnique(t *testing.T) {
}{
{"abcd", true},
{"abcc", false},
{"asbckmdsf", false},
{" ", false},
{" ", true},
{"", true},
}
for _, c := range cases {
actual := IsUnique(c.input)
if actual != c.expected {
t.Fatalf("Input %s. Expected: %b, actual: %b\n", c.input, c.expected, actual)
t.Fatalf("Input %s. Expected: %t, actual: %t\n", c.input, c.expected, actual)
}
}
}

func TestIsUniqueB(t *testing.T) {
cases := []struct {
input string
expected bool
}{
{"abcd", true},
{"abcc", false},
{"asbckmdsf", false},
}
for _, c := range cases {
actual := IsUniqueB(c.input)
if actual != c.expected {
t.Fatalf("Input %s. Expected: %t, actual: %t\n", c.input, c.expected, actual)
}
}
}
18 changes: 18 additions & 0 deletions src/chapter1/problem2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ func ArePermutations(input1, input2 string) bool {
}
return true
}

func ArePermutationsB(input1, input2 string) bool {
if len(input1) != len(input2) {
return false
}
counts := make(map[rune]int)
for _, r := range input1 {
counts[r]++
}
for _, r := range input2 {
counts[r]--
if counts[r] < 0 {
return false
}
}

return true
}
25 changes: 24 additions & 1 deletion src/chapter1/problem2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,30 @@ func TestArePermutations(t *testing.T) {
for _, c := range cases {
actual := ArePermutations(c.input1, c.input2)
if actual != c.expected {
t.Fatalf("Inputs %s, %s. Expected: %b, actual: %b\n",
t.Fatalf("Inputs %s, %s. Expected: %t, actual: %t\n",
c.input1, c.input2, c.expected, actual)
}
}
}

func TestArePermutationsB(t *testing.T) {
cases := []struct {
input1 string
input2 string
expected bool
}{
{"abcd", "abcd", true},
{"abcd", "abdc", true},
{"abcc", "ccbb", false},
{"abcc", "abcc ", false},
{"abccc", "abccb", false},
{" ", " ", true},
{"", "", true},
}
for _, c := range cases {
actual := ArePermutationsB(c.input1, c.input2)
if actual != c.expected {
t.Fatalf("Inputs %s, %s. Expected: %t, actual: %t\n",
c.input1, c.input2, c.expected, actual)
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/chapter1/problem3.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,30 @@ func URLifySlice(input []rune) {
}
}
}

func URLifySliceWithLength(input []rune, realLength int) []rune {
inWord := false

slowPtr := len(input) - 1
movedChar := 0

for i := slowPtr; i >= 0 && movedChar < realLength; i-- {
if input[i] == rune(' ') {
if inWord {
input[slowPtr-2] = rune('%')
input[slowPtr-1] = rune('2')
input[slowPtr] = rune('0')
slowPtr -= 3
movedChar++
}
} else {
if !inWord {
inWord = true
}
input[slowPtr] = input[i]
movedChar++
slowPtr--
}
}
return input[slowPtr+1:]
}
19 changes: 19 additions & 0 deletions src/chapter1/problem3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,22 @@ func TestURLifySlice(t *testing.T) {
}
}
}

func TestURLifySliceB(t *testing.T) {
cases := []struct {
input []rune
expected []rune
realLength int
}{
{[]rune("hello my name is "), []rune("hello%20my%20name%20is"), 16},
{[]rune("hello"), []rune("hello"), 5},
{[]rune(" hello my name is "), []rune("%20hello%20my%20name%20is"), 17},
{[]rune(" hello my name is "), []rune("%20hello%20my%20name%20is"), 17},
}
for _, c := range cases {
result := URLifySliceWithLength(c.input, c.realLength)
if !reflect.DeepEqual(c.expected, result) {
t.Fatalf("Expected: %s, actual: %s\n", string(c.expected), string(result))
}
}
}
35 changes: 35 additions & 0 deletions src/chapter1/problem4.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,38 @@ func IsPalindromePerm(input string) bool {
}
return true
}

// with space O(1)
func IsPalindromePermB(input string) bool {
bitVector := 0

for _, r := range input {
index := getIndexValue(r)
if index == -1 {
continue
}
mask := 1 << uint8(index)
if (bitVector & mask) == 0 {
bitVector |= mask
} else {
bitVector &= ^mask
}
}
return (bitVector & (bitVector - 1)) == 0

}

func getIndexValue(r rune) int8 {

if r < 'A' || r > 'z' {
return int8(-1)
}

if r >= 'a' {
return int8(r - 'a')
} else if r >= 'A' {
return int8(r - 'A')
}

return int8(-1)
}
23 changes: 22 additions & 1 deletion src/chapter1/problem4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,28 @@ func TestIsPalindromePerm(t *testing.T) {
for _, c := range cases {
actual := IsPalindromePerm(c.input)
if actual != c.expected {
t.Fatalf("Input %s. Expected: %b, actual: %b\n", c.input, c.expected, actual)
t.Fatalf("Input %s. Expected: %t, actual: %t\n", c.input, c.expected, actual)
}
}
}

func TestIsPalindromePermB(t *testing.T) {
cases := []struct {
input string
expected bool
}{
{"My rats live on no evil star", false}, // not a palindrome
{"Rats live on no evil star", true}, // normal palindrome
{"amanaplanacanalpanama", true}, // normal palindrome
{"amanalpancaaanplanama", true}, // jumbled palindrome
{"amanaplanacanalpanamab", false}, // not a palindrome
{"a", true},
{"", true},
}
for _, c := range cases {
actual := IsPalindromePermB(c.input)
if actual != c.expected {
t.Fatalf("Input %s. Expected: %t, actual: %t\n", c.input, c.expected, actual)
}
}
}
32 changes: 32 additions & 0 deletions src/chapter1/problem5.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,35 @@ func AreOneEditAway(input1, input2 string) bool {
return oneRemovalAway(input1, input2)
}
}

func AreOneEditAwayB(input1, input2 string) bool {

longerOne, shorterOne := input1, input2
if len(longerOne) < len(shorterOne) {
longerOne = input2
shorterOne = input1
}
longerLength, shorterLength := len(longerOne), len(shorterOne)
if (longerLength - shorterLength) > 1 {
return false
}

diffFound := false
for i, j := 0, 0; i < longerLength && j < shorterLength; i++ {

if longerOne[i] != shorterOne[j] {
if diffFound {
return false
} else {
diffFound = true
}
if longerLength == shorterLength {
j++
}
} else {
j++
}
}

return true
}
32 changes: 31 additions & 1 deletion src/chapter1/problem5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,44 @@ func TestAreOneEditAway(t *testing.T) {
{"abcd", "abcdef", false},
{"abcde", "abcd", true},
{"abcdef", "abcd", false},
{"abbc", "abcd", false},
{"abbcd", "abcd", true},
{" ", "", true},
{"", " ", true},
{"", "", true},
}
for _, c := range cases {
actual := AreOneEditAway(c.input1, c.input2)
if actual != c.expected {
t.Fatalf("Inputs %s, %s. Expected: %b, actual: %b\n",
t.Fatalf("Inputs %s, %s. Expected: %t, actual: %t\n",
c.input1, c.input2, c.expected, actual)
}
}
}

func TestAreOneEditAwayB(t *testing.T) {
cases := []struct {
input1 string
input2 string
expected bool
}{
{"abcd", "abcd", true},
{"abcd", "abcc", true},
{"abcd", "accc", false},
{"abcd", "abcde", true},
{"abcd", "abcdef", false},
{"abcde", "abcd", true},
{"abcdef", "abcd", false},
{"abbc", "abcd", false},
{"abbcd", "abcd", true},
{" ", "", true},
{"", " ", true},
{"", "", true},
}
for _, c := range cases {
actual := AreOneEditAwayB(c.input1, c.input2)
if actual != c.expected {
t.Fatalf("Inputs %s, %s. Expected: %t, actual: %t\n",
c.input1, c.input2, c.expected, actual)
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/chapter1/problem6.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chapter1

import (
"bytes"
"strconv"
)

Expand All @@ -23,3 +24,28 @@ func BasicCompress(input string) string {
return compStr
}
}

func BasicCompressB(input string) string {
var compressed bytes.Buffer
counter := 1
inputLen := len(input)
for i := 1; i < inputLen; i++ {

if input[i-1] == input[i] {
counter++
} else {
compressed.WriteByte(input[i-1])
compressed.WriteString(strconv.Itoa(counter))
counter = 1
}
if compressed.Len() >= inputLen {
return input
}
}
compressed.WriteByte(input[inputLen-1])
compressed.WriteString(strconv.Itoa(counter))
if compressed.Len() >= inputLen {
return input
}
return compressed.String()
}
18 changes: 18 additions & 0 deletions src/chapter1/problem6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ func TestBasicCompress(t *testing.T) {
}
}
}
func TestBasicCompressB(t *testing.T) {
cases := []struct {
input string
expected string
}{
{"aaaabbbcdddd", "a4b3c1d4"},
{"aaaabbbbbbbbbbbcdddd", "a4b11c1d4"},
{"aaaabbbbbbbbbbbcd", "a4b11c1d1"},
{"abcd", "abcd"}, // compressed is longer, expect input
{"aabbccdd", "aabbccdd"}, // compressed is longer, expect input
}
for _, c := range cases {
actual := BasicCompressB(c.input)
if actual != c.expected {
t.Fatalf("Input %s. Expected: %s, actual: %s\n", c.input, c.expected, actual)
}
}
}
15 changes: 15 additions & 0 deletions src/chapter1/problem7_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ func TestMatrixRotate(t *testing.T) {
[]int{15, 11, 7, 3},
[]int{16, 12, 8, 4},
},
},{
[][]int{
[]int{1, 2, 3, 4,5},
[]int{5, 6, 7, 8,9},
[]int{9, 10, 11, 12,13},
[]int{13, 14, 15, 16,17},
[]int{23, 24, 25, 26,27},
},
[][]int{
[]int{23,13, 9, 5, 1},
[]int{24,14, 10, 6, 2},
[]int{25,15, 11, 7, 3},
[]int{26,16, 12, 8, 4},
[]int{27,17, 13, 9, 5},
},
},
}
for _, c := range cases {
Expand Down
Loading