Know python an Java? Then help me ! :P

Status
Not open for further replies.

Luke

Golden Master
Messages
5,605
Location
Vancouver, Canada
I need something translated from python into java. Be great if someone could help me out.

N = 20
hc = []
for i in range( N ):
hc.append( [] )
for j in range( N ):
hc.append( '-' )
def main():
i = N/2
j = N/2
num = 1
hc[j] = num
num += 1
directions1 = ['S', 'NW', 'N', 'NE', 'SE', 'S' ]
print "%3d:"% 1, '-'.join( directions1 )
i,j, num = fillArray( hc, i, j, directions1, num )
directions2 = ['S', 'W', 'NW', 'N', 'N', 'NE', 'E', 'SE','S', 'S' ]
print "%3d:"% 1, '-'.join( directions2 )
i, j, num = fillArray( hc, i, j, directions2, num )
for row in range( 3, 9 ):
newdirections = []
for n, dir in enumerate( directions2 ):
if n in [ 1, 3, 6, 8 ]:
newdirections += [dir] * (row-1)
else:
newdirections.append( dir )

i, j, num = fillArray( hc, i, j, newdirections, num )


displayArray( hc )


while True:
line = raw_input( "Enter 2 integers: " )
if len( line ) <= 1: return
i1 = int( line.split( )[0] )
i2 = int( line.split( )[1] )
i1, j1 = search( hc, i1 )
i2, j2 = search( hc, i2 )
diag = min( abs(i1-i2), abs(j1-j2) )
straight = max( abs(i1-i2), abs(j1-j2) )-diag
print "distance = ", diag + straight/2



def search( hc, n ):
global N
for i in range( N ):
for j in range( N ):
if hc[j]==n:
return i, j
return None, None


def fillArray( hc, i, j, directions, num ):
for dir in directions:
if dir=='S': i += 2
if dir=='N': i -= 2
if dir=='W': j -= 2
if dir=='E': j += 2
if dir=='NW':
j -=1
i -=1
if dir=='NE':
j +=1
i -=1
if dir=='SE':
j +=1
i +=1
if dir=='SW':
j -=1
i +=1
hc[j] = num
num += 1
return j, i, num

def displayArray( hc ):
global N
for i in range( N ):
for j in range( N ):
print "%3s" % str( hc[j] ),
print
print


main()
 
Status
Not open for further replies.
Back
Top Bottom