Skip to content

Instantly share code, notes, and snippets.

View cefaijustin's full-sized avatar

Justin Cefai cefaijustin

View GitHub Profile
@cefaijustin
cefaijustin / linked-02.rb
Created July 21, 2018 23:07
Ruby Linked List Two
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
end
@cefaijustin
cefaijustin / btree.rb
Created June 26, 2018 02:16
Binary Tree Traversal
class BinaryTree
attr_accessor :payload, :left, :right
def initialize(payload, left, right)
@payload = payload
@left = left
@right = right
end
def self.build_tree(array)
@cefaijustin
cefaijustin / tree.rb
Created June 14, 2018 02:09
Final Tree Traversal
class Tree
attr_accessor :payload, :children
def initialize(payload, children)
@payload = payload
@children = children
end
# depth_first returns nil if 11 isn't in the tree
def search_node(number)
@cefaijustin
cefaijustin / spinWords.js
Created June 2, 2018 03:59
Stop Spinning My Words
// Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
function spinWords(string){
return string.split(' ').map(function(word){
if (word.length >= 5) {
return word.split('').reverse().join('');
} else {
return word;
}
}).join(' ');
@cefaijustin
cefaijustin / toWeirdCase.js
Created June 2, 2018 03:50
To Weird Case
// Write a function toWeirdCase (weirdcase in Ruby) that accepts a string, and returns the same string with all even indexed characters in each word upper cased, and all odd indexed characters in each word lower cased. The indexing just explained is zero based, so the zero-ith index is even, therefore that character should be upper cased.
// The passed in string will only consist of alphabetical characters and spaces(' '). Spaces will only be present if there are multiple words. Words will be separated by a single space(' ').
function toWeirdCase(string){
return string.split(' ').map(function(word){
return word.split('').map(function(letter, index){
if (index % 2 === 0) {
return letter.toUpperCase();
} else if (index % 2 != 0) {
// write a function that creates a black and white chess board.
function mineColor(line, number) {
if ((line === 'a' || line === 'c' || line === 'e' || line === 'g') && number % 2 == 0) {
return 'white';
} else if ((line === 'b' || line === 'd' || line === 'f' || line === 'h') && number % 2 != 0) {
return 'white';
} else {
return 'black';
}
"use strict";
var _ = {
join: (array, separator = ",") => {
var str = "";
for (var i = 0; i < array.length; i++) {
str += separator + array[i];
}
return str.substring(2);
}
@cefaijustin
cefaijustin / collatz.rb
Created May 12, 2018 20:25
Longest Collatz Sequence with comments.
# n → n/2 (n is even)
# n → 3n + 1 (n is odd)
# Write a method to determine a number's collatz sequence.
# Find the number (1 to 1,000,000) with the longest sequence.
# collatz method defined below with (n) as an argument.
def collatz(n)
module Luhn
def self.is_valid?(number)
# ----------------------------------------------------
#
# line 17 - take the number, convert it to a string
# and reverse it. '.chars' splits it by digit, then
class Bike
attr_accessor :brand, :color
def initialize(brand, color)
@brand = brand
@color = color
end
end