Skip to content

Instantly share code, notes, and snippets.

@sally
Last active January 31, 2017 17:03
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 sally/4ea89e073f725006007fbbc977d5c374 to your computer and use it in GitHub Desktop.
Save sally/4ea89e073f725006007fbbc977d5c374 to your computer and use it in GitHub Desktop.
Hustle Interview Question (Conway's GoL recolor)
## Problem statement:
# Building meaningful relationships is a delicate balance. When hustlers have too many relationships they can get burned out. When they have too little, they are ineffective at achieving meaningful outcomes. When they hustle the right amount, they are successful at achieving their goals and in recruiting others to do the same.
# Hustle wants help modeling these relationship interactions using a two-dimensional grid of cells, each of which is in one of two possible states, hustling or not-hustling.
# Hustlers interact with people around them, the cells that are horizontally, vertically, or diagonally adjacent to them in the the following ways:
# Any hustler with fewer than two hustling neighbors gives up and stops hustling. There were not enough relationships to achieve their goals.
# Any hustler with two or three hustling neighbors keeps on hustling.
# Any hustler with more than three hustling neighbors stops hustling.
# Any non-hustler with exactly three hustling neighbours becomes a hustler (recruitment).
# Write a function named “simulateHustling” to simulate these interactions.
## Notes:
# In the grid, hustlers are represented with the string "H" and empty cells are represented with "n" for nil
# The grid is assumed to be square
def simulate_hustling(grid)
dimension = grid.length
toggled_hustlers_coordinates = []
get_hustling_neighbors = lambda do |hustler, row_index, column_index|
hustling_neighbors = []
(!row_index.zero? && !column_index.zero?) ? up_left = grid[row_index - 1][column_index - 1] : up_left = nil
!row_index.zero? ? up = grid[row_index - 1][column_index] : up = nil
!row_index.zero? && column_index != dimension-1 ? up_right = grid[row_index - 1][column_index + 1] : up_right = nil
!column_index.zero? ? left = grid[row_index][column_index - 1] : left = nil
column_index != dimension-1 ? right = grid[row_index][column_index + 1] : right = nil
(row_index != dimension-1 && !column_index.zero?) ? down_left = grid[row_index + 1][column_index - 1] : down_left = nil
row_index != dimension-1 ? down = grid[row_index + 1][column_index] : down = nil
(row_index != dimension-1 && column_index != dimension-1) ? down_right = grid[row_index + 1][column_index + 1] : down_right = nil
hustling_neighbors.push( up_left ) if up_left
hustling_neighbors.push( up ) if up
hustling_neighbors.push( up_right ) if up_right
hustling_neighbors.push( left ) if left
hustling_neighbors.push( right ) if right
hustling_neighbors.push( down_left ) if down_left
hustling_neighbors.push( down ) if down
hustling_neighbors.push( down_right ) if down_right
hustling_neighbors
end
grid.each_with_index do |row, row_index|
row.each_with_index do |cell, column_index|
next if cell.nil?
hustling_neighbors = get_hustling_neighbors.call(cell, row_index, column_index)
if hustling_neighbors.size < 2 || hustling_neighbors.size > 3
toggled_hustlers_coordinates << [row_index, column_index]
end
end
end
toggled_hustlers_coordinates.each do |coords|
grid[coords.first][coords.last] = nil
end
grid
end
## Driver Code
# The instance method pretty_print for Arrays below allows for readable printing of nested array
# i.e. [[1,2],[3,4]].pretty_print
# => "1 2"
# => "3 4"
class Array
def pretty_print
self.each do |r|
row = r.map do |elt|
if elt
elt
else
"n"
end
end
p row.join(" ")
end
end
end
# Example 1
grid1 =
[
[nil, nil, "H", nil],
[nil, "H", nil, "H"],
[nil, nil, nil, nil],
[nil, "H", nil, nil],
]
puts "Grid 1 prior to simulating Hustling:"
grid1.pretty_print
puts "\nGrid 1 post simulating Hustling:"
simulate_hustling(grid1).pretty_print
# Example 2
grid2 =
[
[nil, "H", nil],
["H", "H", nil],
["H", "H", nil],
]
puts "Grid 2 prior to simulating Hustling:"
grid2.pretty_print
puts "\nGrid 2 post simulating Hustling:"
simulate_hustling(grid2).pretty_print
# Example 3
# Grid 3 is a random NxN matrix where N is between 1 and 5. Its entries are arrays of length N whose entries are randomly picked to be nil or "H" for hustling
dimension = rand(1..10)
grid3 = Array.new(dimension) { Array.new(dimension){|row| row = [nil, "H"].sample} }
puts "\nGrid 3 prior to simulating Hustling:"
grid3.pretty_print
puts "\nGrid 3 post to simulating Hustling:"
simulate_hustling(grid3).pretty_print
[Command: ruby /Users/spark/desktop/hustling/simulate_hustling.rb]
Grid 1 prior to simulating Hustling:
"n n H n"
"n H n H"
"n n n n"
"n H n n"
Grid 1 post simulating Hustling:
"n n H n"
"n n n n"
"n n n n"
"n n n n"
Grid 2 prior to simulating Hustling:
"n H n"
"H H n"
"H H n"
Grid 2 post simulating Hustling:
"n H n"
"n n n"
"H H n"
Grid 3 prior to simulating Hustling:
"H n n n n n n"
"H n H n n H n"
"n n H H H H H"
"n H H H n H n"
"n n H n n n H"
"H n H n H n H"
"n H H n H H n"
Grid 3 post to simulating Hustling:
"n n n n n n n"
"n n H n n H n"
"n n n n n n H"
"n H n n n n n"
"n n n n n n H"
"n n H n H n H"
"n H H n H H n"
[Finished in 0.134s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment