Created
January 24, 2015 13:03
-
-
Save rtcoms/0ff4c71853dd298d2f61 to your computer and use it in GitHub Desktop.
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
def find_next_largest arr | |
base = arr[0] | |
new_base = arr.map{|x| x if x > base}.compact.sort.first | |
new_array = [new_base] | |
no_of_times_base_value_present = arr.select{|x| x == new_base}.compact.count | |
rest_array = arr - [new_base] + (new_base.to_s*(no_of_times_base_value_present-1)).split("").map{|x| x.to_i} | |
rest_array = rest_array.sort | |
new_array << rest_array | |
new_array.flatten | |
end | |
num = 134466530 | |
puts "input : #{num}" | |
numbers_array = num.to_s.split('').map{|x| x.to_i} | |
reverse_array = numbers_array.reverse | |
puts "arr : #{numbers_array}" | |
reverse_array.reverse.each_with_index do |x, i | | |
sub_string = reverse_array[0, i].reverse | |
next if sub_string == sub_string.sort.reverse | |
string_to_replace = sub_string.join('') | |
puts "STRING TO REPLACE" | |
puts string_to_replace | |
puts "==================" | |
new_string = find_next_largest(sub_string).join('') | |
puts "NEW STRING" | |
puts new_string | |
puts "==================" | |
puts "ANSWER : " | |
puts num.to_s.gsub(string_to_replace, new_string) | |
puts "--------------------" | |
break | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment