Hi, I was working on some USACO Bronze Problems in Python and realized that my solution was way too slow. How could I shorten my solution and still solve the problem at the same time?
Problem - http://www.usaco.org/index.php?page=viewproblem2&cpid=1107
My Solution -
https://drive.google.com/file/d/1LDu3qTI5iu7gG-zfgEE0fuzMXs65vpzm/view?usp=sharing
N = input()
N = int(N)
nums = []
Person1 = []
Person2 = []
Person3 = []
for i in range(0,N,1):
nums.append(input())
def Determine_Names(a):
Names = []
Name = ‘’
for i in range(0,len(a), 1):
if (a[i] == ’ '):
return Name.join(Names)
else:
Names.append(a[i])
def Determine_Year(a):
Years = []
Year = ‘’
x = 0
for i in range(0, len(a), 1):
if (a[i] == ’ '):
x = x+1
if (x == 4):
i = i+1
for i in range(i, len(a), 1):
if (a[i] == ’ '):
return Year.join(Years)
else:
Years.append(a[i])
def Determine_Other(a):
Names = []
Name = ‘’
for i in range(-1,-(len(a)),-1):
if (a[i] == ’ '):
Names.reverse()
return Name.join(Names)
else:
Names.append(a[i])
def Determine_Prefix(a):
Prefixes = []
Prefix = ‘’
x = 0
for i in range(0, len(a), 1):
if (a[i] == ’ '):
x = x+1
if (x == 3):
i = i+1
for i in range(i, len(a), 1):
if (a[i] == ’ '):
return Prefix.join(Prefixes)
else:
Prefixes.append(a[i])
def Years_Apart(a, b, c):
if (c == ‘previous’):
Years = a - b + 12
if (c == ‘next’):
Years = (b - a + 12)*-1
return Years
Person1.append(Determine_Names(nums[0]))
Person1.append(Determine_Prefix(nums[0]))
Person1.append(Determine_Other(nums[0]))
Person1.append(Determine_Year(nums[0]))
Person2.append(Determine_Names(nums[1]))
Person2.append(Determine_Prefix(nums[1]))
Person2.append(Determine_Other(nums[1]))
Person2.append(Determine_Year(nums[1]))
Person3.append(Determine_Names(nums[2]))
Person3.append(Determine_Prefix(nums[2]))
Person3.append(Determine_Other(nums[2]))
Person3.append(Determine_Year(nums[2]))
def Year_Number(a):
if (a == ‘Ox’):
return 1
if (a == ‘Dragon’):
return 4
if (a == ‘Tiger’):
return 2
if (a == ‘Rabbit’):
return 3
if (a == ‘Snake’):
return 5
if (a == ‘Horse’):
return 6
if (a == ‘Goat’):
return 7
if (a == ‘Monkey’):
return 8
if (a == ‘Rooster’):
return 9
if (a == ‘Dog’):
return 10
if (a == ‘Pig’):
return 11
if (a == ‘Rat’):
return 12
def Find_Year(a):
if (a == 1):
return ‘Ox’
if (a == 2):
return ‘Tiger’
if (a == 3):
return ‘Rabbit’
if (a == 4):
return ‘Dragon’
if (a == 5):
return ‘Snake’
if (a == 6):
return ‘Horse’
if (a == 7):
return ‘Goat’
if (a == 8):
return ‘Monkey’
if (a == 9):
return ‘Rooster’
if (a == 10):
return ‘Dog’
if (a == 11):
return ‘Pig’
if (a == 12):
return ‘Rat’
def Find_Birth(a,b,c,d):
if (b[0] == d):
return b[3]
if (c[0] == d):
return c[3]
if (a[0] == d):
return a[3]
Person1.append(Years_Apart(1, Year_Number(Person1[3]), Person1[1]))
Person2.append(Years_Apart(Year_Number(Find_Birth(Person1, Person2, Person3, Person2[2])), Year_Number(Person2[3]), Person2[1]))
Person3.append(Years_Apart(Year_Number(Find_Birth(Person1, Person2, Person3, Person3[2])), Year_Number(Person3[3]), Person3[1]))
def Find_People(a,b,c,d):
if (b[0] == d):
return [b[2], b[4]]
if (c[0] == d):
return [c[2], c[4]]
if (a[0] == d):
return [a[2], a[4]]
Sum = 0
for i in range(0, N, 1):
if (i == 0):
Place_Hldr = Find_People(Person1, Person2, Person3, ‘Elsie’)
Sum = Sum + int(Place_Hldr[1])
if (Place_Hldr[0] == ‘Bessie’):
print(Sum)
break
continue
Place_Hldr = Find_People(Person1, Person2, Person3, Place_Hldr[0])
Sum = Sum + int(Place_Hldr[1])
if (Place_Hldr[0] == ‘Bessie’):
print(Sum)
break