Last active
August 29, 2015 14:23
-
-
Save quintessence/2ca4f5a7e33dfade4a28 to your computer and use it in GitHub Desktop.
Coding Game - Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, math | |
# Auto-generated code below aims at helping you parse | |
# the standard input according to the problem statement. | |
# Write an action using print | |
# To debug: print >> sys.stderr, "Debug messages..." | |
# game loop | |
while 1: | |
SX, SY = [int(i) for i in raw_input().split()] | |
maxHeight = 0 | |
posMaxHeightX = 0 | |
for i in xrange(8): | |
MH = int(raw_input()) # represents the height of one mountain, from 9 to 0. Mountain heights are provided from left to right. | |
if MH > maxHeight: | |
maxHeight = MH | |
posMaxHeightX = i | |
if SX == posMaxHeightX: | |
print "FIRE" | |
else: | |
print "HOLD" | |
# Explanation: | |
# The ship needs to only fire when it is over an appropriate mountain. The FOR loop is the X position (verfiy by printing). | |
# The maxHeight is the height of the highest mountain. posMaxHeightX is the X position of that mountain. | |
# These are reset to zero every time the game goes through the while loop. | |
# WITHIN the FOR loop, the mountain height is obtained for the current position (remember, i is the X current X position) and stored in MH | |
# The minimum value of MH is zero, which is no mountain. | |
# For every i in the range (0..7) the height of the mountain is checked. If that height is greater than the previous "greatest height" | |
# then the value for maxHeight is updated. | |
# After exiting the FOR loop, the FIRE command is only given to the tallest mountain. | |
## Previous solution involved firing over every mountain. This caused issues with multiple mountains as the ship seemed to only fire at | |
## the first mountain it encountered from whatever its current incoming direction was (R -> L or L -> R). Then it would crash into | |
## a different mountain. This also caused problems with recharge as the ship will only fire ONCE per transit, so if the ship could not yet | |
## fire while passing over a taller mountain, it could then crash into that mountain on the next pass. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, math | |
# Auto-generated code below aims at helping you parse | |
# the standard input according to the problem statement. | |
road = int(raw_input()) # the length of the road before the gap. | |
gap = int(raw_input()) # the length of the gap. | |
platform = int(raw_input()) # the length of the landing platform. | |
# game loop | |
while 1: | |
speed = int(raw_input()) # the motorbike's speed. | |
coordX = int(raw_input()) # the position on the road of the motorbike. | |
# Write an action using print | |
# To debug: print >> sys.stderr, "Debug messages..." | |
# sys.stderr.write("coordX: %i, road: %i, platform: %i, gap: %i\n" % (coordX, road, platform, gap)) | |
if coordX < road and speed < gap+1: | |
print "SPEED" | |
elif coordX < road and speed > gap+1: | |
print "SLOW" | |
elif coordX < road-speed: | |
print "WAIT" | |
elif coordX == road-1: | |
print "JUMP" | |
elif coordX < platform: | |
print "WAIT" | |
elif coordX > platform: | |
print "SLOW" | |
# Road is length of road before the gap, gap is length of gap to jump, and platform is the length of road after the gap. | |
# In order to successfully complete the jump, the speed must be of the value gap+1. So if the size of the gap is 3, the speed | |
# that the bike must be going to jump across successfully is 4. | |
# The jump must start on the last position before the gap, or else the jump will start in the gap because coordX starts at 0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, math | |
# Auto-generated code below aims at helping you parse | |
# the standard input according to the problem statement. | |
surfaceN = int(raw_input()) # the number of points used to draw the surface of Mars. | |
for i in xrange(surfaceN): | |
# landX: X coordinate of a surface point. (0 to 6999) | |
# landY: Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars. | |
landX, landY = [int(j) for j in raw_input().split()] | |
# game loop | |
while 1: | |
# hSpeed: the horizontal speed (in m/s), can be negative. | |
# vSpeed: the vertical speed (in m/s), can be negative. | |
# fuel: the quantity of remaining fuel in liters. | |
# rotate: the rotation angle in degrees (-90 to 90). | |
# power: the thrust power (0 to 4). | |
X, Y, hSpeed, vSpeed, fuel, rotate, power = [int(i) for i in raw_input().split()] | |
# Write an action using print | |
# To debug: print >> sys.stderr, "Debug messages..." | |
# Must print "R T" | |
# Where "R" is the rotation angle and "T" is the thrust power | |
if vSpeed <= -40: | |
# Slow, thrust power 4 | |
print "0 4" | |
elif vSpeed > -40: | |
# Nada, output thrust power of 0 | |
print "0 0" | |
# They give you the answer at the bottom of the problem... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, math | |
# Auto-generated code below aims at helping you parse | |
# the standard input according to the problem statement. | |
N = int(raw_input()) # the number of temperatures to analyse | |
TEMPS = raw_input() # the N temperatures expressed as integers ranging from -273 to 5526 | |
# Write an action using print | |
# To debug: print >> sys.stderr, "Debug messages..." | |
sys.stderr.write("N: %i, TEMPS: %s\n" % (N, TEMPS)) | |
closest = 0 | |
if N < 1: | |
print closest | |
else: | |
for t in TEMPS.split(): | |
#sys.stderr.write("t: %i\n" % int(t)) | |
sys.stderr.write("abs(t): %d, closest: %i\n" % (abs(int(t)), closest)) | |
if abs(int(t)) < abs(closest) or closest == 0: | |
closest = int(t) | |
if abs(int(t)) == abs(closest) and int(t) > closest: | |
closest = int(t) | |
print closest | |
# The test cases provided are not all the test cases needed to achieve a "perfect score". Full list: | |
# * Result is correct with a simple data set: {7 5 9 1 4} (should return 1) | |
# * It works with -273 alone (minimum bound) | |
# * It works with 5526 alone (upper bound) | |
# * It works when inputs contains only negative numbers: : {-15 -7 -9 -14 -12} -> -7 | |
# * When two temperatures are as close to 0, then the positive wins: {15 -7 9 14 7 12} -> 7 | |
# * When two temperatures are as close to 0, then the positive wins (2nd, similar, case): {15 7 9 14 -7 12} -> 7 | |
# * It works with two negative temperatures that are equal: {-10 -10} -> -10 | |
# * The solution displays 0 if no temperature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, math | |
# Auto-generated code below aims at helping you parse | |
# the standard input according to the problem statement. | |
## Handy little function I found that easily enumerates the letters | |
## AND can be used with maps. | |
from string import letters | |
def rank(x, d = dict((letr,n%26+1) for n,letr in enumerate(letters[0:52]))): | |
try: | |
return d[x] | |
except KeyError: | |
return 27 | |
L = int(raw_input()) | |
H = int(raw_input()) | |
T = raw_input() | |
T_numeric = map(rank, T) | |
for i in xrange(H): | |
ROW = raw_input() | |
for j in T_numeric: | |
sys.stdout.write(ROW[L*(j-1):L*j]) | |
sys.stdout.write("\n") | |
## Had to use sys.stdout.write instead of just "print" or it would print a newline | |
## for each row of each letter rather than printing across. Using sys.stdout.write | |
## allowed me to print the newline only in the outer FOR loop. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, math | |
# Auto-generated code below aims at helping you parse | |
# the standard input according to the problem statement. | |
MESSAGE = raw_input() | |
# Write an action using print | |
# To debug: print >> sys.stderr, "Debug messages..." | |
ascii = '' | |
## This was shorter without the 'a', but apparently the game requires | |
## the length of the binary string to be 7 per this requirement: | |
## -> The input message consists of ASCII characters (7-bit) | |
## Barring this constraint, I prefer: | |
#### for letr in MESSAGE: | |
#### ascii += bin(ord(letr))[2:] | |
for letr in MESSAGE: | |
a = bin(ord(letr))[2:] | |
while len(a) < 7: | |
a = '0'+a | |
ascii += a | |
norris = '' | |
i = 0 | |
while (i < len(ascii)): | |
if ascii[i] == "0": | |
norris+="00 0" | |
elif ascii[i] == "1": | |
norris+="0 0" | |
while (i < len(ascii)-1 and ascii[i] == ascii[i+1]): | |
norris+="0" | |
i+=1 | |
norris+=" " | |
i+=1 | |
print norris[:len(norris)-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment