Skip to content

Instantly share code, notes, and snippets.

@BadAllOff
Last active January 8, 2018 00:47
Show Gist options
  • Save BadAllOff/f632b2ba9df2c12c80f67653254f6cf1 to your computer and use it in GitHub Desktop.
Save BadAllOff/f632b2ba9df2c12c80f67653254f6cf1 to your computer and use it in GitHub Desktop.
Just another coding questions
# One away same Strings
# given 2 strings
# functions is_one_away(a,b) should return TRUE if they are one edit away
# 3 type of operations allowed
# 1 - change char
# 2 - add char
# 3 - delete char
#
# example 1
# a = "abcde", b = "abfde"
# we can make this strings the same by applying only
# one operation (changing "c" to "f" or vice versa)
#
# example 2
# a = '1qwerty', b = 'qwerty'
# we can make this strings the same by applying
# delete char operation (delete "1" in "a")
# or add char operation (add "1" to "b")
#
# ... I know ... it looks creepy, but it's really intuitively understandable
class TestClass4
def self.is_one_away(a1,b1)
arrs = sort_arrays_sizes(a1,b1)
larger_array = arrs[0].split("")
smaller_array = arrs[1].split("")
size_diff = larger_array.size - smaller_array.size
return false if size_diff > 1
equal ||= size_diff == 0 ? true : false
@@count_diff = 0
larger_array.each_with_index do |char, index|
return false if @@count_diff > 1
if larger_array[index + 1].nil? && @@count_diff == 1
p "This strings are one way ok"
return false
end
if @@count_diff == 1
if equal
s_arr_index = index + 1
l_arr_index = index + 1
else
l_arr_index = index + 1
s_arr_index = index
end
else
l_arr_index = index
s_arr_index = index
end
if is_same_characters?(larger_array[l_arr_index], smaller_array[s_arr_index])
p "Character #{larger_array[l_arr_index]} is equal to charachter #{smaller_array[s_arr_index]}"
else
p "'#{larger_array[l_arr_index]}' is not equal to '#{smaller_array[s_arr_index]}'"
@@count_diff +=1
if @@count_diff > 1
puts "Stop, To many errors"
return false
end
p "... checking if this arrays are equal size..."
if equal
p "Arrays are in equal size"
p "... checking if it's not last char..."
compare_chars(larger_array, smaller_array, l_arr_index+1, s_arr_index+1)
else # not_equal
p "Arrays are not in equal size"
p 'Found mismatching, cheking for next char ...'
p "... checking if it's not last char..."
compare_chars(larger_array, smaller_array, l_arr_index+1, s_arr_index)
end # not_equal end
end
end
p "This strings are one way ok"
p "#{@@count_diff} errors total"
end
def self.compare_chars(larger_array, smaller_array, l_arr_index, s_arr_index)
if !larger_array[l_arr_index].nil?
p 'Not the last char'
p '... proceed'
if is_same_characters?(larger_array[l_arr_index], smaller_array[s_arr_index])
p "Next char (in BIGGER string) #{larger_array[l_arr_index]} is egual to current char #{smaller_array[s_arr_index]} in smaller string"
p "... we can proceed"
return true
else
p "'#{larger_array[l_arr_index]}' is not equal to '#{smaller_array[s_arr_index]}'"
p 'Found next mismatching, this strings is not one_way equal'
@@count_diff +=1
return false
end
else
if @@count_diff > 1
p "This strings are NOT equal size BUT NOT one way ok"
return false
else
p "This strings are NOT equal size and one way ok"
return true
end
end
end
def self.is_same_characters?(a,b)
return a == b
end
def self.sort_arrays_sizes(a,b)
if a.size > b.size
larger_array = a
smaller_array = b
else
larger_array = b
smaller_array = a
end
return [larger_array, smaller_array]
end
end
# find uniq charachters in given string
class TestClass3
def self.uniq_elem(someStr)
someStr = someStr.split("")
h = Hash.new(0)
someStr.each { |item| h[item] += 1 }
result = h.select do |key, val|
if val == 1
puts "#{key} is unique"
return
end
end
puts 'No repeating chars'
end
end
# given two arrays
# [1,2,3,4,5,6,7,8,9] and [3,4,5,6,7,8,9,1,2,] *no duplicates
# func takes this arrays and returns TRUE
# if one of them is rotation of the other
class TestClass2
def self.is_rotation?(a,b)
#sizes should be equal
return false if a.size != b.size
key = a[0]
key_i = -1
# find first intersection
b.each_with_index do |item, index|
if item == key
#save the index of first intersection
key_i += index
puts "for #{item} Key_i is #{key_i}"
break
end
end
return false if key_i == -1
a.each_with_index do |item, index|
#find a position for b
j = ((key_i +=1) % a.size )
# return FALSE if no intersection in given position
return false if a[index] != b[j]
end
# return TRUE if everything passed
return true
end
end
class TestClass
def self.test_function(someArr)
h = Hash.new(1)
someArr.each { |item| h[item] += 1 }
puts "Max times was number #{largest_hash_key(h)}"
end
def self.largest_hash_key(hash)
hash.key(hash.values.max)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment