This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedListNode | |
attr_accessor :value, :next | |
def initialize(value) | |
@value = value | |
@next = nil | |
end | |
def kth_to_last_node(k, head) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pokemon.rb:14:in `<main>': undefined method `type' for | |
#<Pokemon:0x007ff70613d648> (NoMethodError) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def collatz(n) | |
# set an empty arry to contain the values of the sequence | |
ary = [] | |
# set initial index value to 0 | |
index = 0 | |
# start by pushing n into the array | |
ary.push(n) | |
# loop until the array includes the number 1, i.e. until the sequence is complete | |
until ary.include?(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def recursive_fib(n) | |
# sets the first two values (0 and 1) by default | |
if n < 2 | |
return n | |
# if n is greater than or equal to 2, the nth number in the fibonacci | |
# is the sum of the function called on n-1 and the function called on | |
# n-2 | |
else | |
return recursive_fib(n-1) + recursive_fib(n-2) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var _ = { | |
// Implements: | |
// https://lodash.com/docs#map | |
map: (array, callback) => { | |
return callback(array); | |
} | |
} |
NewerOlder