Skip to content

Instantly share code, notes, and snippets.

def coin_change n, d
s = Array.new(n+1) {0}
s[0] = 0
for i in 1..n
s[i] = get_min(s, i, d) + 1
end
return s[n]
end
B = [
[3],
[7,4],
[2,4,6],
[8,5,9,3]
]
############# Iterative (bottom up)
DP = Array.new(B.length) {Array.new(B.length) {0}}
def sum sol, p
s = p[0][0]
n = p.length
index = sol[0]
for i in 1..(n-1)
row = p[i]
for j in 0..(row.length-1)
if j == index
########
# HELPERS
########
extend = (protoProps, staticProps) ->
parent = this
child
if (protoProps && _.has(protoProps, 'constructor'))
child = protoProps.constructor;
else
@pavlos-io
pavlos-io / reverseWords
Created September 30, 2014 22:38
Reverse words in a sting
reverseMe = function(string){
var result = "",
arr = string.split(" ");
for (var j=0; j <=arr.length-1; j++) {
for (var i = arr[j].length - 1; i >= 0; i--) {
result += arr[j][i]
if (i == 0) result += " "; //put space when EOW
};
};
@pavlos-io
pavlos-io / unique chars
Created September 30, 2014 21:18
Unique characters in a string
def first_unique_char(string)
count = {}
string.split("").each do |char|
if count[char]
count[char] += 1
else
count[char] = 1
end
end
@pavlos-io
pavlos-io / needleHackstack
Created September 26, 2014 00:18
Needle Haystack
function find(string, sub){
var small = sub.split("");
var found = [];
var indexes = [];
var j = 0;
for (i=0; i<=string.length-1; i++){
if( string[i] == small[j] ){
found.push( small[j] );
if(j == 0)
@pavlos-io
pavlos-io / Latin Pig&Eng
Created September 24, 2014 17:17
Specs by Vaggelis, code by Paul.
VOWELS = %w{a e i o u}
def to_pig (text)
text.gsub(/(\A|\s)\w+/) do |str|
str.strip!
str += str[0] #appends first char
kind = (VOWELS.include? str[0]) ? 'vowel' : 'consonant'
is_upper = str[0].upcase! #returns nil if str[0] is alredy uppercase
@pavlos-io
pavlos-io / prime factors
Created June 10, 2014 16:19
prime factors
primes = [];
memo = [2];
num = 13195;
function isPrime(n) {
for(i = 0; i <= memo.length; i++){
if (n % memo[i] == 0)
return false;
}
return true;
var alphabet = 'abcdefghijklmnopqrstuvwxyz',
shipher = [],
key,
output = [];
function generateKey(input, string){
for(var j = 0; j < input.length; j++){
var numRand = Math.floor( Math.random() * (string.length-1) ); //gets a random integer between [0 - alphabet.length]
shipher.push( string.charAt(numRand).toUpperCase() ); //appends next char to shipher.
}