mirror of
https://github.com/Xevion/contest.git
synced 2025-12-10 06:06:47 -06:00
UIL move
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import os, sys
|
||||
|
||||
mappings = {
|
||||
'@' : ['at'],
|
||||
'&' : ['and'],
|
||||
'1' : ['one', 'won'],
|
||||
'2' : ['to', 'too', 'two'],
|
||||
'4' : ['for', 'four'],
|
||||
'b' : ['bea', 'be', 'bee'],
|
||||
'c' : ['sea', 'see'],
|
||||
'i' : ['eye'],
|
||||
'o' : ['oh', 'owe'],
|
||||
'r' : ['are'],
|
||||
'u' : ['you'],
|
||||
'y' : ['why']
|
||||
}
|
||||
|
||||
# gets the largest and smallest possible subs
|
||||
minSub = min([min(list(map(len, vals))) for vals in mappings.values()])
|
||||
maxSub = max([max(list(map(len, vals))) for vals in mappings.values()])
|
||||
|
||||
# Gets the token ('are' -> 'r') for a sub, case sensitive
|
||||
def getToken(sub):
|
||||
for key in mappings.keys():
|
||||
if sub in mappings[key]:
|
||||
return key
|
||||
|
||||
def processWord(word):
|
||||
processed = [False] * len(word)
|
||||
# For every character in the word
|
||||
index = 0
|
||||
while index < len(word):
|
||||
possible = []
|
||||
for length in range(minSub, maxSub + 1):
|
||||
if index + length > len(word):
|
||||
continue
|
||||
sub = word[index:index + length]
|
||||
token = getToken(sub.lower())
|
||||
if token:
|
||||
# Sometimes mulitple tokens can appear for the same one, we just select the one that is longest
|
||||
possible.append((token, len(sub), sub))
|
||||
# A token replacement has been found at this index
|
||||
if possible:
|
||||
# Find the best one, based on the sub
|
||||
select = max(possible, key=lambda item : item[1])
|
||||
# print(index, select, word[index:index + select[1]])
|
||||
word = word[:index] + (select[0].title() if select[2].istitle() else select[0]) + word[index + select[1]:]
|
||||
# word[index:index + select[1]] = select[0]
|
||||
index += len(select[0])
|
||||
else:
|
||||
index += 1
|
||||
return word
|
||||
|
||||
# Process a single line
|
||||
def processLine(line):
|
||||
return ' '.join([processWord(word) for word in line.split(' ')])
|
||||
|
||||
# Drive Code
|
||||
def main():
|
||||
path = os.path.join(sys.path[0], 'input')
|
||||
data = open(path, 'r').read().split('\n')[1:]
|
||||
print('\n'.join(map(processLine, data)))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,6 @@
|
||||
5
|
||||
H 2 0:13
|
||||
A 2 0:19
|
||||
H 1 0:52
|
||||
H 1 0:52
|
||||
A 3 1:08
|
||||
@@ -0,0 +1,2 @@
|
||||
1
|
||||
H 1 16:00
|
||||
@@ -1,65 +1,41 @@
|
||||
import os, sys
|
||||
import datetime, re, os, sys
|
||||
|
||||
mappings = {
|
||||
'@' : ['at'],
|
||||
'&' : ['and'],
|
||||
'1' : ['one', 'won'],
|
||||
'2' : ['to', 'too', 'two'],
|
||||
'4' : ['for', 'four'],
|
||||
'b' : ['bea', 'be', 'bee'],
|
||||
'c' : ['sea', 'see'],
|
||||
'i' : ['eye'],
|
||||
'o' : ['oh', 'owe'],
|
||||
'r' : ['are'],
|
||||
'u' : ['you'],
|
||||
'y' : ['why']
|
||||
}
|
||||
pattern = r'([HA]) (\d) (\d+):(\d+)'
|
||||
mmsspattern = '%M:%S'
|
||||
|
||||
# gets the largest and smallest possible subs
|
||||
minSub = min([min(list(map(len, vals))) for vals in mappings.values()])
|
||||
maxSub = max([max(list(map(len, vals))) for vals in mappings.values()])
|
||||
# Process the input for a single niput
|
||||
def process(rawinput):
|
||||
def getBest():
|
||||
return max(score.items(), key=lambda x:x[1])[0]
|
||||
score = {'H' : 0, 'A' : 0}
|
||||
leads = {'H' : 0, 'A' : 0}
|
||||
rawinput = '\n'.join(rawinput.split('\n')[1:])
|
||||
events = re.finditer(pattern, rawinput)
|
||||
curLead = ''
|
||||
lastTime = datetime.datetime(1990, 1, 1, 0, 0, 0)
|
||||
gameEnd = datetime.datetime(1990, 1, 1, 1, 30)
|
||||
for event in events:
|
||||
# Create the formatted timecode, add the score
|
||||
timecode = '{}:{}'.format(event.group(3).zfill(2), event.group(4).zfill(2))
|
||||
curtime = datetime.datetime.strptime(timecode, mmsspattern)
|
||||
score[event.group(1)] += int(event.group(2))
|
||||
# Get the team in the lead and if it's different
|
||||
best = getBest()
|
||||
leads[best] += int((curtime - lastTime).seconds)
|
||||
lastTime = curtime
|
||||
# if gameEnd > lastTime:
|
||||
# leads[best] += (gameEnd - lastTime).seconds
|
||||
return '{} {} {}'.format(getBest(), leads['H'], leads['A'])
|
||||
|
||||
# Gets the token ('are' -> 'r') for a sub, case sensitive
|
||||
def getToken(sub):
|
||||
for key in mappings.keys():
|
||||
if sub in mappings[key]:
|
||||
return key
|
||||
|
||||
def processWord(word):
|
||||
processed = [False] * len(word)
|
||||
# For every character in the word
|
||||
index = 0
|
||||
while index < len(word):
|
||||
possible = []
|
||||
for length in range(minSub, maxSub + 1):
|
||||
if index + length > len(word):
|
||||
continue
|
||||
sub = word[index:index + length]
|
||||
token = getToken(sub.lower())
|
||||
if token:
|
||||
# Sometimes mulitple tokens can appear for the same one, we just select the one that is longest
|
||||
possible.append((token, len(sub), sub))
|
||||
# A token replacement has been found at this index
|
||||
if possible:
|
||||
# Find the best one, based on the sub
|
||||
select = max(possible, key=lambda item : item[1])
|
||||
# print(index, select, word[index:index + select[1]])
|
||||
word = word[:index] + (select[0].title() if select[2].istitle() else select[0]) + word[index + select[1]:]
|
||||
# word[index:index + select[1]] = select[0]
|
||||
index += len(select[0])
|
||||
else:
|
||||
index += 1
|
||||
return word
|
||||
|
||||
# Process a single line
|
||||
def processLine(line):
|
||||
return ' '.join([processWord(word) for word in line.split(' ')])
|
||||
|
||||
# Drive Code
|
||||
# Driver code
|
||||
def main():
|
||||
path = os.path.join(sys.path[0], 'input')
|
||||
data = open(path, 'r').read().split('\n')[1:]
|
||||
print('\n'.join(map(processLine, data)))
|
||||
# Read inputs
|
||||
inputs = [os.path.join(sys.path[0], 'inputs', x) for x in
|
||||
os.listdir(os.path.join(sys.path[0], 'inputs'))]
|
||||
inputs = [open(path).read() for path in inputs]
|
||||
for rawinput in inputs:
|
||||
print(process(rawinput))
|
||||
print('-'*19)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -4,25 +4,41 @@ def process(item):
|
||||
# Constants and initial values
|
||||
stocks, (n, m) = item[1], item[0]
|
||||
peaks, valleys = [], []
|
||||
possiblePeaks, possibleValleys = range(n, len(stocks) - n), range(m, len(stocks) - m)
|
||||
# Check all possible peaks
|
||||
for index in possiblePeaks:
|
||||
isPeak = True
|
||||
for index in range(len(stocks)):
|
||||
isPeak, isValley = True, True
|
||||
# Peak Checking
|
||||
for i in range(1, n + 1):
|
||||
if not stocks[index - i] < stocks[index] or not stocks[index] > stocks[index + i]:
|
||||
isPeak = False
|
||||
if isPeak:
|
||||
peaks.append(index)
|
||||
print(peaks)
|
||||
# Check all possible valleys
|
||||
for index in possibleValleys:
|
||||
isValley = True
|
||||
# If we're no the first index
|
||||
if index - i >= 0:
|
||||
# If position behind us is lower
|
||||
if not stocks[index] > stocks[index - i]:
|
||||
isPeak = False
|
||||
break
|
||||
# If we're not the final index
|
||||
if index + i < len(stocks):
|
||||
# and position in front of us is lower
|
||||
if not stocks[index] > stocks[index + i]:
|
||||
isPeak = False
|
||||
break
|
||||
# Valley checking
|
||||
for i in range(1, m + 1):
|
||||
if not stocks[index - i] > stocks[index] or not stocks[index] < stocks[index + i]:
|
||||
isValley = False
|
||||
if isValley:
|
||||
valleys.append(index)
|
||||
print(valleys)
|
||||
# If we're not the first index
|
||||
if index - i >= 0:
|
||||
# if position behind us is higher
|
||||
if not stocks[index] < stocks[index - i]:
|
||||
isValley = False
|
||||
break
|
||||
# If we're not the final index
|
||||
if index + i < len(stocks):
|
||||
# and position in front of us is higher
|
||||
if not stocks[index] < stocks[index + i]:
|
||||
isValley = False
|
||||
break
|
||||
if isPeak: peaks.append(index)
|
||||
if isValley: valleys.append(index)
|
||||
print(f'n{n}, m{m}')
|
||||
print('{0}{1}\n{0}{2}'.format('-> ', peaks, valleys))
|
||||
return len(peaks), len(valleys)
|
||||
|
||||
# Driver code for all inputs in folder
|
||||
|
||||
@@ -8,6 +8,14 @@ Most of these problems seem modestly easy, if I'm being honest.
|
||||
|
||||
## Progress
|
||||
|
||||
#1 : Unsovled
|
||||
#A : **Solved**
|
||||
|
||||
#2 : **Solved**
|
||||
#B : **Solved**
|
||||
|
||||
#C : Unsolved
|
||||
|
||||
#D : **Solved**
|
||||
|
||||
#E : **Solved**
|
||||
|
||||
#F : **Solved**
|
||||
Reference in New Issue
Block a user