Skip to content

Instantly share code, notes, and snippets.

@suryart
suryart / binary_tree.rb
Last active August 24, 2016 19:27
Algo and DS implementations in Ruby
class BinaryTreeNode
attr_accessor :left, :right, :data
def initialize(data)
@data = data
end
end
class TreeQueue

Implementation of flatten method of Array#flatten

Requires Ruby 1.9.2 or above. This implementation adds Array#my_flatten to an Array object, which is itself a replica of Array#flatten which is done in C.

Run test

Make sure you have minitest gem installed: $ gem install minitest. Then run:

ruby my_flatten_test.rb

@suryart
suryart / mysql-start.sh
Last active August 1, 2016 05:20
OSX - Starting MySQL . ERROR! The server quit without updating PID file (/usr/local/var/mysql/<computer_name>.err
// Just empty macbook.local.err file then restart the server
rm /usr/local/var/mysql/macbook.local.err
mysql.server start
@suryart
suryart / keybase.md
Created February 25, 2015 07:13
keybase

Keybase proof

I hereby claim:

  • I am suryart on github.
  • I am srt (https://keybase.io/srt) on keybase.
  • I have a public key whose fingerprint is 0BEA 34ED DE36 3D58 3601 FC41 BED2 86EA 420A F92C

To claim this, I am signing this object:

@suryart
suryart / check_prime.rb
Created December 21, 2013 16:13
check prime number
def is_prime?(num)
return false if num <= 1
2.upto(Math.sqrt(num).to_i) do |x|
return false if num%x == 0
end
true
end
module InstanceModule
def track
"Tracking todo: #{self.name}"
end
end
module ClassModule
def class_name
"Tracking class: #{self.name}"
@suryart
suryart / palindrome_count_string.rb
Created November 21, 2013 07:45
Stackoverflow: http://stackoverflow.com/questions/20104428/palindrome-count-in-a-string Palindrome count Problem: Given a string S, count the number of non empty sub strings that are palindromes. A sub string is any continuous sequence of characters in the string. A string is said to be palindrome, if the reverse of the string is same as itself.…
require 'benchmark'
def my_method
string = 'dskjkd'
chars = string.split('')
counts = chars.count
(2..chars.count).each do |len|
chars.combination(len).each do |comb|
string = comb.inject(:<<)
counts += 1 if string.reverse == string
@suryart
suryart / string_size_vs_length.rb
Created November 21, 2013 06:14
String .size vs .length
require 'benchmark'
string = 'dskjkd'
n = 500000
Benchmark.bm(40) do |x|
x.report("string.size "){ n.times { string.size } }
x.report("string.length "){ n.times { string.length } }
end
@suryart
suryart / find_sum_of_array.rb
Created November 20, 2013 18:14
I need to locate all integer elements in an array, whose sum is equal to one of the integer elements within the array. For example, let's assume I have an array like this as input: [1, 2, 4, 10, 90, 302, 312, 500] Then output should have all integer elements including the integer element which is sum of other elements. It will be like: [10, 302,…
arr = [1, 2, 4, 10, 90, 302, 312, 500]
(2..arr.count).each do |len|
arr.combination(len).each do |comb|
sum = comb.inject(:+)
if arr.include? sum
puts (comb << sum).inspect
end
end
end
@suryart
suryart / application.html.erb
Last active October 26, 2023 00:16
Rails 4 flash messages using Twitter Bootstrap(bootstrap-sass: https://github.com/thomas-mcdonald/bootstrap-sass). An improved version of https://gist.github.com/roberto/3344628
// layout file
<body>
<div class="container">
<%= flash_messages %>
<%= yield %>
</div><!-- /container -->
</body>