Skip to content

Instantly share code, notes, and snippets.

@suryart
suryart / fizzbuzz.rb
Last active August 24, 2016 19:32
Write a program which prints the numbers from 1 to N, each on a new line. But for multiples of three print “Fizz” instead of the number 3 and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Read in the input number from STDIN.Solution below is in ruby.
1.upto(STDIN.gets.to_i) do |num|
break if num > 10**7 # num should be less than 10^7
output = nil
output = output.to_s + 'Fizz' if num % 3 == 0
output = output.to_s + 'Buzz' if num % 5 == 0
puts output || num
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 / 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>
@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
@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 / 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