Skip to content

Instantly share code, notes, and snippets.

@shtakai
Created May 6, 2016 20:27
Show Gist options
  • Save shtakai/9695b7f21c6242a3dd85c490c362482f to your computer and use it in GitHub Desktop.
Save shtakai/9695b7f21c6242a3dd85c490c362482f to your computer and use it in GitHub Desktop.
#WORK IN PROGRESS : fill water
require 'pp'
def shuffle(l)
return l[0] if l.size == 1
i = l.index(l.sample)
h = l[i]
l.delete_at(i)
return [h, shuffle(l)].flatten
end
def fill_water2(list)
#pp l
first = list.max
index_first= list.index(first)
tmp_l = (list[0...index_first] << list.min-1) + list[index_first...list.size-1]
pp tmp_l
second = tmp_l.max
index_second = tmp_l.index(second)
pp "first:#{first}:#{index_first}, second:#{second}:#{index_second}"
h,m,l = list[(0..(index_second-1))], list[((index_second-1)..index_first-1)], list[((index_first-1)..(list.size-1))] if index_first > index_second
pp h,m,l
end
array = [1,3,2,4,6,5]
#fill_water( shufle(array))
fill_water( array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment