TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

MorseEnc

Problem statement, definition, constraints, and public examples.

Problem Statement

PROBLEM STATEMENT

In the early days of communications, transmissions could only send Boolean data
(on or off), and were sent manually.  Transmissions also had to be interpreted
by humans - not computers.  Morse code (actually developed by Alfred Vail, not
Samuel Morse as many people believe) was used to communicate.  This code was a
collection of short pulses (dots) and longer pulses (dashes) and varying delays
(short breaks for spaces between letters, longer breaks for word breaks).

Create a class MorseEnc that contains the method encode, which takes a string
argument that contains a message in alphanumerics ('A'-'Z', 'a'-'z' and '0'-'9'
- all ranges inclusive) and spaces and returns a string representing the
international Morse code representation of this message.  The representation
should use the period ('.') character to represent a dot, the hyphen ('-')
character to represent a dash, the space (' ') character to represent a letter
break (used to separate encoded letters and numbers), and the slash ('/')
character, surrounded by single spaces, to represent a word break (used to
separate encoded words).


DEFINITION
Class: MorseEnc
Method: encode
Parameters: String
Returns: String
Method signature (be sure your method is public):  String encode(String message);

International Morse code encodings for numbers and letters are as follows:

A: .-      N: -.      0: -----
B: -...    O: ---     1: .----
C: -.-.    P: .--.    2: ..---
D: -..     Q: --.-    3: ...--
E: .       R: .-.     4: ....-
F: ..-.    S: ...     5: .....
G: --.     T: -       6: -....
H: ....    U: ..-     7: --...
I: ..      V: ...-    8: ---..
J: .---    W: .--     9: ----.
K: -.-     X: -..-
L: .-..    Y: -.--
M: --      Z: --..

To save you some typing, here they are in a more usable form - you can copy and
paste these into your source code if you wish:
- Encodings for 'A' - 'Z' (inclusive and in alphabetical order) = { ".-",
"-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--",
"-..-", "-.--", "--.." }
- Encodings for '0' - '9' (inclusive and in numerical order) = { "-----",
".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." }


NOTES
- Morse code is NOT case sensitive.  The encoding for a capital and lower-case
version of a letter are the same.  For example, the encoding for 'a' is ".-"
which is also the encoding for 'A'.
- Your result must neither start nor end with a space.
- Your result must never contain two spaces in a row.
- You can NOT have spaces within a letter's encoding.
- You must have exactly one space between each letter's encoding.
- For those who know Morse code: punctuation and full stops are NOT to be
included.


TopCoder will ensure the validity of the inputs.  Inputs are valid if all of
the following criteria are met:
- message will be between 1 and 50 characters in length.
- message will neither start nor end with a space.
- message will contain only letters ('A'-'Z' and 'a'-'z' inclusive), numbers
('0'-'9', inclusive) and spaces.
- message will not contain more than one space in a row.


EXAMPLES

In each of these examples, quotes ('"') and parenthesis ('(' and ')') are
included for clarity only.  They are never part of the input or output.

Example #1)
message = "SOS"
return "... --- ..."

Example #2)
message = "After you"
return ".- ..-. - . .-. / -.-- --- ..-"

Example #3)
message = "Four score and 7 years ago"
return "..-. --- ..- .-. / ... -.-. --- .-. . / .- -. -.. / --... / -.-- . .-
.-. ... / .- --. ---"

Example #4)
message = "abcdefghijklmnopqrstuvwxyz"
return ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.-
.-. ... - ..- ...- .-- -..- -.-- --.."

Definition

Class:
MorseEnc
Method:
encode
Parameters:
String
Returns:
String
Method signature:
String encode(String param0)
(be sure your method is public)

Constraints

    Examples

      No public sample cases were explicitly marked for this problem in the original data.

      Back to all problems