Skip to content

Instantly share code, notes, and snippets.

@fernandojunior
Last active August 29, 2015 14:06
Show Gist options
  • Save fernandojunior/3e8abd7bb4098d702d92 to your computer and use it in GitHub Desktop.
Save fernandojunior/3e8abd7bb4098d702d92 to your computer and use it in GitHub Desktop.
Solution in Python for the first codingame trannig
# The code below will read all the game information for you.
# On each game turn, information will be available on the standard input, you will be sent:
# -> the total number of visible enemies
# -> for each enemy, its name and distance from you
# The system will wait for you to write an enemy name on the standard output.
# Once you have designated a target:
# -> the cannon will shoot
# -> the enemies will move
# -> new info will be available for you to read on the standard input.
"""
Solution in Python for the first codingame trannig/tutorial.
Game name: Onboarding
Topic: finding a minimal value
ide: 4283500be9f37066245417f51fb54d1f2ce348
@link www.codingame.com/
@author Fernando Felix do Nascimento Junior
"""
class Enemy:
"""
Class that defines an enemy
"""
def __init__(self, name, distance):
"""
Constructor
"""
# enemy name
self.name = name
# enemy distance from cannon
self.distance = distance
def __cmp__(self, other):
"""
Comparation mathod. Need to sorted function
"""
return self.distance.__cmp__(other.distance)
# game loop
while True:
count = int(raw_input()) # The number of current enemy ships within range
# enemy list within range
enemies = [];
for i in range(count):
# name: The name of this enemy
# dist: The distance to your cannon of this enemy
name, dist = raw_input().split() # reading an enemy
# creating and appending an enemy object
enemies.append(Enemy(name, int(dist)))
# sorting (based on distance) the list asc
enemies = sorted(enemies)
# printing the closest enemy
print enemies[0].name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment