Skip to content

Instantly share code, notes, and snippets.

View aarti's full-sized avatar
🎯
Focusing

aarti

🎯
Focusing
  • San Francisco Bay Area
View GitHub Profile
@aarti
aarti / negative_la_lb__regexp
Created February 26, 2014 16:51
Regexp using negative lookahead and negative look behind assertions
irb(main):040:0> testcases
=> ["hello world", "'hello world", "'hello' world", "hello' world"]
irb(main):041:0> testcases.each { |i| puts /(?<!')hello(?!')/.match(i) }
hello
=> ["hello world", "'hello world", "'hello' world", "hello' world"]
audience_range_values = []
age_from = nil
audience_range_values << age_from || 3 # does not work, returns book.a
print audience_range_values
audience_range_values = []
# output [nil]
@aarti
aarti / extract_largest_value.rb
Created May 2, 2014 00:29
How can I extract the bigger value for each key in a list of hashes in Ruby
Many concepts here to revisit, so saving in gist.
http://stackoverflow.com/questions/21712814/how-can-i-extract-the-bigger-value-for-each-key-in-a-list-of-hashes-in-ruby/21729254?noredirect=1#comment32860522_21729254
a = [{1=>19.4}, {1=>12.4}, {2=>29.4}, {3=>12.4}, {2=>39.4}, {2=>59.4}]
# the below is the main trick, to group the hashes and sorted the key/value pair
# in **ascending order**.
a.sort_by(&:to_a)
# => [{1=>12.4}, {1=>19.4}, {2=>29.4}, {2=>39.4}, {2=>59.4}, {3=>12.4}]
@aarti
aarti / string_multiplication
Created June 17, 2014 05:37
string multiplication
irb(main):001:0> 'A' * 3
=> "AAA"
@aarti
aarti / splat_operator
Created June 25, 2014 18:38
combining ranges into an array using splat operator to generate a random string of two chars
# I thought this was neat
# Reference: http://stackoverflow.com/questions/21404323/combining-two-different-ranges-to-one-in-ruby
[*'0'..'9', *'a'..'z', *'A'..'Z'].shuffle.first(2).join
@aarti
aarti / js_map_generic
Created July 8, 2014 15:20
Javascript Map Calling directly or Generically
function genericallyCallingMap(dna) {
var complements = {"C":"G", "G":"C", "T":"A", "A":"U"};
var map = Array.prototype.map
var a = map.call(dna, function(x) {
return complements[x];
})
return a.join("");
};
function callingMapOnAnArray(dna) {
var a = dna.split("").map( function(x) {
@aarti
aarti / bst.rb
Last active August 29, 2015 14:04
Binary Search Tree Count Unival
class Bst
attr_accessor :data, :left, :right
def initialize(data)
self.data = data
end
def insert_r(data)
if (self.right )
@aarti
aarti / linked_list.rb
Created September 8, 2014 01:54
Simple Linked List
class Element
attr_accessor :datum, :next
def initialize(datum,next_element)
@datum = datum
@next = next_element
end
def reverse
@aarti
aarti / resize_hash
Created September 26, 2014 18:39
Ruby lang hash resize algorithim
new_size(st_index_t size)
{
st_index_t i;
for (i=3; i<31; i++) {
if ((st_index_t)(1<<i) > size) return 1<<i;
}
#ifndef NOT_RUBY
rb_raise(rb_eRuntimeError, "st_table too big");
#endif
return -1; /* should raise exception */
@aarti
aarti / config.lua
Last active August 29, 2015 14:07
Corona Simple config.lua supporting multiple devices
--http://forums.coronalabs.com/topic/37362-improvement-to-the-ultimate-configlua-file/
--The effect of that bit of math is to set the content width or height appropriately for letterbox scaling according to the aspect ratio of the device. Why is that good? Because the upper-left corner is now (0,0), and display.contentWidth and display.contentHeight now cover the entire screen. For example, on an iPhone 5, display.pixelHeight is 1136 and display.pixelHeight is 640. If you do the math, you'll see that it'll set width to 320 and height to 568.
-- It'll set the width and height appropriately for every single device, no matter what the resolution or aspect ratio. And there's no need to code into your config.lua different blocks for different devices.
--the upper-left corner isn't at (0,0). Instead, it's at (display.screenOriginX, display.screenOriginY)
local aspectRatio = display.pixelHeight/display.pixelWidth
application =
{
content =