Skip to content

Instantly share code, notes, and snippets.

@ceshm
Created February 14, 2020 15:12
Show Gist options
  • Save ceshm/11212630dce1ffbe317ad3a3bd6a76f3 to your computer and use it in GitHub Desktop.
Save ceshm/11212630dce1ffbe317ad3a3bd6a76f3 to your computer and use it in GitHub Desktop.
# M x N matrix spiriling direction
# Tested on python 2.7.16 and 3.4.4
import sys
if sys.version_info >= (3, 0):
my_input = input # Python 3.x
else:
my_input = raw_input # Python 2.x
def validate_case_input(case_input):
try:
N, M = int(case_input[0]), int(case_input[1])
return True, N, M
except:
return False, None, None
def evaluate_case(N, M):
if M < N:
if M % 2 == 0:
return "U"
else:
return "D"
else:
if N % 2 == 0:
return "L"
else:
return "R"
while True:
try:
T = int(my_input("Number of test cases: "))
break
except:
print("Please enter an integer")
results = []
for i in range(T):
while True:
current_case = my_input("Case "+str(i+1)+": ").split(" ")
input_is_valid, N, M = validate_case_input(current_case)
if input_is_valid:
current_result = evaluate_case(N, M)
break
else:
print("Please input two integers separated by a space")
results.append(current_result)
for i, result in enumerate(results):
print("Result {0}: {1}".format(str(i+1),str(result)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment