RB
rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# simple version (for verification)
def verify(pos, target, shufflecmd)
puts "verify"
shufflecmd.each{ |idx|
pos[idx-1], pos[-idx] = pos[-idx], pos[idx-1]
p pos
}
ans = pos.index(target) + 1
puts "Cup no. #{target} at position #{ans}"
end
# my try. (counting the swap)
def mysearch(pos, target, shuffle)
puts "#{pos} | #{target} | #{shuffle}"
idx = pos.index(target) + 1
cor = pos.length - idx + 1
if idx == cor
puts "Cup no. #{target} at position #{idx}"
else
num = shuffle.count {|x| x == idx or x == cor}
if num.odd?
ans = cor
else
ans = idx
end
puts "Cup no. #{target} at position #{ans}"
end
end
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run