From 392b2213317e3b8f2eeae58522fd79624936e78f Mon Sep 17 00:00:00 2001 From: Christopher Fremond Date: Fri, 6 Nov 2020 03:06:52 +0000 Subject: [PATCH] Adding of a new exercise printrot. The goal: printing the alphabet from a chosen letter and going around all the letters. --- go/tests/prog/printrot_prog/main.go | 33 ++++++++++++++++++++++ go/tests/prog/printrot_test/main.go | 44 +++++++++++++++++++++++++++++ subjects/printrot/README.md | 27 ++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 go/tests/prog/printrot_prog/main.go create mode 100644 go/tests/prog/printrot_test/main.go create mode 100644 subjects/printrot/README.md diff --git a/go/tests/prog/printrot_prog/main.go b/go/tests/prog/printrot_prog/main.go new file mode 100644 index 00000000..7d845890 --- /dev/null +++ b/go/tests/prog/printrot_prog/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "os" +) + +func printAnswer(letter rune) { + answer := []rune{} + for i := letter; i <= 'z'; i++ { + answer = append(answer, i) + } + for j := 'a'; j < letter; j++ { + answer = append(answer, j) + } + fmt.Println(string(answer)) +} + +func main() { + if len(os.Args) == 2 { + letter := os.Args[1] + if len(letter) == 1 { + letterConverted := []rune(letter) + if letterConverted[0] >= 'a' && letterConverted[0] <= 'z' { + printAnswer(letterConverted[0]) + } + } else { + fmt.Println() + } + } else { + fmt.Println() + } +} diff --git a/go/tests/prog/printrot_test/main.go b/go/tests/prog/printrot_test/main.go new file mode 100644 index 00000000..09fdae88 --- /dev/null +++ b/go/tests/prog/printrot_test/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "lib" + "strings" +) + +func main() { + table := string{} + table = append(table, "A") + table = append(table, "a") + table = append(table, "b") + table = append(table, "c") + table = append(table, "d") + table = append(table, "e") + table = append(table, "f") + table = append(table, "g") + table = append(table, "h") + table = append(table, "i") + table = append(table, "j") + table = append(table, "k") + table = append(table, "l") + table = append(table, "m") + table = append(table, "n") + table = append(table, "o") + table = append(table, "p") + table = append(table, "q") + table = append(table, "r") + table = append(table, "s") + table = append(table, "t") + table = append(table, "u") + table = append(table, "v") + table = append(table, "w") + table = append(table, "x") + table = append(table, "y") + table = append(table, "z") + table = append(table, "Z") + table = append(table, "2 arguments") + table = append(table, "4 arguments so invalid") + + for _, s := range table { + lib.ChallengeMain("printrot", strings.Fields(s)...) + } +} diff --git a/subjects/printrot/README.md b/subjects/printrot/README.md new file mode 100644 index 00000000..06ed77f7 --- /dev/null +++ b/subjects/printrot/README.md @@ -0,0 +1,27 @@ +## printrot + +### Instructions + +Write a program that takes a lower case letter and goes around in order to display the whole alphabet starting from +this letter on a single line. + +A line is a sequence of characters preceding the [end of line](https://en.wikipedia.org/wiki/Newline) character (`'\n'`). + +If the input is invalid the program prints a newline (`'\n'`). + +### Usage + +```console +student@ubuntu:~/[[ROOT]]/test$ go build +student@ubuntu:~/[[ROOT]]/test$ ./test "abc" + +student@ubuntu:~/[[ROOT]]/test$ ./test "a" +abcdefghijklmnopqrstuvwxyz +student@ubuntu:~/[[ROOT]]/test$ ./test "g" +ghijklmnopqrstuvwxyzabcdef +student@ubuntu:~/[[ROOT]]/test$ ./test "a" "a" + +student@ubuntu:~/[[ROOT]]/test$ ./test "A" + +student@ubuntu:~/[[ROOT]]/test$ +```