Initial commit

This commit is contained in:
2018-01-29 16:14:37 -05:00
commit c9be15672c
72 changed files with 723 additions and 0 deletions

108
.gitignore vendored Normal file
View File

@@ -0,0 +1,108 @@
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
.static_storage/
.media/
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.idea/

40
haiku_sol.py Normal file
View File

@@ -0,0 +1,40 @@
import re
line = input().strip()
sylcounts = [5, 7, 5]
words = re.findall('[a-zA-Z]+', line)
originals = line.split()
final = ''
for word, original in zip(words, originals):
word = word.lower().replace('qu', 'q')
word = re.sub('y[aoeui]', 'b', word)
if re.match('.*e$', word) and not re.match('.*[^aoeuiy]le$', word):
word = word[:-1]
if re.match('.*es$', word) and not re.match('.*[^aoeuiy][^aoeuiy]es', word):
word = word[:-2]
word = re.sub('[aoeuiy]+', 'A', word)
syllables = word.count('A') or 1
sylcounts[0] -= syllables
final += original + ' '
if sylcounts[0] == 0:
del sylcounts[0]
final = final[:-1] + '\n'
continue
if sylcounts[0] < 0:
final = line
sylcounts = []
break
if sylcounts:
final = line
print(final.strip())

31
icpc_test.py Normal file
View File

@@ -0,0 +1,31 @@
import glob, sys, subprocess
_, src, test, t = sys.argv
ig = glob.glob(f'test/{test}/*.in')
og = glob.glob(f'test/{test}/*.ans')
print(test, len(ig), 'tests')
for ifn, ofn in zip(ig, og):
with open(ifn) as fi, open(ofn) as fx:
try:
o = subprocess.check_output(['python', src],
stdin=fi, stderr=subprocess.STDOUT,
timeout=float(t))
except subprocess.CalledProcessError as e:
o = e.output
except subprocess.TimeoutExpired:
o = b'TIMED OUT'
o = o.decode('utf-8').strip().replace('\r\n', '\n')
x = fx.read().strip().replace('\r\n', '\n')
if o != x:
print(ifn, 'expected:', x, 'got:', o, '=' * 50, sep='\n')
# run this with arguments:
# <script>.py <testdir> <timelimit>
# <testdir> should be populated with <name>.in <name>.out
# file pairs, as can be downloaded from kattis
# <testdir> should be in the test/ directory, eg test/haiku

54
longlong.py Normal file
View File

@@ -0,0 +1,54 @@
prog1 = [(i, int(b), *r) for i, b, *r in (tuple(line.split()) for line in iter(input, 'E'))]
prog2 = [(i, int(b), *r) for i, b, *r in (tuple(line.split()) for line in iter(input, 'E'))]
def swapped(a, b):
if a is None:
return b, None
if b is None:
return a, None
ai, ap, *ar = a
bi, bp, *br = b
if bi == 'I':
if ap >= bp:
a = ai, ap + 1, *ar
return b, a
if ai != bi and ap == bp:
return None, None
if bi == 'D':
if ap > bp:
a = ai, ap - 1, *ar
return b, a
return a, b
def bsort(lst, swapper):
while True:
olist = list(lst)
i = 0
while i < len(lst) - 1:
if lst[i] is None or lst[i + 1] is None or lst[i][1] >= lst[i + 1][1]:
lst[i], lst[i+1] = swapper(lst[i], lst[i + 1])
i += 1
if olist == lst:
break
# print(prog1)
bsort(prog1, swapped)
prog1 = [x for x in prog1 if x is not None]
# print(prog1)
# print()
# print(prog2)
bsort(prog2, swapped)
prog2 = [x for x in prog2 if x is not None]
# print(prog2)
print('10'[prog1 == prog2])

61
longlong_sol.py Normal file
View File

@@ -0,0 +1,61 @@
prog1 = [(i, int(b), *r) for i, b, *r in (tuple(line.split()) for line in iter(input, 'E'))]
prog2 = [(i, int(b), *r) for i, b, *r in (tuple(line.split()) for line in iter(input, 'E'))]
def swapped(a, b):
if a is None:
return b, None
if b is None:
return a, None
ai, ap, *ar = a
bi, bp, *br = b
if bi == 'I':
if ap >= bp:
a = ai, ap + 1, *ar
return b, a
if ai != bi and ap == bp:
return None, None
if bi == 'D':
if ap > bp:
a = ai, ap - 1, *ar
return b, a
return a, b
def bsort(lst, swapper, i0, i1):
if i1 - i0 <= 1:
return
if i1 - i0 == 2:
r = swapper(lst[i0], lst[i0 + 1])
lst[i0] = r[0]
lst[i0 + 1] = r[1]
m = (i0 + i1) // 2
bsort(lst, swapper, i0, m)
bsort(lst, swapper, m, i1)
for i in range(m, i1):
for j in range(i - 1, i0 - 1, -1):
oa, ob = lst[j], lst[j + 1]
na, nb = swapper(oa, ob)
if (oa, ob) == (na, nb):
break
lst[j], lst[j + 1] = na, nb
# print(prog1)
bsort(prog1, swapped, 0, len(prog1))
prog1 = [x for x in prog1 if x is not None]
# print(prog1)
# print()
# print(prog2)
bsort(prog2, swapped, 0, len(prog2))
prog2 = [x for x in prog2 if x is not None]
# print(prog2)
print('10'[prog1 == prog2])

82
palindrome_sol.py Normal file
View File

@@ -0,0 +1,82 @@
TESTING = False
inp = '9084194700940903797191718247801197019268'
def once(values, turnlist):
values = list(values)
for ai in reversed(range(len(values))):
bi = len(values) - ai - 1
diff = (values[bi] - values[ai]) % 10
if diff < 5:
turnlist[ai] += diff
values[ai] += diff
if values[ai] > 9:
if ai > 0:
values[ai - 1] += values[ai] // 10
values[ai] = values[ai] % 10
return values
def is_pal(s):
return s == s[::-1]
def get(values):
values = list(map(int, values))
turns = [0] * len(values)
while True:
new = once(values, turns)
if new == values:
break
values = new
if TESTING:
print('-----', *values)
for ai in reversed(range(len(values))):
bi = len(values) - ai - 1
if ai == bi:
pass
else:
av = int(values[ai])
bv = int(values[bi])
diff = bv - av
if diff == 5:
if bi > 0 and turns[bi - 1] > 0:
turns[bi] += 5
turns[bi - 1] -= 1
values[bi] = (values[bi] + 5) % 10
else:
turns[ai] += 5
values[ai] += 5
return turns, values
if not TESTING:
inp = input()
else:
print('input', *inp)
turns, res = get(inp)
if TESTING:
print()
print('reslt', *res)
print('rturn', *res[::-1])
print(is_pal(res))
print()
print('turns', *turns)
print(sum(turns))

BIN
printouts/icpc_test.pdf Normal file

Binary file not shown.

BIN
printouts/solutions.pdf Normal file

Binary file not shown.

Binary file not shown.

27
purple_rain_sol.py Normal file
View File

@@ -0,0 +1,27 @@
line = input()
count = 0
min_ = max_ = 0
maxi = mini = 0
for i, let in enumerate(line):
if let == 'B':
count += 1
else:
count -= 1
if count < min_:
min_ = count
mini = i + 1
if count > max_:
max_ = count
maxi = i + 1
if mini < maxi:
print(mini + 1, maxi)
elif mini > maxi:
print(maxi + 1, mini)
else:
print(mini, mini)

74
rainbowroads_sol.py Normal file
View File

@@ -0,0 +1,74 @@
from queue import Queue
n = int(input())
tree = {i: [] for i in range(1, n + 1)}
for i in range(n - 1):
a, b, c = map(int, input().strip().split())
tree[a].append((b, c))
tree[b].append((a, c))
keys = list(tree)
bad_paths = {}
def recdel(n, root):
global bad_paths
if bad_paths == 'ALL':
return
q = Queue()
q.put((n, root))
while not q.empty():
n, root = q.get()
if n not in tree:
continue
if n in bad_paths and bad_paths[n] == root:
continue
if root in bad_paths and bad_paths[root] == n:
bad_paths = 'ALL' # needs global
return
bad_paths[n] = root
neighbors = tree[n]
for e, _ in neighbors:
if e == root:
continue
q.put((e, n))
for f in keys:
if f not in tree:
continue
edges = tree[f]
all_colors = set()
bad_colors = set()
for t, c in edges:
if c in all_colors:
bad_colors.add(c)
else:
all_colors.add(c)
for t, c in edges:
if c in bad_colors:
recdel(t, f)
all_good = set()
if bad_paths != 'ALL':
all_good = set(tree) - set(bad_paths)
print(len(all_good))
for i in sorted(all_good):
print(i)

57
secbadge.py Normal file
View File

@@ -0,0 +1,57 @@
##N, L, B = input().strip().split()
##S, D = input().strip().split()
class Range():
def __init__(self, ranges):
self.ranges = ranges
def __add__(self, other):
new_ranges = list(self.ranges)
for rang1 in other.ranges:
for i, rang2 in enumerate(new_ranges):
if rang1[0] <= rang2[0] and rang1[1] >= rang2[1]:
new_ranges[i] = rang1
break
if rang1[0] >= rang2[0] and rang1[1] <= rang2[1]:
break
if rang1[1] >= rang2[0]-1 and rang1[1] <= rang2[1]:
new_ranges[i] = (rang1[0], rang2[1])
break
if rang1[0] >= rang2[0] and rang1[0] <= rang2[1]+1:
new_ranges[i] = (rang2[0], rang1[1])
break
else:
new_ranges.append(rang1)
return Range(new_ranges)
def __mul__(self, other):
new_ranges = []
for rang1 in other.ranges:
cand = rang1
for rang2 in self.ranges:
if rang1[0] <= rang2[0] and rang1[1] >= rang2[1]:
cand = rand2
elif rang1[0] >= rang2[0] and rang1[1] <= rang2[1]:
pass
elif rang1[1] >= rang2[0] and rang1[1] <= rang2[1]:
cand (rang2[0], rang1[1])
elif rang1[0] >= rang2[0] and rang1[0] <= rang2[1]:
cand = (rang1[0], rang2[1])
else:
break
else:
new_ranges.append(cand)
return Range(new_ranges)
def __repr__(self):
return str(*self.ranges)
def __str__(self):
return str(self.ranges)
Range.INF = Range([(-float('inf'), float('inf'))])
a = Range.INF
b = Range([(3, 4)])
print(a+b)

View File

@@ -0,0 +1 @@
[print('%s:'%S,*('%s,%s'%((n+1)//2,n//2)for n in range(3,S+1)if S%n in(0,(n+1)//2)),sep='\n')for S in[int(input())]]

7
star_arrangements_sol.py Normal file
View File

@@ -0,0 +1,7 @@
S = int(input())
print('%s:' % S)
for n in range(3, S + 1):
if S % n in (0, (n + 1) // 2):
print('%s,%s' % ((n + 1) // 2, n // 2))

View File

@@ -0,0 +1 @@
12

View File

@@ -0,0 +1,9 @@
5 8
1 2 1 10
2 4 11 16
2 1 9 12
3 5 28 100
1 2 3 8
4 3 20 21
1 3 13 27
3 5 23 24

View File

@@ -0,0 +1 @@
1900

View File

@@ -0,0 +1,6 @@
3 5
1 1 10 20
1 2 30 40
1 2 50 60
1 2 70 80
2 3 90 95

View File

@@ -0,0 +1,3 @@
Blue Ridge mountain road.
Leaves, glowing in autumn sun,
fall in Virginia.

View File

@@ -0,0 +1 @@
Blue Ridge mountain road. Leaves, glowing in autumn sun, fall in Virginia.

View File

@@ -0,0 +1 @@
Who would know if we had too few syllables?

View File

@@ -0,0 +1 @@
Who would know if we had too few syllables?

View File

@@ -0,0 +1,3 @@
International
contest- motivation high
Programmers have fun!.

View File

@@ -0,0 +1 @@
International contest- motivation high Programmers have fun!.

View File

@@ -0,0 +1 @@
Programming contest is stressing us all out. International pain.

View File

@@ -0,0 +1 @@
Programming contest is stressing us all out. International pain.

View File

@@ -0,0 +1 @@
3 7

View File

@@ -0,0 +1 @@
BBRRBRRBRB

View File

@@ -0,0 +1 @@
1 5

View File

@@ -0,0 +1 @@
BBRBBRRB

View File

@@ -0,0 +1,5 @@
4
3
4
5
6

View File

@@ -0,0 +1,8 @@
8
1 3 1
2 3 1
3 4 3
4 5 4
5 6 3
6 7 2
6 8 2

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,8 @@
8
1 2 2
1 3 1
2 4 3
2 7 1
3 5 2
5 6 2
7 8 1

View File

@@ -0,0 +1,6 @@
5
1
2
3
6
7

View File

@@ -0,0 +1,9 @@
9
1 2 2
1 3 1
1 4 5
1 5 5
2 6 3
3 7 3
4 8 1
5 9 2

View File

@@ -0,0 +1,5 @@
4
1
6
7
9

View File

@@ -0,0 +1,10 @@
10
9 2 1
9 3 1
9 4 2
9 5 2
9 1 3
9 6 4
1 8 5
1 10 5
6 7 9

View File

@@ -0,0 +1 @@
5

View File

@@ -0,0 +1,7 @@
4 5 10
3 2
1 2 4 7
3 1 1 6
3 4 7 10
2 4 3 5
4 2 8 9

View File

@@ -0,0 +1 @@
5

View File

@@ -0,0 +1,7 @@
4 5 9
1 4
1 2 3 5
1 3 6 7
1 4 2 3
2 4 4 6
3 4 7 9

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
3

View File

@@ -0,0 +1 @@
009990001

View File

@@ -0,0 +1 @@
5

View File

@@ -0,0 +1 @@
29998

View File

@@ -0,0 +1 @@
4

View File

@@ -0,0 +1 @@
610

View File

@@ -0,0 +1 @@
2

View File

@@ -0,0 +1 @@
981

View File

@@ -0,0 +1 @@
54

View File

@@ -0,0 +1 @@
9084194700940903797191718247801197019268

View File

@@ -0,0 +1,2 @@
3:
2,1

View File

@@ -0,0 +1 @@
3

View File

@@ -0,0 +1,11 @@
50:
2,1
2,2
3,2
5,4
5,5
6,5
10,10
13,12
17,16
25,25

View File

@@ -0,0 +1 @@
50

View File

@@ -0,0 +1,6 @@
51:
2,1
3,3
9,8
17,17
26,25

View File

@@ -0,0 +1 @@
51

View File

@@ -0,0 +1 @@
0

12
test/strings/custom_1.in Normal file
View File

@@ -0,0 +1,12 @@
I 5 A
I 6 B
D 3
D 4
I 4 C
I 3 D
D 6
E
I 3 D
D 4
I 5 C
E

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,6 @@
D 1
D 2
E
D 3
D 1
E

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,6 @@
D 2
D 1
E
D 1
D 2
E

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,4 @@
I 1 X
D 1
E
E

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,6 @@
I 14 B
I 14 A
E
I 14 A
I 15 B
E

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,6 @@
I 14 A
I 15 B
E
I 14 B
I 15 A
E