Created
August 12, 2016 21:04
-
-
Save devster31/f78e40ad72ac3ec17ebf98a8c68d0fc8 to your computer and use it in GitHub Desktop.
r/dailyprogrammer 278 part 1
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 ( | |
"bytes" | |
"fmt" | |
"strings" | |
) | |
type Interleave struct { | |
opType, first, second string | |
} | |
func LinesToArray(s string) []string { | |
out := make([]string, 0) | |
for _, line := range strings.Split(s, "\n") { | |
pend := strings.TrimSpace(string(line)) | |
out = append(out, pend) | |
} | |
return out | |
} | |
func StrToArray(s string) []string { | |
out := make([]string, 0) | |
for _, r := range s { | |
out = append(out, string(r)) | |
} | |
return out | |
} | |
func StrArrayPrint(s []string) string { | |
out := "" | |
for i, v := range s { | |
if i < len(s)-1 { | |
out += fmt.Sprintf("%v, ", v) | |
} else { | |
out += fmt.Sprintf("%v", v) | |
} | |
} | |
return out | |
} | |
func ByTwo(s string) []string { | |
var res string = "" | |
a := []rune(s) | |
out := make([]string, 0) | |
for i, r := range a { | |
res = res + string(r) | |
if i > 0 && (i+1)%2 == 0 { | |
out = append(out, res) | |
res = "" | |
} | |
} | |
return out | |
} | |
func (a Interleave) Bracket() string { | |
var l []string | |
if strings.Contains(a.first, "\n") { | |
l = LinesToArray(a.first) | |
} else { | |
l = StrToArray(a.first) | |
} | |
r := a.second | |
if len(r)%2 != 0 { | |
tempr := []rune(r) | |
tempr = append(tempr, '_') | |
r = string(tempr) | |
} | |
brackets := ByTwo(r) | |
var long int | |
if len(brackets) > len(l) { | |
long = len(brackets) | |
} else { | |
long = len(l) | |
} | |
out := make([]string, 0) | |
for i := 0; i < long; i++ { | |
temp := string(brackets[i%len(brackets)][0]) + string(l[i%len(l)]) + string(brackets[i%len(brackets)][1]) | |
out = append(out,temp) | |
} | |
return StrArrayPrint(out) | |
} | |
func (a Interleave) Weave() string { | |
l, r := LinesToArray(a.first), LinesToArray(a.second) | |
out := make([]string, 0) | |
for i := range r { | |
out = append(out, r[i%len(r)]) | |
if i <= len(l) { | |
out = append(out, l[i%len(l)]) | |
} | |
} | |
return StrArrayPrint(out) | |
} | |
func main() { | |
input := []string{ | |
"Bracket\n+-\n\n234567", | |
"Bracket\n2+3\n4-5\n6+7\n\n()", | |
"Weave\n*\n\n(2+3)\n(4-5)\n(6+7)", | |
"Bracket\n-+*\n\n123456789", | |
"Bracket\nab\n\n()[]{}", | |
"Bracket\nabc\n\n()", | |
} | |
data := make([]Interleave, 0) | |
for _, el := range input { | |
p := Interleave{} | |
prim := strings.SplitN(el, "\n", 2) | |
p.opType = strings.TrimSpace(prim[0]) | |
s := strings.SplitN(prim[1], "\n\n", 2) | |
p.first, p.second = strings.TrimSpace(s[0]), strings.TrimSpace(s[1]) | |
data = append(data, p) | |
} | |
for _, ins := range data { | |
var out string | |
if ins.opType == "Bracket" { | |
out = ins.Bracket() | |
} else if ins.opType == "Weave" { | |
out = ins.Weave() | |
} | |
fmt.Printf("Input:\n%v\n%v\n\n%v\nOutput: %v\n", ins.opType, ins.first, ins.second, out) | |
} | |
} |
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
Input: | |
Bracket | |
+- | |
234567 | |
Output: 2+3, 4-5, 6+7 | |
Input: | |
Bracket | |
2+3 | |
4-5 | |
6+7 | |
() | |
Output: (2+3), (4-5), (6+7) | |
Input: | |
Weave | |
* | |
(2+3) | |
(4-5) | |
(6+7) | |
Output: (2+3), *, (4-5), *, (6+7) | |
Input: | |
Bracket | |
-+* | |
123456789 | |
Output: 1-2, 3+4, 5*6, 7-8, 9+_ | |
Input: | |
Bracket | |
ab | |
()[]{} | |
Output: (a), [b], {a} | |
Input: | |
Bracket | |
abc | |
() | |
Output: (a), (b), (c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment