Skip to content
This repository has been archived by the owner on Jun 20, 2020. It is now read-only.

Commit

Permalink
Reading grid from string
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed May 27, 2011
1 parent 21adeaa commit ebcb3c7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
21 changes: 21 additions & 0 deletions problem_011/11.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"container/vector"
"fmt"
"strconv"
"strings"
)

func ConvertGridToString(grid *vector.Vector) string {
Expand All @@ -27,6 +29,25 @@ func ConvertGridToString(grid *vector.Vector) string {
return output
}

func ReadGridFromString(input string) *vector.Vector {
grid := new(vector.Vector)
lines := strings.Split(input, "\n", -1)

for _, line := range lines {
lineVector := new(vector.IntVector)

numbers := strings.Split(line, " ", -1)
for _, number := range numbers {
number, _ := strconv.Atoi(number)
lineVector.Push(number)
}

grid.Push(lineVector)
}

return grid
}

func GridProduct(grid *vector.Vector, elements int) uint {
var greater, product uint
var lineLength int
Expand Down
39 changes: 39 additions & 0 deletions problem_011/11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ func AssertGridProduct(t *testing.T, grid *vector.Vector, expected uint) {
}
}

func AreGridEquals(grid1, grid2 *vector.Vector) bool {
if grid1.Len() != grid2.Len() {
return false
}

for i := 0; i < grid1.Len(); i++ {
line1 := grid1.At(i).(*vector.IntVector)
line2 := grid2.At(i).(*vector.IntVector)
for j := 0; j < line1.Len(); j++ {
if line1.At(j) != line2.At(j) {
return false
}
}
}

return true
}

func TestConvertGridToString(t *testing.T) {
firstLine := new(vector.IntVector)
firstLine.Push(10)
Expand All @@ -34,6 +52,27 @@ func TestConvertGridToString(t *testing.T) {
}
}

func TestConvertGridFromString(t *testing.T) {
grid := "10 15\n8 5"

firstLine := new(vector.IntVector)
firstLine.Push(10)
firstLine.Push(15)

secondLine := new(vector.IntVector)
secondLine.Push(8)
secondLine.Push(5)

expected := new(vector.Vector)
expected.Push(firstLine)
expected.Push(secondLine)
got := ReadGridFromString(grid)

if !AreGridEquals(expected, got) {
t.Errorf("Expected: %q\nGot: %q", expected, got)
}
}

func TestGridProductOfHorizontalLine(t *testing.T) {
firstLine := new(vector.IntVector)
firstLine.Push(10)
Expand Down

0 comments on commit ebcb3c7

Please sign in to comment.