Skip to content

Instantly share code, notes, and snippets.

@oieioi
Created July 10, 2015 03:28
Show Gist options
  • Save oieioi/cd21fd7610d1a67489ce to your computer and use it in GitHub Desktop.
Save oieioi/cd21fd7610d1a67489ce to your computer and use it in GitHub Desktop.
#raw_list = STDIN.gets.chomp.split("\n").collect {|item| item.to_i}
raw_list = []
while str = STDIN.gets#.chomp
break if str.empty?
raw_list.push str.to_i
end
count_changed = 0
raw_list.each_with_index do |item, index|
if item != (index + 1) then
item_index = raw_list.find_index(index + 1)
raw_list[item_index] = item
raw_list[index] = index + 1
count_changed += 1
end
end
p count_changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment