Skip to content

Instantly share code, notes, and snippets.

class LinkedListNode
attr_accessor :value, :next
def initialize(value)
@value = value
@next = nil
end
def kth_to_last_node(k, head)
class Pokemon
# from here until line 9, we are INSIDE the class
attr_accessor :name, :type, :level
# when a Pokemon is created, its name, type, and level must be set
def initialize(name, type, level)
@name = name
@type = type
@level = level
end
class Pokemon
# from here until line 9, we are INSIDE the class
# when a Pokemon is created, its name, type, and level must be set
def initialize(name, type, level)
@name = name
@type = type
@level = level
end
end
class Pokemon
# from here until line 21, we are INSIDE the class
# when a Pokemon is created, its name, type, and level must be set
def initialize(name, type, level)
@name = name
@type = type
@level = level
end
pokemon.rb:14:in `<main>': undefined method `type' for
#<Pokemon:0x007ff70613d648> (NoMethodError)
# Write a method that takes a camelcase string as an argument and converts it into snake case
def to_underscore(string)
uppercase = ("A".."Z").to_a
lowercase = ("a".."z").to_a
letters = string.split('')
snake_case = []
letters.each do |letter|
# first uppercase letter
if letters.index(letter) == 0 && uppercase.include?(letter)
# Sudoku Solution Validator
# Write a function validSolution/ValidateSolution/valid_solution() that accepts a 2D array representing a Sudoku board, and returns true if it is a valid solution, or false otherwise. The cells of the sudoku board may also contain 0's, which will represent empty cells. Boards containing one or more zeroes are considered to be invalid solutions.
# The board is always 9 cells by 9 cells, and every cell only contains integers from 0 to 9.
def valid_solution(board)
range = (1..9).to_a
simple = board.flatten
return false if simple.include?(0)
@amyhenning
amyhenning / luhn.rb
Last active September 26, 2018 20:56
Created a method to determine if a credit card number is valid using the Luhn Algorithm
module Luhn
def self.is_valid?(number)
# split the number into an array of its individual digits
numbers = number.to_s.split('').map { |x| x.to_i }
# set the index value for the starting number to be the second to last digit in numbers
digit_to_double = numbers.length - 2
# until digit_to_double reaches the end of the array
while digit_to_double >= 0
# double the number with an index of digit_to_double
@amyhenning
amyhenning / linked_list_two.rb
Last active September 1, 2018 01:00
Built a method to reverse a linked list using a queue, then created a method to check if a list is an infinite loop using Floyd's Cycle Detection Algorithm
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
end
def print_values(list_node)
@amyhenning
amyhenning / join.js
Last active September 1, 2018 00:57
Built the join method using JavaScript
"use strict";
var _ = {
join: (array, separator = ',') => {
var newString = array.toString();
if (separator) {
var replaced = newString.replace(",", separator);
}
console.log(replaced);
}