From e247a4082c08b27b3e9bcd42f631e61bf07125bb Mon Sep 17 00:00:00 2001 From: Anishluke92 Date: Mon, 25 Jan 2021 22:46:55 +0000 Subject: [PATCH 1/3] Distance_to_nearest_vowel.rb --- Distance_to_nearest_vowel.rb | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Distance_to_nearest_vowel.rb diff --git a/Distance_to_nearest_vowel.rb b/Distance_to_nearest_vowel.rb new file mode 100644 index 0000000..d6bd227 --- /dev/null +++ b/Distance_to_nearest_vowel.rb @@ -0,0 +1,63 @@ +=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 distance_calculation(array, ind) + right_count = 0 + left_count= 0 + (0..ind).each do |i| + right_count = array.index(array[ind - i]) + 1 if $vowels[array[ind - i].to_sym] + end + (ind..array.length - 1).each do |i| + left_count = array.index(array[i]) + 1 if $vowels[array[i].to_sym] + end + + + + + + +end + +def distance_to_nearest_vowel(string) + result = [] + # vowels = { a: 0, e: 0, i: 0, o: 0, u: 0 } + array = string.chars + array.each_with_index do |letter, ind| + if !$vowels[letter.to_sym] + result << distance_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 " " From e4c038cb4efb8297794929b60bdcb2586691ef92 Mon Sep 17 00:00:00 2001 From: Anishluke92 Date: Fri, 29 Jan 2021 18:41:46 +0000 Subject: [PATCH 2/3] Distance_to_nearest_vowel.rb --- Distance_to_nearest_vowel.rb | 63 ++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/Distance_to_nearest_vowel.rb b/Distance_to_nearest_vowel.rb index d6bd227..c2c8bf4 100644 --- a/Distance_to_nearest_vowel.rb +++ b/Distance_to_nearest_vowel.rb @@ -20,44 +20,43 @@ Vowels are: a, e, i, o, u. =end -$vowels = { a: 0, e: 0, i: 0, o: 0, u: 0 } - -def distance_calculation(array, ind) - right_count = 0 - left_count= 0 - (0..ind).each do |i| - right_count = array.index(array[ind - i]) + 1 if $vowels[array[ind - i].to_sym] - end - (ind..array.length - 1).each do |i| - left_count = array.index(array[i]) + 1 if $vowels[array[i].to_sym] - end - - - - - - -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 = [] - # vowels = { a: 0, e: 0, i: 0, o: 0, u: 0 } - array = string.chars - array.each_with_index do |letter, ind| - if !$vowels[letter.to_sym] - result << distance_calculation(array, ind) - else - result << $vowels[letter.to_sym] - end - end - result + 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") +print distance_to_nearest_vowel("aaaaa") puts " " print distance_to_nearest_vowel("abcdabcd") puts " " -#print distance_to_nearest_vowel("babbb") +print distance_to_nearest_vowel("babbb") puts " " -#print distance_to_nearest_vowel("shopper") +print distance_to_nearest_vowel("shopper") puts " " From 05ae31318549d5188668b703edac170c6b5f181c Mon Sep 17 00:00:00 2001 From: Anishluke92 Date: Fri, 29 Jan 2021 18:43:52 +0000 Subject: [PATCH 3/3] Modified the code - Distance to nearest vowel --- Distance_to_nearest_vowel.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Distance_to_nearest_vowel.rb b/Distance_to_nearest_vowel.rb index c2c8bf4..7f942b3 100644 --- a/Distance_to_nearest_vowel.rb +++ b/Distance_to_nearest_vowel.rb @@ -43,11 +43,11 @@ 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 + if !$vowels[letter.to_sym] + result << calculation(array, ind) + else + result << $vowels[letter.to_sym] + end end result end