Skip to content

Instantly share code, notes, and snippets.

View vjdhama's full-sized avatar
🎯
Focusing

Vijay Dhama vjdhama

🎯
Focusing
View GitHub Profile
@vjdhama
vjdhama / Fun with strings
Created July 25, 2013 13:28
Edx Class 169.1x HW 1-1
#!/usr/bin/env ruby
def palindrome?(str)
str = str.downcase
r = /[\W]/ #/\W/ - A non-word character ([^a-zA-Z0-9_])
str = str.gsub(r,'')
return str == str.reverse
end
def count_words(str)
@vjdhama
vjdhama / Rock-Paper-Scissors
Last active December 20, 2015 05:39
Edx Class 169.1x HW 1-2
#!/usr/bin/env ruby
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
class Object
def depth #Check depth of array passed
self.class == Array ? 1 + self[0].depth : 0
end
end
@vjdhama
vjdhama / part3.rb
Created July 29, 2013 21:18
Edx 169.1x Hw 1-3 Anagrams
#!/usr/bin/env ruby
class String
def sort_by_char
self.downcase.split(//).sort.join # Handle all test cases
end
end
def combine_anagrams(words)
sorted_arr = []
@vjdhama
vjdhama / oop.rb
Created July 29, 2013 22:44
Edx Class 169.1x HW 1-4
#!/usr/bin/env ruby
class Dessert
attr_accessor :name, :calories
def initialize(name, calories)
@name = name
@calories = calories
end
@vjdhama
vjdhama / Advanced_oop.rb
Created August 7, 2013 20:28
Edx Class 169.1x HW 1-5
#!/usr/bin/env ruby
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s # make sure it's a string
attr_reader attr_name # create the attribute's getter
attr_reader attr_name+"_history" # create bar_history getter
class_eval %Q"
def #{attr_name}=(value)
if !defined? @#{attr_name}_history
@vjdhama
vjdhama / Advanced_oop_2.rb
Created August 7, 2013 20:29
Edx Class 169.1x HW 1-6
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1}
def method_missing(method_id)
singular_currency = method_id.to_s.gsub( /s$/, '')
@src_currency = singular_currency
if @@currencies.has_key?(singular_currency)
self * @@currencies[singular_currency]
else
super
@vjdhama
vjdhama / Iterators.rb
Created August 7, 2013 20:30
Edx Class 169.1x HW 1-7
#!/usr/bin/env ruby
class CartesianProduct
include Enumerable
def initialize(array_a, array_b)
@cartesian_array = Array.new(0)
array_a.each do |element_a|
array_b.each do |element_b|
tmp_array = Array.new(0)
@vjdhama
vjdhama / index.html
Created August 8, 2013 22:56
via:geojson.io
<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
.marker-properties {
border-collapse:collapse;
@vjdhama
vjdhama / BitmapLruCache
Last active August 29, 2015 13:56
Volley class
public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
public BitmapLruCache(int maxSize) {
super(maxSize);
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
@vjdhama
vjdhama / VolleyRequest
Created March 1, 2014 13:57
Volley Singleton
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class VolleyRequest {
private static VolleyRequest mInstance = null;