Skip to content

Instantly share code, notes, and snippets.

@maxpou
Last active April 6, 2018 08:33
Show Gist options
  • Save maxpou/1dc0b8742b145a46aef85f2433207401 to your computer and use it in GitHub Desktop.
Save maxpou/1dc0b8742b145a46aef85f2433207401 to your computer and use it in GitHub Desktop.
algorithm exercises
function fabioCross (n) {
const square = Array(n)
for (let rowIndex = 0; rowIndex < square.length; rowIndex++) {
square[rowIndex] = Array(n).fill(' ')
square[rowIndex][0] = '#'
square[rowIndex][square.length - 1] = '#'
if (rowIndex === 0 || rowIndex === square.length - 1) {
square[rowIndex] = Array(n).fill('#')
} else {
square[rowIndex][rowIndex] = '#'
square[rowIndex][n - rowIndex - 1] = '#'
}
square[rowIndex] = square[rowIndex].join('')
}
return square.join('\n')
}
console.log(fabioCross(10))
/*
$ node src/others/fabio-cross.js
##########
## ##
# # # #
# # # #
# ## #
# ## #
# # # #
# # # #
## ##
##########
*/
// --- Directions
// Write a function that accepts an integer N
// and returns a NxN spiral matrix.
// --- Examples
// matrix(2)
// [[1, 2],
// [4, 3]]
// matrix(3)
// [[1, 2, 3],
// [8, 9, 4],
// [7, 6, 5]]
// matrix(4)
// [[1, 2, 3, 4],
// [12, 13, 14, 5],
// [11, 16, 15, 6],
// [10, 9, 8, 7]]
// matrix(5)
// [[1, 2, 3, 4, 5],
// [16, 17, 18, 19, 6],
// [15, 24, 25, 20, 7],
// [14, 23, 22, 21, 8],
// [13, 12, 11, 10, 9]]
// matrix(3)
// [[1, 2, 3],
// [8, 9, 4],
// [7, 6, 5]]
function matrix (n) {
const structure = [...Array(n).fill(null)].map(e => [...Array(n).fill(null)])
let curentNumber = 1
const lastNumber = n * n
const directionsOrder = ['right', 'down', 'left', 'up']
const changeDirection = (currentDirection) => directionsOrder.indexOf(currentDirection) < 3
? directionsOrder[directionsOrder.indexOf(currentDirection)+1]
: directionsOrder[0]
let cursor = {
x: 0,
y: 0,
direction: directionsOrder[0] // start with right
}
const getNextCursor = (currCursor, structure) => {
let cursorCopy = { ...currCursor }
const isFieldValid = value => value !== undefined || value !== null
switch (cursorCopy.direction) {
case 'right': cursorCopy.x += 1; break
case 'down': cursorCopy.y += 1; break
case 'left': cursorCopy.x -= 1; break
case 'up': cursorCopy.y -= 1; break
}
if (cursorCopy.x >= n || cursorCopy.y >= n || structure[cursorCopy.y][cursorCopy.x] !== null) {
return getNextCursor({...currCursor, direction: changeDirection(currCursor.direction)}, structure)
}
return cursorCopy
}
for (let curentNumber = 1; curentNumber <= lastNumber; curentNumber++) {
structure[cursor.y][cursor.x] = curentNumber
if (curentNumber < lastNumber) {
cursor = getNextCursor(cursor, structure)
}
}
return structure
}
console.log(matrix(7))
/*
» node index.js
[ [ 1, 2, 3, 4, 5, 6, 7 ],
[ 24, 25, 26, 27, 28, 29, 8 ],
[ 23, 40, 41, 42, 43, 30, 9 ],
[ 22, 39, 48, 49, 44, 31, 10 ],
[ 21, 38, 47, 46, 45, 32, 11 ],
[ 20, 37, 36, 35, 34, 33, 12 ],
[ 19, 18, 17, 16, 15, 14, 13 ] ]
*/
// pyramid(1)
// '#'
// pyramid(2)
// ' # '
// '###'
// pyramid(3)
// ' # '
// ' ### '
// '#####'
// pyramid(n=4)
// ' # ' 1 -- floor 1 | spaces = n-floor (4-1)
// ' ### ' 3 -- floor 1 + 2 | spaces = n-floor (4-2)
// ' ##### ' 5 -- floor 2 + 2 | spaces = n-floor (4-3)
// '#######' 7 -- floor 3 + 2 | spaces = n-floor (4-4)
function pyramid (n) {
const pyramidContent = Array(n)
const getRepetedChar = (char, nb) => Array(nb + 1).join(char)
const getNbBricks = (floorNumber) => floorNumber === 1 ? 1 : getNbBricks(floorNumber - 1) + 2
for (let floor = 1; floor <= n; floor++) {
const nbSpaces = n-floor;
const nbBrick = (floor)
const content = `${getRepetedChar(' ', nbSpaces)}${getRepetedChar('#', getNbBricks(floor))}${getRepetedChar(' ', nbSpaces)}`
pyramidContent[floor-1] = content
}
return pyramidContent
}
pyramid(4).forEach(i => {console.log(i)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment