Skip to content

Instantly share code, notes, and snippets.

@dapi
Last active December 10, 2019 07:07
Show Gist options
  • Save dapi/6173322 to your computer and use it in GitHub Desktop.
Save dapi/6173322 to your computer and use it in GitHub Desktop.
Test task for Ruby

The issue

Write a program in Ruby that takes one command line argument (referred to going forward as n). If n is not a perfect square print an appropriate error message and exit. If n is a perfect square then create a sequence from 1 to n and build a matrix from the sequence by walking in counter-clockwise spiral order. Examples below illustrate this for n=1,4,9,16 but your solution should work for any n that is a perfect square. Once you have built the matrix print it, ensuring even column widths as in examples below and then exit.

  Input:  1
  Output: 1

  Input:  4
  Output: 4 3
          1 2

  Input:  9
  Output: 5 4 3
          6 1 2
          7 8 9

  Input:  16
  Output: 16 15 14 13
          5  4  3  12
          6  1  2  11
          7  8  9  10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment