Ruby simple code(ruby is so damn readable)
pos[0]=x pos[1]=y
just pass the value u wanna convert to translate function, logic is
quite clean :)
step=10
print translate(step)
def move(pos,direction)
case direction
when :down
pos[1]+=1
when :left
pos[0]-=1
when :up
pos[1]-=1
when :right
pos[0]+=1
when :diagup
pos[0],pos[1]=pos[0]+1,pos[1]-1
when :diagdown
pos[0],pos[1]=pos[0]-1,pos[1]+1
end
end
def translate(steps)
guide=[:left,:up,:diagup,:right,:down,:down,:diagdown]
pos=[0,0]
move(pos,:down)
(steps-1).times{|i|
move(pos,guide[i%7])
}
return pos
end
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.