From 1c1ba03ffb1a50d80e21eef753e8b71754212dfe Mon Sep 17 00:00:00 2001 From: fabiante Date: Tue, 13 Oct 2020 19:45:19 +0200 Subject: [PATCH] Initial code --- main.go | 30 ++++++++++++++++++++++++++++++ sponge-case/sponge-case.go | 23 +++++++++++++++++++++++ sponge-case/sponge-case_test.go | 10 ++++++++++ 3 files changed, 63 insertions(+) create mode 100644 main.go create mode 100644 sponge-case/sponge-case.go create mode 100644 sponge-case/sponge-case_test.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..3cbf8e3 --- /dev/null +++ b/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "bufio" + "fmt" + "os" + sponge_case "sponge-case/sponge-case" +) + +func main() { + if len(os.Args) == 2 { + applyToArgs() + } else { + applyToStdin() + } +} + +func applyToStdin() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + fmt.Println(sponge_case.ApplyStr(scanner.Text())) + } + if err := scanner.Err(); err != nil { + fmt.Println(err) + } +} + +func applyToArgs() { + fmt.Println(sponge_case.ApplyStr(os.Args[1])) +} diff --git a/sponge-case/sponge-case.go b/sponge-case/sponge-case.go new file mode 100644 index 0000000..277bbef --- /dev/null +++ b/sponge-case/sponge-case.go @@ -0,0 +1,23 @@ +package sponge_case + +import ( + "math/rand" + "unicode" +) + +func ApplyStr(in string) string { + out := make([]rune, len(in)) + var newChar rune + + for pos, char := range []rune(in) { + toUpper := rand.Float32() > 0.5 + if toUpper { + newChar = unicode.ToUpper(char) + } else { + newChar = unicode.ToLower(char) + } + out[pos] = newChar + } + + return string(out) +} diff --git a/sponge-case/sponge-case_test.go b/sponge-case/sponge-case_test.go new file mode 100644 index 0000000..14b8682 --- /dev/null +++ b/sponge-case/sponge-case_test.go @@ -0,0 +1,10 @@ +package sponge_case + +import ( + "fmt" + "testing" +) + +func TestApplyStr(t *testing.T) { + fmt.Println(ApplyStr("Das ist wirklich nicht witzig!")) +}