Last active
August 29, 2015 13:56
-
-
Save ygpark2/9228235 to your computer and use it in GitHub Desktop.
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
MOVE | |
MOVE | |
MOVE | |
RIGHT | |
MOVE | |
REPORT | |
RIGHT | |
MOVE | |
RIGHT | |
MOVE | |
MOVE | |
LEFT | |
MOVE | |
MOVE | |
REPORT | |
PLACE 0,0,NORTH | |
MOVE | |
REPORT | |
PLACE 0,0,NORTH | |
LEFT | |
REPORT | |
PLACE 1,2,EAST | |
MOVE | |
MOVE | |
LEFT | |
MOVE | |
REPORT | |
PLACE 4,2,SOUTH | |
MOVE | |
MOVE | |
LEFT | |
MOVE | |
RIGHT | |
MOVE | |
MOVE | |
REPORT | |
PLACE 3,2,WEST | |
MOVE | |
MOVE | |
LEFT | |
MOVE | |
RIGHT | |
MOVE | |
LEFT | |
MOVE | |
REPORT | |
PLACE 3,3,NORTH | |
MOVE | |
RIGHT | |
MOVE | |
RIGHT | |
MOVE | |
RIGHT | |
MOVE | |
REPORT | |
PLACE 2,1,NORTH | |
MOVE | |
MOVE | |
MOVE | |
RIGHT | |
MOVE | |
RIGHT | |
MOVE | |
RIGHT | |
MOVE | |
MOVE | |
LEFT | |
MOVE | |
MOVE | |
REPORT |
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
#!/usr/bin/python | |
import sys, getopt | |
class Robot: | |
direction_map = {"NORTH": {"LEFT" : "WEST", "RIGHT": "EAST"}, | |
"SOUTH": {"LEFT" : "EAST", "RIGHT": "WEST"}, | |
"EAST": {"LEFT" : "NORTH", "RIGHT": "SOUTH"}, | |
"WEST": {"LEFT" : "SOUTH", "RIGHT": "NORTH"}} | |
def __init__(self, posX, posY, direction): | |
self.x = posX | |
self.y = posY | |
self.d = direction | |
def right(self): | |
self.d = self.direction_map[self.d]["RIGHT"] | |
def left(self): | |
self.d = self.direction_map[self.d]["LEFT"] | |
def move(self): | |
if self.x <= 0 and self.d == "WEST": | |
pass | |
elif self.x >= 4 and self.d == "EAST": | |
pass | |
elif 0 < self.x <= 4 and self.d == "WEST": | |
self.x -= 1 | |
elif 0 <= self.x < 4 and self.d == "EAST": | |
self.x += 1 | |
elif self.y <= 0 and self.d == "SOUTH": | |
pass | |
elif self.y >= 4 and self.d == "NORTH": | |
pass | |
elif 0 < self.y <= 4 and self.d == "SOUTH": | |
self.y -= 1 | |
elif 0 <= self.y < 4 and self.d == "NORTH": | |
self.y += 1 | |
else: | |
print("This is unexpected movement! error !") | |
def report(self): | |
print "Output : %d, %d %s" % (self.x, self.y, self.d) | |
def main(argv): | |
try: | |
opts, args = getopt.getopt(argv,"hcf:",["file="]) | |
except getopt.GetoptError: | |
print 'robot.py -f <inputfile> or robot.py -c' | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print 'test.py -f <inputfile> or robot.py -c' | |
sys.exit() | |
elif opt in ("-f", "--file"): | |
toy = None | |
lines = [line.strip().upper() for line in open(arg) if line.strip().upper() is not ""] | |
for line in lines: | |
cmds = map(lambda s: s.strip().upper(), line.split(" ")) # this is for command | |
if cmds[0] == "PLACE": | |
params = map(lambda s: s.strip(), cmds[1].split(",")) # this is for parameters | |
toy = Robot(int(params[0]), int(params[1]), params[2]) | |
else: | |
if toy is not None: | |
getattr(toy, cmds[0].lower())() | |
elif opt in ("-c", "--command"): | |
input_msg = "Place Command? (ex 'PLACE 0,0,NORTH'): " | |
cmd_str = raw_input(input_msg) | |
cmds = map(lambda s: s.strip().upper(), cmd_str.split(" ")) # this is for command | |
toy = None | |
if cmds[0] == "PLACE": | |
params = map(lambda s: s.strip(), cmds[1].split(",")) # this is for parameters | |
toy = Robot(int(params[0]), int(params[1]), params[2]) | |
while cmds[0] in ['PLACE', 'MOVE', 'LEFT', 'RIGHT', 'REPORT']: | |
cmd_str = raw_input("Command? (ex 'PLACE 0,0,NORTH', 'MOVE', 'LEFT', 'RIGHT', 'REPORT', 'QUIT to exit'): ") | |
cmds = map(lambda s: s.strip().upper(), cmd_str.split(" ")) # this is for command | |
if cmds[0] == "PLACE": | |
params = map(lambda s: s.strip(), cmds[1].split(",")) # this is for parameters | |
toy = Robot(int(params[0]), int(params[1]), params[2]) | |
elif cmds[0] == "QUIT": | |
break | |
else: | |
if toy is not None: | |
getattr(toy, cmds[0].lower())() | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
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
require 'optparse' | |
options = {} | |
opt_parser = OptionParser.new do |opt| | |
opt.banner = "Usage: opt_parser OPTIONS" | |
opt.separator "" | |
opt.separator "Options" | |
opt.separator " -f filename" | |
opt.separator " -c " | |
opt.separator "" | |
opt.on("-f","--file filename","which test data file you want to input") do |filename| | |
options[:file] = filename | |
end | |
opt.on("-c","--command","runing on standard input mode?") do | |
options[:command] = true | |
end | |
opt.on("-h","--help","help") do | |
puts opt_parser | |
end | |
end | |
opt_parser.parse! | |
class Robot | |
@x, @y, @d, @direction_map = 0, 0, "NORTH", {} | |
def initialize(posX, posY, direction) | |
@x = posX | |
@y = posY | |
@d = direction | |
@direction_map = {"NORTH" => {"LEFT" => "WEST", "RIGHT" => "EAST"}, | |
"SOUTH" => {"LEFT" => "EAST", "RIGHT" => "WEST"}, | |
"EAST" => {"LEFT" => "NORTH", "RIGHT" => "SOUTH"}, | |
"WEST" => {"LEFT" => "SOUTH", "RIGHT" => "NORTH"}} | |
end | |
def right | |
@d = @direction_map[@d]["RIGHT"] | |
end | |
def left | |
@d = @direction_map[@d]["LEFT"] | |
end | |
def move | |
case | |
when (@x <= 0 and @d == "WEST") | |
# pass | |
when (@x >= 4 and @d == "EAST") | |
# pass | |
when ((1..4).include?(@x) and @d == "WEST") | |
@x -= 1 | |
when ((0..3).include?(@x) and @d == "EAST") | |
@x += 1 | |
when (@y <= 0 and @d == "SOUTH") | |
# pass | |
when (@y >= 4 and @d == "NORTH") | |
# pass | |
when ((1..4).include?(@y) and @d == "SOUTH") | |
@y -= 1 | |
when ((0..3).include?(@y) and @d == "NORTH") | |
@y += 1 | |
else | |
print("This is unexpected movement! error !") | |
end | |
end | |
def report | |
printf( "Output : %d, %d, %s \n", @x, @y, @d ) | |
end | |
end | |
if options.has_key?(:command) | |
print "Place Command? (ex 'PLACE 0,0,NORTH'): " | |
toy = nil | |
while line = gets.chomp | |
cmds = line.split.collect{ |x| x.strip.upcase } | |
if (cmds[0] == "QUIT") | |
break | |
elsif (cmds[0] == "PLACE") | |
params = cmds[1].split(",").collect{ |x| x.strip.upcase } # this is for parameters | |
toy = Robot.new(params[0].to_i, params[1].to_i, params[2]) | |
else | |
method_name = cmds[0].downcase | |
if (["move", "left", "right", "report"].include?(method_name)) | |
toy.method(method_name.to_sym).call unless toy.nil? | |
else | |
print "Unknown command : #{method_name} so will be ignored! \n" | |
end | |
end | |
print "Command? (ex 'PLACE 0,0,NORTH', 'MOVE', 'LEFT', 'RIGHT', 'REPORT', 'QUIT to exit'): " | |
end | |
elsif options.has_key?(:file) | |
toy = nil | |
File.readlines(options[:file]).each do |line| | |
unless line.strip.empty? | |
cmds = line.split.collect{ |x| x.strip.upcase } | |
if (cmds[0] == "PLACE") | |
params = cmds[1].split(",").collect{ |x| x.strip.upcase } # this is for parameters | |
toy = Robot.new(params[0].to_i, params[1].to_i, params[2]) | |
else | |
method_name = cmds[0].downcase | |
if (["move", "left", "right", "report"].include?(method_name)) | |
toy.method(method_name.to_sym).call unless toy.nil? | |
else | |
print "Unknown command : #{method_name} so will be ignored! \n" | |
end | |
end | |
end | |
end | |
else | |
puts opt_parser | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment