Created
June 20, 2013 03:47
-
-
Save mythicalprogrammer/5820176 to your computer and use it in GitHub Desktop.
Interview Question
1. given three parameters, first one the string, 2nd one is the letter where the sorted string will start, 3rd the index within the given sorted string
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
str_parse = gets.chomp | |
s_char = gets.chomp | |
f_char = gets.chomp | |
alpha = "abcdefghijklmnopqrstuvwxyz".chars | |
iter = alpha.find_index(s_char) | |
while iter >= 0 | |
substr_arg1 = str_parse.chars.find_index(alpha[iter]) | |
if (substr_arg1.nil?) | |
iter = iter-1 | |
else | |
break | |
end | |
end | |
if substr_arg1.nil? | |
while iter < 26 | |
substr_arg1 = str_parse.chars.find_index(alpha[iter]) | |
if (substr_arg1.nil?) | |
iter = iter+1 | |
else | |
break | |
end | |
end | |
end | |
if (!substr_arg1.nil?) | |
str_sorted = str_parse.chars.sort_by { |x| x.downcase}.join | |
a = str_sorted.chars[0..(substr_arg1-1)].join | |
b =str_sorted.chars[substr_arg1..str_sorted.length].join | |
str_sorted = b+a | |
else | |
str_sorted = str_parse.chars.sort_by { |x| x.downcase}.join | |
end | |
puts str_sorted[f_char.to_i] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment