Skip to content

Instantly share code, notes, and snippets.

View itismadhan's full-sized avatar

Madhan Padmanabhan itismadhan

  • Careem
  • Berlin
View GitHub Profile
class JsonHelper {
class func JSONStringify(jsonObj: AnyObject) -> String {
var e: NSError?
let jsonData: NSData! = NSJSONSerialization.dataWithJSONObject(
jsonObj,
options: NSJSONWritingOptions(0),
error: &e)
if e != nil {
return ""
} else {

Implementing a Reverse Polish notation calculator in Ruby

A couple weeks ago I had a go at implementing an RPN calculator in Ruby. I knew I wanted the calculator to function by popping operands out of an array populated with the values of the input expression, operating upon the operands with the appropriate operator, and pushing the result back into the stack of operands.

I was able to implement this in version 1, but it took forever and the resulting code was not very beautiful. Why?

  • I started coding before I had a thorough understanding of RPN

    Wait, 20 10 5 4 + * - is what now?

list = [1, 2, 5, 7, 20]
def check_sum list
list_hash = Hash.new
list.each do |item|
if list_hash.has_key?(10-item)
return true
else
list_hash[item] = true
end
end
# You will need the pygments and xclip packages
# This example highlights some Bash source code
# '-O noclasses=true' tells pygments to embed colors inline in the source
# the '-t text/html' option tells xclip what "target" to specify for the selection
pygmentize -l bash -f html -O noclasses=true mysource.sh | xclip -selection clipboard -t text/html
#!/usr/bin/env ruby
# 2011-10-10 20:57:53 +1000
def merge_sort(a)
return a if a.size <= 1
l, r = split_array(a)
result = combine(merge_sort(l), merge_sort(r))
end
#!/usr/bin/env ruby
# 2011-10-10 20:57:53 +1000
def merge_sort(a)
return a if a.size <= 1
l, r = split_array(a)
result = combine(merge_sort(l), merge_sort(r))
end
module BinaryTree
class Node
attr_reader :word, :count, :left, :right
include Enumerable
def initialize(word)
@word, @count = word, 1
end
@itismadhan
itismadhan / dfs.rb
Created February 8, 2014 01:31 — forked from gosuri/dfs.rb
# Depth-first search (DFS) is an algorithm for traversing or
# searching a tree, tree structure, or graph. One starts at
# the root (selecting some node as the root in the graph case)
# and explores as far as possible along each branch before backtracking.
#
# A graph can be represented by its adjacency matrix G,
# where G[i][j] == 1 if there is an edge between
# vertices i and j and 0 otherwise.
#
# Below Graph in diagram http://i.imgur.com/sV1UzUn.png