Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / fibonacci.rb
Created March 7, 2013 08:22
Check, if a number i is part of the Fibonacci sequence.
def is_fibonacci?(i)
sum = [0,1]
x = 0
y = 0
while sum.max <= i
sum.length.times do |a|
x = sum[a] + sum[a-1]
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
@jkaihsu
jkaihsu / reverse_words.rb
Created March 7, 2013 08:20
Write a method reverse_words which takes a sentence as a string and reverse each word in it.
def reverse_words(str)
words = str.split(' ')
reverse_str = []
words.length.times do |i|
reverse_str[i] = words[i].reverse
end
return reverse_str.join(" ")
@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