Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Distance_to_nearest_vowel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
=begin
Write a function that takes in a string and for each character,
returns the distance to the nearest vowel in the string.
If the character is a vowel itself, return 0.

Examples

distance_to_nearest_vowel("aaaaa") ➞ [0, 0, 0, 0, 0]

distance_to_nearest_vowel("babbb") ➞ [1, 0, 1, 2, 3]

distance_to_nearest_vowel("abcdabcd") ➞ [0, 1, 2, 1, 0, 1, 2, 3]

distance_to_nearest_vowel("shopper") ➞ [2, 1, 0, 1, 1, 0, 1]

Notes

All input strings will contain at least one vowel.
Strings will be lowercased.
Vowels are: a, e, i, o, u.

=end
$vowels = { a: 0, e: 0, i: 0, o: 0, u: 0 }

def calculation(array, index)
left_count = 0
right_count = 0
for i in 0..index
break if $vowels[array[index - i].to_sym]
left_count += 1
end
for j in index..array.length-1
break if $vowels[array[j].to_sym]
right_count += 1
end

return left_count if index == array.length - 1
return right_count if left_count >= right_count || index == 0
left_count
end

def distance_to_nearest_vowel(string)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you try optimise the algorithm? if the word has 5 letters, then for each letter, you are doing two loops in the calculation method. So that's 5 * 2 = 10 times that you go through the whole input array. Could you try and reduce that to improve performance?

Step 1 is done. Getting it to work.
Step 2 - optimising performance.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try optimizing the performance but didn't work out. Can you suggest me a way i could optimize the performance. so i will get an idea how to approach in getting a good performance and i can work on it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this algorithm:

Loop from the left to right, as you go along mark the difference from left vowels.
The do the same from right to left, but only replace if the number is lower than the one computed already.

So in the end there will only be two loops then.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically I'm looping from left to right (0 to target element ) and right to left (last element to target). but the only thing is i have created a function that will loop and return the closest value and then that value is stored into an array called result. i have a main loop that just gives the index which is the target.

I tried your algorithm but still i do need a third loop to set as the marker.

result = []
array = string.chars
array.each_with_index do |letter, ind|
if !$vowels[letter.to_sym]
result << calculation(array, ind)
else
result << $vowels[letter.to_sym]
end
end
result
end

print distance_to_nearest_vowel("aaaaa")
puts " "
print distance_to_nearest_vowel("abcdabcd")
puts " "
print distance_to_nearest_vowel("babbb")
puts " "
print distance_to_nearest_vowel("shopper")
puts " "