Skip to content

Instantly share code, notes, and snippets.

View npras's full-sized avatar
😃
Coding all the time

Prasanna Natarajan npras

😃
Coding all the time
View GitHub Profile
@npras
npras / my_impl.rb
Created July 3, 2012 05:34
Re-implementing the iterators
class Array
def my_map(&nameless_fn)
new_arr = []
return Enumerator.new(self, :my_map) unless nameless_fn
self.each do |x|
new_arr << nameless_fn.call(x)
end
new_arr
end
@npras
npras / gist:3038097
Created July 3, 2012 06:37
Ruby Challenge 3: Capitalizing words within a string
# Challenge 3: http://www.therubygame.com/challenges/3/submissions
str = "The small brown & ginger fox JUMPED OVER the gate"
# output = "The Small Brown & Ginger Fox Jumped Over The Gate"
# Algorithm:
# 1. downcase #{str}
# 2. to_a
# 3. capitalize on each array elements
# 4. merge as str
def Capitalize(str)
@npras
npras / gist:3038075
Created July 3, 2012 06:30
Ruby Challenge 2: The Twelve Days of Christmas
# challenge 2: http://www.therubygame.com/challenges/2
gifts = "12 Drummers Drumming
11 Pipers Piping
10 Lords-a-Leaping
9 Ladies Dancing
8 Maids-a-Milking
7 Swans-a-Swimming
6 Geese-a-Laying
5 Gold Rings
4 Colly Birds
@npras
npras / gist:3038220
Created July 3, 2012 07:14
Ruby Challenge 5: Roman Numerals
# http://www.therubygame.com/challenges/5/submissions
# = 1
# V = 5
# X = 10
# L = 50
# C = 100
# D = 500
# M = 1000
# Sample i/o:
# MCMXCIX gives 1999, MMXII gives 2012
@npras
npras / gist:3038191
Created July 3, 2012 07:07
Ruby Challenge 4: Puns, Palindromes and Parrots
# Puns, Palindromes and Parrots
# Challenge 4: http://www.therubygame.com/challenges/4/submissions
#str = "abacdfgdcabaypqqpy" # 324 171 163
#str = "natarajan" # 81 45 42 # "ata"
str = "ghjiiooiijplr"
# desired output "ypqqpy"
def find_longest_palindrome(string)
p = ""
@npras
npras / gist:3038253
Created July 3, 2012 07:23
Ruby Challenge 6: Morsels and Dimes
# http://www.therubygame.com/challenges/6/submissions
module RubyChallenge
extend self
MORSE = {
A: '.-', B: '-...', C: '-.-.', D: '-..', E: '.', F: '..-.', G: '--.',
H: '....', I: '..', J: '.---', K: '-.-', L: '.-..', M: '--', N: '-.',
O: '---', P: '.--.', Q: '--.-', R: '.-.', S: '...', T: '-', U: '..-',
@npras
npras / Gemfile
Created July 12, 2012 06:42
gemfile of a gem
source 'https://rubygems.org'
# Specify your gem's dependencies in boomboom.gemspec
gemspec
@npras
npras / gist:3683129
Created September 9, 2012 07:16
Stubbing out a module
# spec/spec_helper_lite.rb
def stub_module(full_name)
full_name.to_s.split(/::/).inject(Object) do |context, name|
begin
context.const_get(name)
rescue NameError
context.const_set(name, Module.new)
end
end
end
@npras
npras / js_fn_def_and_calling.js
Created March 17, 2016 07:47
javascript function definition and calling syntax
// this is function definition.
// you can make a function take zero or more arguments (also called as parameters)
// This Dog fn takes one argument: 'name'
function Dog(name){
return "hello, my name is: " + name;
}
// calling the Dog function
Dog('tommy'); // this will return "hello, my name is: tommy"