Skip to content

Instantly share code, notes, and snippets.

@hackjoy
hackjoy / bottles_test_5.rb
Created August 18, 2014 21:11
bottles_test_5.rb
# ... previous setup and tests not shown for brevity ...
def test_the_whole_song
assert_equal bottles.verses(99, 0), bottles.song
end
# skipped tests not shown for brevity
@hackjoy
hackjoy / bottles_lib_6.rb
Created August 18, 2014 21:14
bottles_lib_6.rb
class Bottles
# ... verse() & verses() methods not shown for brevity ...
def song
verses(99,0)
end
end
@hackjoy
hackjoy / bottles_lib_7.rb
Created August 19, 2014 17:51
bottles_lib_7.rb
class Bottles
def verse(num)
case num
when 0
"No more bottles of beer on the wall, no more bottles of beer.\n" +
"Go to the store and buy some more, 99 bottles of beer on the wall.\n"
when 1
"1 bottle of beer on the wall, 1 bottle of beer.\n" +
"Take it down and pass it around, no more bottles of beer on the wall.\n"
@hackjoy
hackjoy / atom_packages.txt
Created August 17, 2015 23:02
Atom packages
├── Stylus@1.1.0
├── Zen@0.16.2
├── advanced-new-file@0.4.3
├── atom-beautify@0.28.11
├── atom-jade@0.3.0
├── atom-lint@0.20.1
├── atom-rails@0.4.0
├── atomatigit@1.5.4
├── auto-indent@0.5.0
├── auto-update-packages@1.0.0
@hackjoy
hackjoy / react-example.js
Last active November 11, 2015 19:26
React Example
import React from 'react';
export default React.createClass({
displayName: 'Button',
propTypes: {
text: React.PropTypes.string.isRequired,
isDisabled: React.PropTypes.bool
},
getDefaultProps() {
return {
@hackjoy
hackjoy / count_frequency_of_string_values.rb
Created January 11, 2013 12:37
A method to count the frequency that each value within a string of comma separated values appears in another string of comma separated values. Usage: count_frequency_of_string_values(["1,2,"], ["1,2,2,2"]) => ["1-1", "2-3"]
require 'benchmark'
require 'test/unit'
def count_frequency_of_string_values(key, observation)
# convert arguments into arrays for comparison
key_array = key.split(",").map { |s| s.to_i }
observation_array = observation.split(",").map { |s| s.to_i }
# for each key - count how many matches within observation and add result to the output
output = []
key_array.each do |k|
@hackjoy
hackjoy / month_hall_problem.rb
Created January 11, 2013 15:35
Problem: Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to swi…
def createDoors
doors = [false, false, false]
# set one door equal to the car
doors[rand(3)] = true
doors
end
def openADoorThatHasAGoat(doors, userChoice)
# return the first false element after removing the users choice and a true value (if present)
([0, 1, 2] - [userChoice, doors.index(true)]).first
@hackjoy
hackjoy / multiply_numbers_in_array.py
Last active December 11, 2015 08:49
Return the sum of multiplying all numbers in an array
def product_list(numbers):
result = 1
for number in numbers
result = result * number
return result
@hackjoy
hackjoy / search_engine.py
Last active December 11, 2015 08:58
Simple implementation of web crawler and search engine
# pull content of a url
def get_page(url):
try:
import urllib
return urllib.urlopen(url).read()
except:
return ""
# find the links within a page
def get_next_target(page):
@hackjoy
hackjoy / split_string.py
Last active December 11, 2015 09:58
Split a string based on predefined characters
def split_string(string, split_characters):
output = []
atsplit = True
for char in source:
if char in splitlist:
atsplit = True
else:
if atsplit:
output.append(char)
atsplit = False