Skip to content

Instantly share code, notes, and snippets.

@Bablzz
Last active September 25, 2019 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bablzz/bc7906970df933d2bfd2531b9ced4371 to your computer and use it in GitHub Desktop.
Save Bablzz/bc7906970df933d2bfd2531b9ced4371 to your computer and use it in GitHub Desktop.
small_puzzles.rb
digit = 543210
# Reverse digit
def reverse_digit(digit)
str = digit.to_s.reverse.split('')
if str.first == '0'
str.shift
end
str.join('').to_i
end
puts reverse_digit(543210)
# Output => 12345
# Math method reverse digit
def reverse_math_digit(digit)
reverse = 0
while digit > 0
lastDigit = digit % 10
reverse = (reverse * 10) + lastDigit
digit = digit / 10
end
reverse
end
puts reverse_math_digit(digit)
# Output => 12345
# Find out if two words are anagram
def is_it_anagram(word_1, word_2)
word_1 = word_1.split('')
word_2 = word_2.split('')
word_stat = Hash.new(0)
word_1.each do |w|
if word_stat.has_key?(w)
word_stat[w] += 1
else
word_stat[w] = 1
end
end
word_2.each do |w|
if word_stat.has_key?(w)
word_stat[w] += 1
else
word_stat[w] = 1
end
end
is_anagram = true
word_stat.each_value do |v|
if v%2 != 0
puts "It's a not anagram"
is_anagram = false
break
end
end
is_anagram
end
puts is_it_anagram('start', 'ttars')
puts is_it_anagram('start', 'vasya')
# => true
# => It's a not anagram
# => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment