I have the following code on python:
f = open("configuration.txt", "r")
position = {}
counter = 0
with open("configuration.txt") as fileobj:
for line in fileobj:
for ch in line:
if ch.isdigit():
position[int(ch)] = counter
counter+=1
# print(dict(sorted(position.items())))
position = dict(sorted(position.items()))
diskPositions = list(position.values())
disk = {0:'4',1:'3',2:'2',3:'1'}
pegs = {0:'A',1:'B',2:'C'}
# print(position)
def moveDisks(diskPositions, largestToMove, targetPeg):
for badDisk in range(largestToMove, len(diskPositions)):
currentPeg = diskPositions[badDisk]
if diskPositions[badDisk] != targetPeg:
otherPeg = 3 - targetPeg - currentPeg
moveDisks(diskPositions, badDisk + 1, otherPeg)
print(disk[badDisk], "-", pegs[targetPeg])
diskPositions[badDisk] = targetPeg
moveDisks(diskPositions, badDisk + 1, targetPeg)
break;
moveDisks(diskPositions[::-1], 0, 2)
And I need to convert this code into ruby. I started doing it, but I'm stuck and don't know where is the error, or what I'm doing different.
When I runt it, i receive the following error on line 30, caused by a nil variable:
/hanoi/final.rb:30:in `-': nil can't be coerced into Integer (TypeError)```
Ruby Code :
f = open("configuration.txt", "r")
position = {}
counter = 0
def is_digit(look_ahead)
look_ahead =~ /[[:digit:]]/
end
File.open('configuration.txt', 'r') do |f|
f.each_line do |l| # iterate on each character
l.each_char do |c|
if is_digit(c)
position[c.to_i] = counter
end
end
counter+=1
end
end
position = position.sort.to_h
disk_positions = position.values
disk = {0 => '4', 1 => '3', 2 => '2', 3 => '1'}
pegs = {0 => 'A', 1 => 'B', 2 => 'C'}
def moveDisks(diskPositions, largestToMove, targetPeg)
for badDisk in (largestToMove..diskPositions.length)
currentPeg = diskPositions[badDisk]
if diskPositions[badDisk] != targetPeg
otherPeg = 3 - targetPeg - currentPeg
moveDisks(diskPositions, badDisk + 1, otherPeg)
print disk[badDisk].to_s + "-", pegs[targetPeg].to_s
diskPositions[badDisk] = targetPeg
moveDisks(diskPositions, badDisk + 1, targetPeg)
break
end
end
end
moveDisks(disk_positions.reverse, 0, 2)
PS: configuration.txt is a text file containing the following lines:
A-1
B-4:2
C-3
In Python, the built-in range
function is not inclusive, so it doesn't return the last number. Ruby doesn't use a function, it has a couple of operators - the two-dot form ..
creates a range including the last number, and the three-dot form ...
which excludes the high value. See the documentation for more information.
Your posted code has a few additional errors and isn't very idiomatic, but your error is being caused by using the inclusive form, which results in a nil value being returned when the index goes out of bounds. The range (largestToMove...diskPositions.length)
would be the equivalent for the Python function.