Python Golfing Suggestions

-2

I have read: Tips for golfing in Python

I would like to golf this code further. Any more tips?

x,y,q,w=[int(i) for i in raw_input().split()]
while 1:
 a=int(input())
 s=d=""
 if q>x:
  s="W"
  q-=1
 elif q<x:
  s="E"
  q+=1
 if w>y:
  d="N"
  w-=1
 elif w<y:
  d="S"
  w+=1
 print d+s

The program: You move on a map which is 40 wide by 18 high. Thor is initially placed on a random spot on the map and must reach the light of power as quickly as possible. You need to have the shortest code possible.

During each turn, you must specify in which direction to move, from one of the following:

N (North) NE (North-East) E (East) SE (South-East) S (South) SW (South-West) W (West) NW (North-West)

YOU WIN WHEN THOR REACHES THE LIGHT OF POWER.

YOU LOSE: if Thor moves off the map. if Thor doesn't have enough energy left to reach the light of power.

EXAMPLE: THOR STARTS ON THE MAP ON THE SQUARE (3, 6). THE LIGHT OF POWER IS ON SQUARE (3, 8).

TURN 1 THOR MOVES SOUTHWARDS. NEW POSITION = (3, 7). THOR HAS ENOUGH ENERGY LEFT FOR 10 MOVES

TURN 2 THOR MOVES SOUTHWARDS. NEW POSITION = (3, 8). THOR HAS ENOUGH ENERGY LEFT FOR 9 MOVES

The program must first read the initialization data from standard input. Then, within an infinite loop, read the data from the standard input related to Thor's current state and provide to the standard output Thor's movement instructions.

INITIALIZATION INPUT: Line 1: 4 integers indicate the position of the light of power. 2 indicate Thor’s starting position.

INPUT FOR ONE GAME TURN: Line 1: The level of Thor’s remaining energy. This represents the number of moves he can still make.

OUTPUT FOR ONE GAME TURN: A single line providing the move to be made: N NE E SE S SW W or NW

Edit : it's a code golf on codingame.com

marsh

Posted 2015-10-03T04:44:58.707

Reputation: 199

Question was closed 2015-10-03T15:51:46.407

8To give you golfing tips, we need to know what your code is supposed to achieve. For example, a=int(input()) strikes me as odd, since you don't use a anywhere. – Dennis – 2015-10-03T04:56:03.637

It is the remaining energy to see if I can move or not, I guess I never run out of energy in the simple test. – marsh – 2015-10-03T05:18:08.607

The description hardly matches the code. – feersum – 2015-10-03T05:19:08.250

2x,y,q,w=map(int,raw_input().split()) saves 9 characters – TessellatingHeckler – 2015-10-03T07:11:04.840

Answers

3

N.B.: This may not be entirely correct as I don't know what your code is supposed to do

The first thing to do is remove unnecessary whitespace, especially that after the if statements:

x,y,q,w=[int(i) for i in raw_input().split()]
while 1:
 a=int(input())
 s=d=""
 if q>x:s="W";q-=1
 elif q<x:s="E";q+=1
 if w>y:d="N";w-=1
 elif w<y:d="S";w+=1
 print d+s 

This golfs your code down by 4 bytes.

What would be ideal for this challenge is a switch case which, unfortunaley, does not exist in Python. Instead we can be clever with an array's index:

x,y,q,w=[int(i) for i in raw_input().split()]
while 1:
 a=int(input())
 s=d=""
 exec(['s="W";q-=1','s="E";q+=1'][q<x])
 exec(['d="N";w-=1','d="S";w+=1'][w<y])
 print d+s

Saving you x bytes.

Finally you can get rid of all whitespace now:

x,y,q,w=[int(i) for i in raw_input().split()]
while 1:a=int(input());s=d="";exec(['s="W";q-=1','s="E";q+=1'][q<x]);exec(['d="N";w-=1','d="S";w+=1'][w<y]);print d+s

Beta Decay

Posted 2015-10-03T04:44:58.707

Reputation: 21 478

2I think you have a bug where you don't allow q==x or w==y to do nothing; you always move Thor in a diagonal, never in a horizontal or vertical. – TessellatingHeckler – 2015-10-03T07:12:35.373

if q>x:s="W";q-=1, I though I tried this? Doesnt it need a tab character? – marsh – 2015-10-03T13:45:02.680

@TessellatingHeckler Yeah, that's a major glaw :P – Beta Decay – 2015-10-03T18:51:05.940

You did not get rid of the space in int(i) for... – Jonathan Frech – 2018-05-09T21:45:23.343

@Jonathan I can't remember exactly, but I think removing that space breaks the code for some reason – Beta Decay – 2018-05-10T12:13:49.570

@BetaDecay Well, seems odd. But in that case I get why it stood. – Jonathan Frech – 2018-05-10T14:02:16.197