Skip to content

Instantly share code, notes, and snippets.

@suryart
suryart / graph.rb
Created September 4, 2013 15:59
Locating the route of the shortest path of a map(connected nodes with weight/distance) in Ruby
# !/usr/bin/ruby1.9.2
# encoding: utf-8
class Node
attr_accessor :src, :dst, :length
def initialize(src, dst, length = 1)
@src = src
@dst = dst
@length = length
end
@suryart
suryart / removing last word from a sentence (a string)
Created October 3, 2013 13:35
How to go about removing the last element? For example, I have strings like these: str1 = "My testing String" str2 = "My another testing string" I need a neat way of showing the output: str1 = "My testing" str2 = "My another testing" This is what I could do: str1 = str1.split(" ") str1.delete(str1.last) str1.join(" ") # => "My testing" I was won…
#!/usr/bin/ruby
require 'benchmark'
str2 = "My another testing well string"
n = 500
Benchmark.bm(40) do |x|
x.report("str2[/(.*)\s/,1] "){ n.times { str2[/(.*)\s/,1] } }
x.report("str2[0...str2.rindex(' ')] "){ n.times { str2[0...str2.rindex(' ')] } }
x.report("str2.split(' ')[0...-1].join(' ') "){ n.times { str2.split(' ')[0...-1].join(' ') } }
x.report("str2[/.*(?=\s)/] "){ n.times { str2[/.*(?=\s)/] } }
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 / 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 / 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
module InstanceModule
def track
"Tracking todo: #{self.name}"
end
end
module ClassModule
def class_name
"Tracking class: #{self.name}"
@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

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 / 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