Skip to content

Instantly share code, notes, and snippets.

Create 9 by 9 Array

string = ["E", "Y", "T", "B", "H", "I", "S", "S", "R", "P", "N", "E", "H", "F", "Qu"]
new_array = Array.new(9) {string.shift(9)}

> [["E", "Y", "T", "B"], ["H", "I", "S", "S"], ["R", "P", "N", "E"], ["H", "F", "Qu"]] 

@jkaihsu
jkaihsu / zoo.js
Last active December 16, 2015 22:09 — forked from dbc-challenges/zoo.js
//------------------------------------------------------------------------------------------------------------------
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here.
//------------------------------------------------------------------------------------------------------------------
function Animal(species, legs) {
this.species = species;
this.legs = legs;
}
var Zoo = {
@jkaihsu
jkaihsu / index.html
Last active December 16, 2015 22:09 — forked from dbc-challenges/index.html
DBC Phase 2 Practice Assessment Part 3
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="http://cdn.jsdelivr.net/normalize/2.1.0/normalize.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lato:100,900">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.0.2/css/font-awesome.min.css">
</head>
@jkaihsu
jkaihsu / sudoku_refactoring.rb
Created March 19, 2013 15:06
Not done yet.
class Sudoku
def initialize(string)
string_of_array = string.split("")
@board = Array.new(9) {string_of_array.shift(9)}
end
def solve!
first_empty = find_empty_cell
@jkaihsu
jkaihsu / triangle.rb
Created March 7, 2013 08:19
Write a method print_triangle which takes at its input an integer representing the number of rows to print, and prints out a right triangle consisting of * characters, one line per row. For example, print_triangle(5) should print out:
def print_triangle(rows)
for j in 1..rows do
for i in 1..j do
print "*" * 1
end
puts
end
end
@jkaihsu
jkaihsu / count_bwtn.rb
Created March 7, 2013 08:15
Write a method count_between which takes three arguments as input: An Array of integers An integer lower bound An integer upper bound count_between should return the number of integers in the Array between the two bounds, including the bounds It should return 0 if the Array is empty.
def count_between(array, lower_bound, upper_bound)
array.count{|x| x >= lower_bound && x <= upper_bound}
end
@jkaihsu
jkaihsu / pad.rb
Created March 7, 2013 08:23
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. For example, ruby [1,2,3].pad(5) should return [1,2,3,nil,nil] And ruby [1…
class Array
def pad!(min_size, value = nil)
x = min_size - self.count
x.times {self << value}
self
end
def pad(min_size, value = nil)
self.clone.pad!(min_size, value)
end
@jkaihsu
jkaihsu / commas1.rb
Created March 7, 2013 08:21
Write a method separate_comma which takes an integer as its input and returns a comma-separated integer as a string.
def separate_comma(number)
number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end