Skip to content

Instantly share code, notes, and snippets.

@a656184
a656184 / index.html
Last active December 21, 2015 02:49 — 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>
class Vehicle
attr_accessor :wheels
attr_reader :status
def initialize(args)
@color = args[:color]
@wheels = args[:wheels]
end
class Car
@@WHEELS = 4
def initialize(args)
@color = args[:color]
@wheels = @@WHEELS
end
def drive
@status = :driving
end
def brake
@a656184
a656184 / gist:5913520
Last active December 19, 2015 06:39
Recursive RPN Calculator
def rpn_calculator (string)
return 'Not valid input' if string.class != String
return string.to_i if string.split(' ').length == 1
new_string = string.scan(/\d+\s{1}\d+\s{1}[^0-9]/)
eval_arr = new_string[0].to_s.split(' ')
result = 0
if eval_arr[2] == '+'
result = eval_arr[0].to_i + eval_arr[1].to_i
@a656184
a656184 / gist:5904262
Created July 1, 2013 20:25
Longets Collatz
def longest_collatz_string(end_range)
longest_chain = Array.new
temp_array = Array.new
((end_range/2)..end_range).each do |number|
temp_array = []
temp_array << number
until temp_array.last == 1
temp_array.last.even? ? temp_array << temp_array.last / 2 : temp_array << temp_array.last * 3 + 1
end
longest_chain = temp_array if temp_array.length > longest_chain.length
@a656184
a656184 / gist:5895500
Created June 30, 2013 15:07
Recursive Flatten Exercise - Refactored
a = ["bananas", [1,2,3], ["apple", "cheese", [100, 20]], [true], [4.0, 7, 32]]
def flatten(array)
return array unless array.is_a?(Array)
new_array = Array.new
array.each do |item|
new_array << item
end
flatten(new_array)
end
@a656184
a656184 / gist:5895467
Last active December 19, 2015 04:09
Recursive Flatten Exercise
a = ["bananas", [1,2,3], ["apple", "cheese", [100, 20]], [true], [4.0, 7, 32]]
def flatten(array)
nested_array_counter = 0
array.each { |item| nested_array_counter += 1 if item.kind_of?(Array)}
return array if nested_array_counter == 0
new_array = Array.new
array.each do |item|
if item.class == Array
@a656184
a656184 / gist:5864079
Last active December 18, 2015 23:49
Project Euler # 12 - Highly divisible triangular number
def nth_divisors(desired_divisors)
natural_number_value = 1 # 1, 3, 6, 10, 15, 21, 28, 36, 45, 55....these are the natural value numbers
natural_number_increment = 2 # To create a natural number we must increment +1, +2, +3, +4, +5, +6, etc
divisor = 1 # This will be used as the denominator to our natural number
number_of_divisors = 0 # Serves as a counter to log how many times the divisor divides evenly into the natural number
until number_of_divisors >= desired_divisors # Code should end when the number of divisors for the natural number has reached my desired number of divisors
divisor = 1
number_of_divisors = 0
until divisor > natural_number_value # Should stop when denominator becomes greater than numerator
number_of_divisors += 1 if natural_number_value % divisor == 0
@a656184
a656184 / gist:5707868
Last active December 18, 2015 01:59
Regex
# Determine whether a string contains a Social Security number.
def has_ssn?(string)
/(\d{3}-\d{2}-\d{4})/.match(string)
end
# Return the Social Security number from a string.
def grab_ssn(string)
m = String.new
m = nil
if /(\d{3}-\d{2}-\d{4})/.match(string)