Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 253
Good morning! Here's your coding interview problem for today.

This problem was asked by PayPal.

Given a string and a number of lines k, print the string in zigzag form. In zigzag, characters are printed out diagonally from top left to bottom right until reaching the kth line, then back up to top right, and so on.

For example, given the sentence `"thisisazigzag"` and `k = 4`, you should print:
```
t     a     g
 h   s z   a
  i i   i z
   s     g
```

Pull request description:
Day253
Resolves #521.

Patch:
diff --git a/README.md b/README.md
index ade3bcf..62b0659 100644
--- a/README.md
+++ b/README.md
@@ -263,3 +263,4 @@ problems from
 * [Day 249](https://github.com/vaskoz/dailycodingproblem-go/issues/514)
 * [Day 251](https://github.com/vaskoz/dailycodingproblem-go/issues/517)
 * [Day 252](https://github.com/vaskoz/dailycodingproblem-go/issues/519)
+* [Day 253](https://github.com/vaskoz/dailycodingproblem-go/issues/521)
diff --git a/day253/problem.go b/day253/problem.go
new file mode 100644
index 0000000..5a49cc2
--- /dev/null
+++ b/day253/problem.go
@@ -0,0 +1,47 @@
+package day253
+
+import (
+	"fmt"
+	"io"
+	"os"
+	"strings"
+)
+
+var (
+	// nolint
+	out io.Writer = os.Stdout
+)
+
+// PrintZigZag prints k lines of a zig-zag pattern for
+// the letters given.
+func PrintZigZag(letters string, k int) {
+	lines := make([]strings.Builder, k)
+	pos := 0
+	goingUp := false
+	for _, r := range letters {
+		switch {
+		case pos == k:
+			goingUp = true
+			pos -= 2
+		case pos == -1:
+			goingUp = false
+			pos += 2
+		}
+		for i := range lines {
+			if i == pos {
+				lines[i].WriteRune(r)
+			} else {
+				lines[i].WriteRune(' ')
+			}
+		}
+		switch {
+		case goingUp:
+			pos--
+		case !goingUp:
+			pos++
+		}
+	}
+	for _, line := range lines {
+		fmt.Fprintln(out, line.String())
+	}
+}


Test patch:
diff --git a/day253/problem_test.go b/day253/problem_test.go
new file mode 100644
index 0000000..9c077b7
--- /dev/null
+++ b/day253/problem_test.go
@@ -0,0 +1,38 @@
+package day253
+
+import (
+	"bytes"
+	"testing"
+)
+
+// nolint
+var testcases = []struct {
+	letters  string
+	k        int
+	expected string
+}{
+	{"thisisazigzag", 4, "t     a     g\n h   s z   a \n  i i   i z  \n   s     g   \n"},
+}
+
+func TestPrintZigZag(t *testing.T) {
+	// don't run in parallel due to global var
+	for _, tc := range testcases {
+		buff := new(bytes.Buffer)
+		out = buff
+		PrintZigZag(tc.letters, tc.k)
+		if result := buff.String(); result != tc.expected {
+			t.Errorf("Expected \n%s, got \n%s", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkPrintZigZag(b *testing.B) {
+	// don't run in parallel due to global var
+	buff := new(bytes.Buffer)
+	out = buff
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			PrintZigZag(tc.letters, tc.k)
+		}
+	}
+}