Statistics

Problem Statement for "Scytale"

Problem Statement

Scytale codes were used by the Spartans approximately 2400 years ago. A strip of leather or parchment was wound around a wooden cylinder (the scytale), and the message was written in rows along the length of the cylinder. When unwound, the strip was unreadable because the characters of each row were scattered across the strip. The recipient of the message simply rewound the strip around a cylinder with the same circumference to realign the characters of each row.

For example, the message

    "attack when "
    "you hear the"
    "horns blow "
would be written on a cylinder with circumference 3 as
            ---------------------------------------
  start --> |\a\\t\\t\\a\\c\\k\\ \\w\\h\\e\\n\\ \ |
   of       | \y\\o\\u\\ \\h\\e\\a\\r\\ \\t\\h\\e\| <-- end of
  strip     |  \h\\o\\r\\n\\s\\ \\b\\l\\o\\w\\ \  |     strip
            ---------------------------------------
                   <--- axis of cylinder --->
(In this picture, the cylinder has been flattened out so you can see all sides at once. The '\' characters indicate the edges of the strip.) When unwound from the cylinder, the strip would read
    "ayhtootura nchske  abwrlh oetwnh  e"

Your task is to decode a message, given the coded message and the circumference of the cylinder. The circumference is measured in rows of text. The first character of the coded message is the first character of the first row, the second character of the coded message is the first character of the second row, and so on. The longest row is at most one longer than the shortest row. If the rows are not all the same length, then the longer rows appear before the shorter rows. Some rows may be empty (see Example 2).

Create a class Scytale containing a method decode that takes a String codedMessage and an int circumference, and returns the rows of the decoded text (in order) as a String[].

Definition

Class:
Scytale
Method:
decode
Parameters:
String, int
Returns:
String[]
Method signature:
String[] decode(String codedMessage, int circumference)
(be sure your method is public)

Constraints

  • codedMessage contains between 1 and 50 characters, inclusive.
  • Allowable characters are letters ('a'-'z' or 'A'-'Z') and spaces.
  • circumference is between 2 and 10, inclusive.

Examples

  1. "ayhtootura nchske abwrlh oetwnh e"

    3

    Returns: { "attack when ", "you hear the", "horns blow " }

    The example above.

  2. "IIyt eohmlnoalipkogeewh t"

    4

    Returns: { "I hope ", "I make ", "yellow ", "tonight" }

  3. "abc"

    5

    Returns: { "a", "b", "c", "", "" }

    Notice that some of the rows may be empty.

  4. "abcdefghijklmnopqrstuvwxyz"

    5

    Returns: { "afkpuz", "bglqv", "chmrw", "dinsx", "ejoty" }

  5. "abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY"

    3

    Returns: { "adgjmpsvyCFILORUX", "behknqtwADGJMPSVY", "cfiloruxBEHKNQTW" }

  6. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS"

    10

    Returns: { "akuEO", "blvFP", "cmwGQ", "dnxHR", "eoyIS", "fpzJ", "gqAK", "hrBL", "isCM", "jtDN" }

  7. "HToeaoohp lsleyaltdrlt s ma c"

    6

    Returns: { "Hold ", "Therm", "opyla", "e at ", "all c", "osts" }

  8. "Q"

    2

    Returns: { "Q", "" }

  9. " "

    7

    Returns: { " ", " ", " ", " ", " ", " ", " " }

  10. "Feeao ngua orny des acsroesrv "

    9

    Returns: { "Fasr", "e v", "eoa ", "arc", "ons", " yr", "n o", "gde", "ues" }

  11. "Once upon a time"

    2

    Returns: { "Oc pnatm", "neuo ie" }

  12. "When in the course of human events it becomes"

    8

    Returns: { "Wtsmtc", "hheaso", "ee n m", "n o ie", " cfets", "io v ", "nuheb", " rune" }

  13. "Itsy bitsy spider climbed up the water spout"

    10

    Returns: { "I ihp", "tsmeo", "spb u", "yiewt", " dda", "be t", "irue", "t pr", "sc ", "ylts" }

  14. "a"

    10

    Returns: { "a", "", "", "", "", "", "", "", "", "" }

  15. "abcdefgh"

    9

    Returns: { "a", "b", "c", "d", "e", "f", "g", "h", "" }

  16. "abcdef"

    6

    Returns: { "a", "b", "c", "d", "e", "f" }

  17. "fedcba"

    5

    Returns: { "fa", "e", "d", "c", "b" }

  18. "test number seventeen"

    7

    Returns: { "tmv", "ebe", "sen", "trt", " e", "nse", "uen" }

  19. "test number eighteen"

    8

    Returns: { "tbt", "eee", "sre", "t n", " e", "ni", "ug", "mh" }

  20. "this statement is false"

    4

    Returns: { "t tnsl", "hset s", "itm fe", "saeia" }

  21. "one two three four five"

    5

    Returns: { "owroi", "noeuv", "e ere", " t ", "thff" }

  22. "foobar"

    2

    Returns: { "foa", "obr" }

  23. "foobar"

    2

    Returns: { "foa", "obr" }


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: