Last active
March 14, 2021 09:27
-
-
Save inotnako/586bd952eb52cfe9b8f94d1b191891ff to your computer and use it in GitHub Desktop.
golang zipper
This file contains hidden or 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 ( | |
"io" | |
"io/ioutil" | |
"os" | |
"strings" | |
) | |
func main() { | |
r := strings.NewReader(`-150`) | |
if err := backwards(r, os.Stdout); err != nil { | |
panic(err) | |
} | |
} | |
const ( | |
Minus = `-` | |
Zero = `0` | |
) | |
func backwards(in io.Reader, out io.Writer) error { | |
input, err := ioutil.ReadAll(in) | |
if err != nil { | |
return err | |
} | |
var ( | |
collection []string | |
) | |
for i, symbol := range strings.TrimSpace(string(input)) { | |
if i == 0 && string(symbol) == Minus { | |
_, _ = out.Write([]byte(Minus)) | |
continue | |
} | |
collection = append(collection, string(symbol)) | |
} | |
startWriteValue := false | |
for i := len(collection) - 1; i >= 0; i-- { | |
if collection[i] == Zero && !startWriteValue { | |
continue | |
} | |
_, _ = out.Write([]byte(collection[i])) | |
if !startWriteValue { | |
startWriteValue = true | |
} | |
} | |
return nil | |
} |
This file contains hidden or 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 ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
r := strings.NewReader(`7 | |
6 4 1 2 3`) | |
if err := missIDs(r, os.Stdout, 2); err != nil { | |
panic(err) | |
} | |
} | |
func missIDs(in io.Reader, out io.Writer, expectedMissCount int) error { | |
input, err := ioutil.ReadAll(in) | |
if err != nil { | |
return err | |
} | |
inputs := strings.Split(string(input), "\n") | |
if len(inputs) < 2 { | |
return fmt.Errorf("expected 2 inputes rows, got - %d", len(inputs)) | |
} | |
countAll, err := strconv.Atoi(inputs[0]) | |
if err != nil { | |
return err | |
} | |
gotIDs := strings.Split(inputs[1], " ") | |
if len(gotIDs) != countAll-2 { | |
return fmt.Errorf("expected %d len of row, got - %d", countAll-expectedMissCount, len(gotIDs)) | |
} | |
index := make(map[string]bool, countAll) | |
for _, id := range gotIDs { | |
index[id] = true | |
} | |
var match int | |
for i := 1; i <= countAll; i++ { | |
if ok := index[strconv.Itoa(i)]; !ok { | |
msg := strconv.Itoa(i) | |
if match > 0 { | |
msg = " " + msg | |
} | |
_, _ = out.Write([]byte(msg)) | |
match++ | |
if match == expectedMissCount { | |
break | |
} | |
} | |
} | |
return nil | |
} |
This file contains hidden or 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 ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
if err := zipper(os.Stdin, os.Stdout); err != nil { | |
panic(err) | |
} | |
} | |
func zipper(in io.Reader, out io.Writer) error { | |
input, err := ioutil.ReadAll(in) | |
if err != nil { | |
return err | |
} | |
inputs := strings.Split(string(input), "\n") | |
if len(inputs) < 3 { | |
return fmt.Errorf("expected 3 inputes rows, got - %d", len(inputs)) | |
} | |
lenOfRange, err := strconv.Atoi(inputs[0]) | |
if err != nil { | |
return err | |
} | |
firstRange := strings.Split(inputs[1], " ") | |
if len(firstRange) != lenOfRange { | |
return fmt.Errorf("expected %d len of first row, got - %d", lenOfRange, len(firstRange)) | |
} | |
secondRange := strings.Split(inputs[2], " ") | |
if len(secondRange) != lenOfRange { | |
return fmt.Errorf("expected %d len of second row, got - %d", lenOfRange, len(secondRange)) | |
} | |
delimetr := []byte(` `) | |
for i := 0; i < lenOfRange; i++ { | |
if i != 0 { | |
_, _ = out.Write(delimetr) | |
} | |
_, _ = out.Write([]byte(firstRange[i] + " " + secondRange[i])) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment