VinceBarresi
BAN USERsimple O(n) solution in ruby
# O(n)
def replace_zeroes zero_arr = []
0.upto(zero_arr.size) {|i| zero_arr[i] += 5 if zero_arr[i] == 0}
zero_arr
end
In Ruby
# O(n)
def sum_of_index_values sum_arr = [], sum
sum_hash, i = {}, 0
sum_arr.each do |x|
sum_hash.store(i, x)
i+=1
end
0.upto(sum_arr.size-1) do |i|
remainder = sum - sum_arr[i]
puts "#{remainder} " + '(index '"#{sum_arr[i]}"')' if sum_hash.include?(remainder)
end
end
solution in ruby
def nodes_from_tail n
n_list = LinkedList.new(1)
2.upto(10) { |x| n_list.add(x) }
reverse_list = n_list.list_to_reverse_arr
new_list = LinkedList.new(reverse_list[0])
1.upto(n-1) { |i| new_list.add(reverse_list[i]) }
new_list.display
end
Where I have the following method added to my custom LinkedList class:
def list_to_reverse_arr
current = @head
full_list = []
while current.next_node != nil
full_list += [current.value.to_s]
current = current.next_node
end
full_list += [current.value.to_s]
full_list.reverse
end
here is a method in ruby using an array to check if the list is a palindrome
def is_palindrome?
current = @head
full_list = []
while current.next_node != nil
full_list += [current.value.to_s]
current = current.next_node
end
full_list += [current.value.to_s]
full_list == full_list.reverse ? true : false
end
Simple ruby solution
def create_c a, b
c = a.to_s.split('').sort.reverse.join.to_i
c < b ? nil : c
end
Ruby solution - O(n)
def index_at_key key, arr
puts 'key is: ' + key.to_s
i, j, key_arr = 0, 0, []
while i <= arr.size - 1
if arr[i] == key
key_arr[j] = i
j += 1
end
i += 1
end
key_arr.join(', ')
end
in ruby
def group_anagrams arr = []
sort_arr, sort_hash, new_sort_arr, newer_sort_arr = [], {}, [], []
arr = arr.each {|w| sort_arr << w.split('').sort.join}
0.upto(arr.size-1) do |i|
sort_hash.store(arr[i], sort_arr[i])
end
new_sort_arr << sort_hash.sort_by {|k, v| k.length}.to_h.keys[0..arr.size-1]
end
Simple ruby solution:
# O(n)
def anagram? str1, str2
str1, str2 = str1.gsub(' ', '').split('').sort.join, str2.gsub(' ', '').split('').sort.join
str1 == str2 ? true : false
end
ruby solution -
def equal_part_sum_string str
half, i, arr_1, arr_2, sum_1, sum_2 = str.length / 2, 0, [], [], 0, 0
str.split('').each do |c|
i < half ? arr_1 << c : arr_2 << c
i += 1
end
0.upto(arr_1.size) do |i|
sum_1 += i
end
0.upto(arr_2.size) do |i|
sum_2 += i
end
if sum_1 == sum_2
str = '12345' + arr_2.join + arr_1.join
end
end
Simple Ruby Solution in linear time
# O(n)
def remove_duplicates str1, str2
bigger = (str1.length > str2.length ? str1 : str2).split('')
smaller = (bigger == str1 ? str2 : str1).split('')
bigger.each do |c|
if smaller.include?(c)
bigger.delete(c)
smaller.delete(c)
end
end
smaller.join + ' ' + bigger.join
end
Not the prettiest solution but ruby solution works
=begin
Give this input: Sea!tle is a nice place. Work Hard! have Fun, make HIStory!
display this output using any language
Seattle is a nice place.
Work hard.
Have fun.
Make history.
=end
def format_statement
str = 'Sea!tle is a nice place. Work Hard! have Fun, make HIStory!'
str = str.split(" ")
str[0] = str[0].gsub('!', 't')
first, i, first_part = str[0..4], 5, first_part = true
puts first.join(" ")
until i == str.size do
if first_part == true
str[i] = str[i].capitalize
first_part = false
elsif
str[i] = str[i].downcase
first_part = true
end
i +=1
end
str = str.join(" ").gsub(",", ".").gsub("!", '.').split(" ")
puts str[5..6].join(" ") + "\n" + str[7..8].join(" ") + "\n" + str[9..10].join(" ")
end
format_statement
Ruby Solution
def most_char str
max, letter, i, count, str_arr = 0, '', 0, [], str.split("")
str_arr.each do |c|
count += [str.count(c)]
if count[i] > max
max = count[i]
letter = str_arr[i]
end
i+=1
end
letter
end
RepCorrinaAyala, Field Sales at Coupondesh
Corrina, works as a Training and development manager. The Training and Development Manager is responsible for improving the productivity of ...
In Ruby
- VinceBarresi September 16, 2015