Skip to content

Instantly share code, notes, and snippets.

@paulodiovani
Last active June 8, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulodiovani/ca8cc2e56ff96d44dbfa6b99a7c5ffb7 to your computer and use it in GitHub Desktop.
Save paulodiovani/ca8cc2e56ff96d44dbfa6b99a7c5ffb7 to your computer and use it in GitHub Desktop.
Arrange Matrix by Diagonals -- OMG

You will be given a 2d char array data.

The task is to rearrange the elements in the SAME order, diagonally, like this:

c h a         c a c 
r a c   ==>   h a e
t e r         r t r

here's another example

j a v a         j v o t 
c o d e    ==>  a c e c
t a c o         a d a o 

another one!

T h i s A r r a y       T i r y s r Y s y
i S s O c r a z Y  ==>  h A a S c z t l a
I t s R l y B a d       s r i O a I R B d

In other words, consider the order in which you read books (from left to right then down a line).

Take the characters in that order, and place them back in so that you can read them diagonally.

The diagonals run from bottom left to top right.

The first diagonal starts at element[0][0].

Note: the ascii value of chars is does not affect the ordering.

All array dimensions will be greater than 0.

source: https://www.codewars.com/kata/arrange-matrix-by-diagonals-omg

source 'https://rubygems.org'
gem 'rspec'
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.3)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
PLATFORMS
ruby
DEPENDENCIES
rspec
BUNDLED WITH
1.16.1
module Main
def diagonize arr
height = arr.size
length = arr[0].size
flat = arr.flatten
pyramid = pyramidize(flat, height, length)
cubize(pyramid, height, length)
end
def pyramidize arr, height, length
pyramid = []
pyramid_index = 0
pyramid_length = 1
while pyramid_length < [height, length].min do
pyramid.push arr[pyramid_index, pyramid_length]
pyramid_index += pyramid_length
pyramid_length += 1
end
diff = (height - length).abs
index = 0
while index < diff do
pyramid.push arr[pyramid_index, pyramid_length]
pyramid_index += pyramid_length
index += 1
end
while pyramid_length > 0 do
pyramid.push arr[pyramid_index, pyramid_length]
pyramid_index += pyramid_length
pyramid_length -= 1
end
pyramid
end
def cubize arr, height, length
cube = []
while cube.length < height
i = 0
line = []
while line.size < length
line.push arr[i].pop unless arr[i].empty?
if i === arr.size - 1
i = 0
else
i += 1
end
end
cube.push line
end
cube
end
end
require 'rspec'
require_relative '../main'
describe Main do
include Main
describe '#diagonize' do
it 'diagonizes matrix with equal dimensions' do
arr = [
['c', 'h', 'a'],
['r', 'a', 'c'],
['t', 'e', 'r']
]
expect(diagonize(arr)).to eq([
['c', 'a', 'c'],
['h', 'a', 'e'],
['r', 't', 'r']
])
end
it 'diagonizes matrix with greater length' do
arr = [
['j', 'a', 'v', 'a'],
['c', 'o', 'd', 'e'],
['t', 'a', 'c', 'o']
]
expect(diagonize(arr)).to eq([
['j', 'v', 'o', 't'],
['a', 'c', 'e', 'c'],
['a', 'd', 'a', 'o']
])
end
it 'diagonizes matrix with even greater length' do
arr = [
['T', 'h', 'i', 's', 'A', 'r', 'r', 'a', 'y'],
['i', 'S', 's', 'O', 'c', 'r', 'a', 'z', 'Y'],
['I', 't', 's', 'R', 'l', 'y', 'B', 'a', 'd']
]
expect(diagonize(arr)).to eq([
['T', 'i', 'r', 'y', 's', 'r', 'Y', 's', 'y'],
['h', 'A', 'a', 'S', 'c', 'z', 't', 'l', 'a'],
['s', 'r', 'i', 'O', 'a', 'I', 'R', 'B', 'd']
])
end
it 'diagonizes matrix with greater height' do
arr = [
['c', 'h'],
['r', 'a'],
['t', 'e'],
['a', 'b']
]
expect(diagonize(arr)).to eq([
['c', 'r'],
['h', 't'],
['a', 'a'],
['e', 'b']
])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment