Skip to content

Instantly share code, notes, and snippets.

@dsasse07
dsasse07 / module_inheritance.rb
Last active Jan 10, 2021
Modules that are inherited can be passed along to a new parent Module
View module_inheritance.rb
module Module_A
def greeting_a
puts "Hello from Module A"
end
end
module Module_B
include Module_A
@dsasse07
dsasse07 / subscription_tracker_environment.rb
Created Jan 10, 2021
Environment file for SubscriptionTracker loading the module files in heirarchy order before loading the model files
View subscription_tracker_environment.rb
require 'bundler'
Bundler.require
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db')
ActiveRecord::Base.logger = nil
require_relative '../app/tools/funstuff.rb' #=> Begin loading files from the bottom of the heirarchy
require_relative '../app/tools/cli_controls.rb' #=> Step 2 on the heirarchy
@dsasse07
dsasse07 / center_align.rb
Last active Jan 10, 2021
Creating a monkey patch to center align a string
View center_align.rb
require 'tty-screen'
class String
def center_align
screen_width = TTY::Screen.width
screen_center = screen_width / 2
string_length = self.length
string_center = string_length/2
offset = screen_center - string_center
"#{sprintf("%#{offset}s" % self)}"
@dsasse07
dsasse07 / sudoku-box-condition.js
Last active Feb 16, 2021
Sudoku box region safe condition
View sudoku-box-condition.js
// puzzleArray is the game board being solved. A 9x9 matrix
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell
// num = integer value 1-9 being tested
const boxSafe = (puzzleArray, emptyCell, num) => {
// Define top left corner of box region for empty cell
boxStartRow = emptyCell.rowIndex - (emptyCell.rowIndex % 3)
boxStartCol = emptyCell.colIndex - (emptyCell.colIndex % 3)
let safe = true
@dsasse07
dsasse07 / sudoku-initialize-functions.js
Last active Feb 17, 2021
Sudoku initialize functions
View sudoku-initialize-functions.js
const newSolvedBoard = _ => {
startTime = new Date
// Create an unaffiliated clone of a fresh board
const newBoard = BLANK_BOARD.map(row => row.slice() )
// Populate the board using backtracking algorithm
fillPuzzle(newBoard)
return newBoard
}
@dsasse07
dsasse07 / sudoku-row-condition.js
Last active Feb 17, 2021
Sudoku safe placement row condition in JS
View sudoku-row-condition.js
// puzzleArray is the game board being solved. A 9x9 matrix
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell
// num = integer value 1-9 being tested
const rowSafe = (puzzleArray, emptyCell, num) => {
// -1 is return value of .find() if value not found
return puzzleArray[ emptyCell.rowIndex ].indexOf(num) == -1
}
@dsasse07
dsasse07 / sudoku-safe-helper.js
Last active Feb 17, 2021
Helper function for checking number placement
View sudoku-safe-helper.js
const safeToPlace = ( puzzleArray, emptyCell, num ) => {
return rowSafe(puzzleArray, emptyCell, num) &&
colSafe(puzzleArray, emptyCell, num) &&
boxSafe(puzzleArray, emptyCell, num)
}
@dsasse07
dsasse07 / sudoku-fill-puzzle.js
Last active Feb 17, 2021
Sudoku recursive solver function & shuffler
View sudoku-fill-puzzle.js
// startingBoard is a 9x9 matrix of zeros
const numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const shuffle = array => {
let newArray = [...array]
for ( let i = newArray.length - 1; i > 0; i-- ) {
const j = Math.floor( Math.random() * ( i + 1 ) );
[ newArray[ i ], newArray[ j ] ] = [ newArray[ j ], newArray[ i ] ];
}
@dsasse07
dsasse07 / sudoku-next-empty.js
Last active Feb 17, 2021
Find next empty cell Sudoku
View sudoku-next-empty.js
const nextEmptyCell = puzzleArray => {
const emptyCell = {rowIndex: "", colIndex: ""}
puzzleArray.forEach( (row, rowIndex) => {
// If this key has already been assigned, skip iteration
if (emptyCell.colIndex !== "" ) return
// find first zero-element
let firstZero = row.find( col => col === 0)
@dsasse07
dsasse07 / sudoku-col-condition.js
Last active Feb 18, 2021
Sudoku safe placement in column check
View sudoku-col-condition.js
// puzzleArray is the game board being solved. A 9x9 matrix
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell
// num = integer value 1-9 being tested
const colSafe = (puzzleArray, emptyCell, num) => {
return !puzzleArray.some(row => row[ emptyCell.colIndex ] == num )
}