-
Notifications
You must be signed in to change notification settings - Fork 1
Distance to nearest vowel #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Anishluke92
wants to merge
3
commits into
master
Choose a base branch
from
Distance_to_nearest_vowel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| 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 " " | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.