TopcoderArchive
Archive
Algorithm problem

Seq

Problem statement, definition, constraints, and public examples.

Problem Statement

The following is the manual page of the GNU utility "seq":

NAME
       seq - print a sequence of numbers

SYNOPSIS
       seq LAST
       seq FIRST LAST
       seq FIRST INCREMENT LAST

DESCRIPTION
       Print numbers from FIRST to LAST, in steps of INCREMENT.

       If  FIRST or INCREMENT is omitted, it defaults to 1. That 
       is,  an omitted INCREMENT  defaults to  1  even when LAST 
       is smaller than FIRST.  INCREMENT is usually positive  if
       FIRST  is  smaller than  LAST,  and INCREMENT  is usually
       negative if FIRST is greater than LAST.

Write a function that will mimic the behavior of seq.

You are given the String commandLine containing one, two, or three small integers, separated by single spaces. Return a int[] with the output of the utility seq when given those arguments.

Definition

Class:
Seq
Method:
generate
Parameters:
String
Returns:
int[]
Method signature:
int[] generate(String arguments)
(be sure your method is public)

Constraints

  • commandLine will be of the form "LAST", "FIRST LAST", or "FIRST INCREMENT LAST".
  • LAST will be an integer between 1 and 100, inclusive.
  • If present, FIRST will be an integer between 1 and 100, inclusive.
  • If present, INCREMENT will be an integer between -100 and 100, inclusive.
  • INCREMENT will not be 0.
  • None of the numbers will have unnecessary leading zeros.

Examples

  1. "7"
    Returns: {1, 2, 3, 4, 5, 6, 7 }
  2. "4 7"
    Returns: {4, 5, 6, 7 }
  3. "11 -3 3"
    Returns: {11, 8, 5 }
    The next number after 11, 8, and 5 would be 2, but that is already smaller than LAST.
  4. "47 10 42"
    Returns: { }
    As LAST is smaller than FIRST, this is an empty range.
Back to all problems