Connection Status:
Competition Arena > Serpentine
SRM 139 · 2003-03-18 · by dgoodman
Class Name: Serpentine
Return Type: String
Method Name: column
Arg Types: (string, int, int)
Problem Statement

Problem Statement

Printing a sequence of characters on a piece of paper could be faster if we were willing to write every other line from right to left. That way, we could avoid the "horizontal retrace" at the end of each line, simply moving down one spot and continuing to print horizontally in the opposite direction.

Create a class Serpentine that contains a method column that is given a String s and the width of the paper. It is also given the 0-based index of a column. It returns the contents of the indicated column that results from printing s using the above plan, printing the first row from left to right and alternating the direction of successive rows.

It should return the column as a String in top to bottom order.

Constraints

  • s has length between 1 and 50 inclusive
  • s contains only uppercase letters 'A'-'Z'
  • width is between 1 and 50 inclusive
  • index is between 0 and width-1 inclusive
Examples
0)
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8
0
Returns: "APQ"

ABCDEFGH PONMLKJI QRSTUVWX ZY index 0 refers to the first column. Note that the returned String must not contain a trailing space.

1)
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8
6
Returns: "GJWZ"
2)
"ABCDEFG"
10
8
Returns: ""
3)
"THISISATEST"
4
2
Returns: "IST"
4)
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX"
1
0
Returns: "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX"

Submissions are judged against all 28 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class Serpentine with a public method string column(string s, int width, int index) · 28 test cases · 2 s / 256 MB per case

Submitting as anonymous