From ddb69a7e26456c51aa56f5eb2fa9bebd8b14b62c Mon Sep 17 00:00:00 2001 From: Andre Rademacher Date: Tue, 31 Oct 2023 20:48:06 +0000 Subject: [PATCH] solved --- .devcontainer/devcontainer.json | 23 ++++++++++++++ .gitignore | 2 +- Dockerfile | 30 ------------------- ci/build_docker_image.sh | 11 ------- container.sh | 13 -------- kata/CountTheSmileyFaces/README.md | 3 +- kata/GetNthEvenNumber/README.md | 2 +- kata/MorseCode1/Morse.py | 10 +++++++ kata/MorseCode1/README.md | 45 ++++++++++++++++++++++++++++ kata/MorseCode1/__init__.py | 0 kata/MorseCode1/preloaded.py | 48 ++++++++++++++++++++++++++++++ kata/MorseCode1/test_Morse.py | 34 +++++++++++++++++++++ requirements.txt | 1 + 13 files changed, 164 insertions(+), 58 deletions(-) delete mode 100644 Dockerfile delete mode 100755 ci/build_docker_image.sh delete mode 100755 container.sh create mode 100644 kata/MorseCode1/Morse.py create mode 100644 kata/MorseCode1/README.md create mode 100644 kata/MorseCode1/__init__.py create mode 100644 kata/MorseCode1/preloaded.py create mode 100644 kata/MorseCode1/test_Morse.py create mode 100644 requirements.txt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e69de29..6916304 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,23 @@ +// for spec, @see https://containers.dev/implementors/spec/ +{ + "name": "Codewars Python", + + // for custom Dockerfile, @see https://containers.dev/guide/dockerfile + "image": "mcr.microsoft.com/devcontainers/python:3.11-bookworm", + + // features that are added to the container, @see https://containers.dev/features + "features": { + // currently none + }, + + // configure tool specific properties + "customizations": { + + "vscode": { + // currently none + } + }, + + // install dependencies + "postCreateCommand": "pip install -r requirements.txt" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index a3acba7..8c5f0e2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ .idea # caching -.cache/pip/ \ No newline at end of file +*.pyc \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 14af36c..0000000 --- a/Dockerfile +++ /dev/null @@ -1,30 +0,0 @@ -FROM python:3.11-bookworm -LABEL maintainer="andre.rademacher.business@googlemail.com" - -ARG GID=1000 -ARG UID=1000 -ARG UNAME=codewars - -ARG PIPCACHE="/.cache/pip" - -RUN groupadd \ - --gid ${GID} \ - --non-unique \ - ${UNAME} - -RUN useradd \ - --create-home \ - --gid ${GID} \ - --home-dir /home/codewars \ - --shell /bin/bash \ - --uid ${UID} \ - ${UNAME} - -RUN pip install --upgrade pip \ - && pip install git+https://github.com/codewars/python-test-framework.git#egg=codewars_test - -USER ${UNAME} -WORKDIR /codewars/python311 - -# setup -VOLUME "${PIPCACHE}" \ No newline at end of file diff --git a/ci/build_docker_image.sh b/ci/build_docker_image.sh deleted file mode 100755 index 44f949a..0000000 --- a/ci/build_docker_image.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -SCRIPT_DIRECTORY="$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")" -cd "${SCRIPT_DIRECTORY}" || exit - -cd .. \ -&& docker build \ - --no-cache \ - --pull \ - --tag "andrerademacher/codewars-python311" \ - . \ No newline at end of file diff --git a/container.sh b/container.sh deleted file mode 100755 index e7e2a0a..0000000 --- a/container.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -SCRIPT_DIRECTORY="$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")" -cd "${SCRIPT_DIRECTORY}" || exit - -docker run \ - --interactive \ - --name 'codewars-python311' \ - --rm \ - --tty \ - --volume "pipcache":/.cache \ - --volume "${PWD}":/codewars/python311 \ - andrerademacher/codewars-python311 "$@" diff --git a/kata/CountTheSmileyFaces/README.md b/kata/CountTheSmileyFaces/README.md index 5e7d65b..e24daf5 100644 --- a/kata/CountTheSmileyFaces/README.md +++ b/kata/CountTheSmileyFaces/README.md @@ -24,5 +24,4 @@ countSmileys([';]', ':[', ';*', ':$', ';-D']); // should return 1; In case of an empty array return 0. You will not be tested with invalid input (input will always be an array). Order of the face (eyes, nose, mouth) elements will always be the same. - -[See this kata in Codewars](https://www.codewars.com/kata/583203e6eb35d7980400002a) +[See this kata in Codewars](https://www.codewars.com/kata/583203e6eb35d7980400002a) \ No newline at end of file diff --git a/kata/GetNthEvenNumber/README.md b/kata/GetNthEvenNumber/README.md index 105c398..975b61d 100644 --- a/kata/GetNthEvenNumber/README.md +++ b/kata/GetNthEvenNumber/README.md @@ -10,4 +10,4 @@ Example(Input --> Output) ``` The input will not be 0. -[See this kata in Codewars](https://www.codewars.com/kata/5933a1f8552bc2750a0000ed) +[See this kata in Codewars](https://www.codewars.com/kata/5933a1f8552bc2750a0000ed) \ No newline at end of file diff --git a/kata/MorseCode1/Morse.py b/kata/MorseCode1/Morse.py new file mode 100644 index 0000000..b9db6af --- /dev/null +++ b/kata/MorseCode1/Morse.py @@ -0,0 +1,10 @@ +from preloaded import MORSE_CODE + +def decode_morse(morse_code: str) -> str: + decoded_words = []; + for word in morse_code.strip().split(' '): + decoded_word = '' + for unit in word.split(' '): + decoded_word += MORSE_CODE[unit] + decoded_words.append(decoded_word) + return ' '.join(decoded_words) \ No newline at end of file diff --git a/kata/MorseCode1/README.md b/kata/MorseCode1/README.md new file mode 100644 index 0000000..68ec341 --- /dev/null +++ b/kata/MorseCode1/README.md @@ -0,0 +1,45 @@ +# Part 1 of the Morse code series + +In this kata you have to write a simple [Morse code](https://en.wikipedia.org/wiki/Morse_code) decoder. While the Morse code is now mostly superseded by voice and digital data communication channels, it still has its use in some applications around the world. + +The Morse code encodes every character as a sequence of "dots" and "dashes". For example, the letter `A` is coded as `·−`, letter `Q` is coded as `−−·−`, and digit `1` is coded as `·−−−−`. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. For example, the message `HEY JUDE` in Morse code is `···· · −·−− ·−−− ··− −·· ·`. + +**NOTE**: Extra spaces before or after the code have no meaning and should be ignored. + +In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal `SOS` (that was first issued by Titanic), that is coded as `···−−−···`. These special codes are treated as single special characters, and usually are transmitted as separate words. + +Your task is to implement a function that would take the morse code as input and return a decoded human-readable string. + +For example: + +``` +decode_morse('.... . -.-- .--- ..- -.. .') +#should return "HEY JUDE" +``` + +NOTE: For coding purposes you have to use ASCII characters `.` and `-`, not Unicode characters. + +The Morse code table is preloaded for you as a dictionary, feel free to use it: + +- Coffeescript/C++/Go/JavaScript/Julia/PHP/Python/Ruby/TypeScript: MORSE_CODE['.--'] +- C#: MorseCode.Get(".--") (returns string) +- F#: MorseCode.get ".--" (returns string) +- Elixir: @morse_codes variable (from use MorseCode.Constants). Ignore the unused variable warning for morse_codes because it's no longer used and kept only for old solutions. +- Elm: MorseCodes.get : Dict String String +- Haskell: morseCodes ! ".--" (Codes are in a Map String String) +- Java: MorseCode.get(".--") +- Kotlin: MorseCode[".--"] ?: "" or MorseCode.getOrDefault(".--", "") +- Racket: morse-code (a hash table) +- Rust: MORSE_CODE +- Scala: morseCodes(".--") +- Swift: MorseCode[".--"] ?? "" or MorseCode[".--", default: ""] +- C: provides parallel arrays, i.e. morse[2] == "-.-" for ascii[2] == "C" +- NASM: a table of pointers to the morsecodes, and a corresponding list of ascii symbols + +All the test strings would contain valid Morse code, so you may skip checking for errors and exceptions. In C#, tests will fail if the solution code throws an exception, please keep that in mind. This is mostly because otherwise the engine would simply ignore the tests, resulting in a "valid" solution. + +Good luck! + +After you complete this kata, you may try yourself at [Decode the Morse code, advanced](https://www.codewars.com/kata/decode-the-morse-code-advanced). + +[See this kata in Codewars](https://www.codewars.com/kata/54b724efac3d5402db00065e) \ No newline at end of file diff --git a/kata/MorseCode1/__init__.py b/kata/MorseCode1/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kata/MorseCode1/preloaded.py b/kata/MorseCode1/preloaded.py new file mode 100644 index 0000000..b32fc6c --- /dev/null +++ b/kata/MorseCode1/preloaded.py @@ -0,0 +1,48 @@ +MORSE_CODE = { + + # single unit characters + '.' : 'E', + '-' : 'T', + + # two unit characters + '..' : 'I', + '.-' : 'A', + '-.' : 'N', + '--' : 'M', + + # three unit characters + '...' : 'S', + '..-' : 'U', + '.-.' : 'R', + '.--' : 'W', + '-..' : 'D', + '-.-' : 'K', + '--.' : 'G', + '---' : 'O', + + # four unit characters + '....' : 'H', + '...-' : 'V', + '..-.' : 'F', + '.-..' : 'L', + '.--.' : 'P', + '.---' : 'J', + '-...' : 'B', + '-..-' : 'X', + '-.-.' : 'C', + '-.--' : 'Y', + '--..' : 'Z', + '--.-' : 'Q', + + # five unit characters + '.----' : '1', + '..---' : '2', + '...--' : '3', + '....-' : '4', + '.....' : '5', + '-....' : '6', + '--...' : '7', + '---..' : '8', + '----.' : '9', + '-----' : '0', +} \ No newline at end of file diff --git a/kata/MorseCode1/test_Morse.py b/kata/MorseCode1/test_Morse.py new file mode 100644 index 0000000..5b17700 --- /dev/null +++ b/kata/MorseCode1/test_Morse.py @@ -0,0 +1,34 @@ +from Morse import decode_morse +import codewars_test as test + +@test.describe("Morse Code 1") +def tests(): + + @test.it("Should obtain correct decoding of Morse code from the description") + def test_morse_hey_jude(): + test.assert_equals(decode_morse('.... . -.-- .--- ..- -.. .'), 'HEY JUDE') + + @test.it("Should obtain correct decoding of Morse code for basic examples") + def test_morse_basic_examples(): + test.assert_equals(decode_morse('.-'), 'A') + test.assert_equals(decode_morse('--...'), '7') + test.assert_equals(decode_morse('...-..-'), '$') + test.assert_equals(decode_morse('.'), 'E') + test.assert_equals(decode_morse('..'), 'I') + test.assert_equals(decode_morse('. .'), 'EE') + test.assert_equals(decode_morse('. .'), 'E E') + test.assert_equals(decode_morse('...-..- ...-..- ...-..-'), '$$$') + test.assert_equals(decode_morse('----- .---- ..--- ---.. ----.'), '01289') + test.assert_equals(decode_morse('.-... ---... -..-. --...'), '&: /7') + test.assert_equals(decode_morse('...---...'), 'SOS') + test.assert_equals(decode_morse('... --- ...'), 'SOS') + test.assert_equals(decode_morse('... --- ...'), 'S O S') + + @test.it("Should obtain correct decoding of Morse code for examples with extra spaces") + def test_morse_basic_examples_with_extra_spaces(): + test.assert_equals(decode_morse(' . '), 'E') + test.assert_equals(decode_morse(' . . '), 'E E') + + @test.it("Should obtain correct decoding of Morse code for a complex example, and should ignore leading and trailing whitespace") + def test_morse_complex_example(): + test.assert_equals(decode_morse(' ...---... -.-.-- - .... . --.- ..- .. -.-. -.- -... .-. --- .-- -. ..-. --- -..- .--- ..- -- .--. ... --- ...- . .-. - .... . .-.. .- --.. -.-- -.. --- --. .-.-.- '), 'SOS! THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..089d372 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +codewars-test @ git+https://github.com/codewars/python-test-framework.git#egg=codewars_test \ No newline at end of file