Skip to content

Instantly share code, notes, and snippets.

View nelsonic's full-sized avatar
💭
Let's Build a Better World Together!☀️

Nelson nelsonic

💭
Let's Build a Better World Together!☀️
View GitHub Profile
@nelsonic
nelsonic / change_spec.rb
Created January 19, 2013 03:00
Coin Change Problem solved in Ruby using While and Until. US Coins. Requires RSpec. To run: $ rspec change_spec.rb Not the fewest lines possible but definitely easy for others to understand. ;-)
class Change
def change(amount)
coins = []
remaining_amount = amount
while remaining_amount != 0
coin = get_highest_coin(remaining_amount)
coins << coin
remaining_amount = remaining_amount - coin
end
coins
@nelsonic
nelsonic / change_spec.rb
Created January 19, 2013 03:19
Coin Change Problem Single Method. Solved in Ruby. Nested until loops. ( requires RSpec) Available coins are USD: http://en.wikipedia.org/wiki/Coins_of_the_United_States_dollar ;-)
class Change
def change(amount)
available_coins = [100,50,25,10,5,1] # http://en.wikipedia.org/wiki/Coins_of_the_United_States_dollar
coins = []
index = 0
coin = available_coins[index]
remaining_amount = amount
until remaining_amount == 0
until remaining_amount >= coin
index = index + 1
@nelsonic
nelsonic / change_spec.rb
Created January 19, 2013 04:14
Coin change problem. Solved in Ruby. using each loop Fewest lines (so far) slightly fancy: times and inject methods used - but no faster than the simple nested until loops Requires RSpec to run.
class Change
def change(amount)
available_coins, coins, remaining_amount = [100,50,25,10,5,1], [], amount
available_coins.each { | coin |
if remaining_amount - coin >= 0
remaining_amount / coin != 0 ? (remaining_amount / coin).to_int.times { coins << coin } : coins << coin
puts "Remaining Amount: #{remaining_amount} | Coin: #{coin}" # console feedback :-)
remaining_amount = amount - coins.inject(:+) # see: http://stackoverflow.com/a/1538801/1148249
end
}
@nelsonic
nelsonic / change_rspec.rb
Last active December 11, 2015 08:08
Coin Change Problem Solved in 5 Lines of Ruby. ( Method code on SINGLE Line! ;-) Do NOT do this in RL kids! ( Seriously Unreadable/Unmaintainable! Simply a Thought Exercise in taking compression to its logical conclusions ... ) Surprisingly fast to execute! :-P
class Change
def change(amount)
available_coins, coins, remaining_amount = [100,50,25,10,5,1], [], amount; available_coins.each { |coin| if ((remaining_amount/coin).to_int > 0) then (remaining_amount/coin).to_int.times { coins << coin }; remaining_amount = amount-coins.inject(:+) end }; return coins
end
end
describe Change do
it "returns [1] for 1" do
expect(subject.change(1)).to eq [1]
end
@nelsonic
nelsonic / disco.html
Last active December 14, 2015 06:29
Disco Squares CoffeeScript HTML5 Canvas Just copy paste the script into an .html file and see. A variation on the disco on p.21 of Jump Start CoffeeScript by Earle Castledine
<html>
<head>
<title> Disco Squares</title>
</head>
<body>
<style type="text/css">
html, body, #game {
width: 100%;
height: 100%;
margin: 1px;
@nelsonic
nelsonic / example-app.json
Last active December 16, 2015 02:39
example-app.json
{
 "attributes" : {
   "type" : "Application__c",
   "url" : "/services/data/v27.0/sobjects/Application__c/a07b0000004bXrFAAU"
 },
 "Mandatory__c" : false,
 "LastModifiedById" : "005b0000000MGa7AAG",
 "Featured_End_Date__c" : "2013-04-26T16:16:00.000+0000",
 "OwnerId" : "005b0000000MGa7AAG",
 "LastModifiedDate" : "2013-04-11T14:43:15.000+0000",
@nelsonic
nelsonic / sort.html
Created April 16, 2013 16:47
ASCII Ascending Order Sort Sort in HTML/JavaScript
<html>
<h1>Sort</h1>
<!-- enable the Chrome Developer Console to see Results -->
<script>
str = 'X,a,A,C,D,F,1,c,4,3'
data = str.split(',')
console.dir(data)
counter = 0;
limit = data.length;
end = limit-1;
@nelsonic
nelsonic / HTTPHelper.java
Created April 18, 2013 03:43
This is what Nimil had.
/**
* @author Nimil Christopher
* @date 12 April 2013
* @description This class has the Rest Callouts to publish an application to
* the CDN
*/
public with sharing class HTTPHelper {
public static Http http;
@nelsonic
nelsonic / hello.coffee
Created April 19, 2013 13:59
A Basic Hello World NodeJS script in CoffeeScript. This can be a two or three line script. but i've done it this way to explain the components.
http = require 'http' # Require the HTTP Core NodeJS Module (notice the lack of brackets)
port = process.env.PORT or 4000 # The TCP port you want to "listen" on (see line #6) allows the system (e.g. Heroku to set it or uses 4000 on local machine)
http.createServer (req, res) -> # Creates a basic Web Server and gives you the REQuest and RESponse objects to the function
res.writeHead 200 # Write the HTTP Status Code "200" ("All OK") in the RESponse to the client (browser)
res.end 'Hello World!' # End the RESponse with the message 'Hello World'
.listen port # the dot before the word listen means "Chain" to the createServer and listen on the port
console.log "Visit: http://localhost:#{port}" # console.log allows you to write a "Note-to-self" on the command line.
### if any of the above is unclear, Google it! (or msg me for a personal Tutorial: https://twitter.com/nelsonic) ###
<apex:page standardController="Opportunity" extensions="jquery_minihack">
<style>
#myTitle {
font-size:20px;
text-align: center;
}
.element {
width:50%;