Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Bounty Awarded with 200 reputation awarded by Peter Wirdemo
golfed
Source Link

Python 3, 974925 bytes

Yay, under 1 KB! Probably still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin:a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
a=set()
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:y=m.get(x.p);x.l=1
 if y:x.l=-1;k(y);xif y else a.l=yadd(x.l+1p);x.l=1
for x in m:k(m[x])
r=setr=L(m[x].p for x in m if m[x]s=a.l<2pop(),p=0,m=0)
if len(r)>1a:e('Multiple roots')
r=L(s=r.pop(),p=0,m=0,l=0)
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:u(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(p,x,o=o):
 j=pd=x.m-p.m;j=p.m+r.x-x.x;s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*j+'└'+'♦'*(x.m-p.md-1);z='─'
 if"'"in s or s[-2:]=='NL'or x.l<1x==r:q+=z*(x.x-l[0].x);z=' '
 q+=z*o+=list("%3d│ "%x.m+q+z*(r.x-len(q));o.append(list("%3d│ "%x.m+q+'+' %-20s[%3d][%3d]'%(s,x.m-p.md,x.x)));j+=5,;j+=5;o[p.i][j]='┐┬'[o[p.i][j]in'─┬']
 for i in range(p.i+1,x.i):o[i][j]='├│'[o[i][j]in' │']
 o[p.i][j]='┐┬'[o[p.i][j]in'─┬']
 for y in l:f(x,y)
f(r,r)
print('\n'.join(''.join(x)for x in o))

Python 3, 974 bytes

Yay, under 1 KB! Probably still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin:a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:y=m.get(x.p);x.l=1
 if y:x.l=-1;k(y);x.l=y.l+1
for x in m:k(m[x])
r=set(m[x].p for x in m if m[x].l<2)
if len(r)>1:e('Multiple roots')
r=L(s=r.pop(),p=0,m=0,l=0)
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:u(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(p,x):
 j=p.m+r.x-x.x;s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*j+'└'+'♦'*(x.m-p.m-1);z='─'
 if"'"in s or s[-2:]=='NL'or x.l<1:q+=z*(x.x-l[0].x);z=' '
 q+=z*(r.x-len(q));o.append(list("%3d│ "%x.m+q+' %-20s[%3d][%3d]'%(s,x.m-p.m,x.x)));j+=5
 for i in range(p.i+1,x.i):o[i][j]='├│'[o[i][j]in' │']
 o[p.i][j]='┐┬'[o[p.i][j]in'─┬']
 for y in l:f(x,y)
f(r,r)
print('\n'.join(''.join(x)for x in o))

Python 3, 925 bytes

Yay, under 1 KB! Probably still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin:a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
a=set()
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:y=m.get(x.p);x.l=-1;k(y)if y else a.add(x.p);x.l=1
for x in m:k(m[x])
r=L(s=a.pop(),p=0,m=0)
if a:e('Multiple roots')
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:u(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(p,x,o=o):
 d=x.m-p.m;j=p.m+r.x-x.x;s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*j+'└'+'♦'*(d-1);z='─'
 if"'"in s or s[-2:]=='NL'or x==r:q+=z*(x.x-l[0].x);z=' '
 o+=list("%3d│ "%x.m+q+z*(r.x-len(q))+' %-20s[%3d][%3d]'%(s,d,x.x)),;j+=5;o[p.i][j]='┐┬'[o[p.i][j]in'─┬']
 for i in range(p.i+1,x.i):o[i][j]='├│'[o[i][j]in' │']
 for y in l:f(x,y)
f(r,r)
print('\n'.join(''.join(x)for x in o))
golfed
Source Link

Python 3, 1023974 bytes

Yay, under 1 KiBKB! Probably still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin.read().splitlines():a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:
 y=m.get(x.p);x.l=1;x.d=x.ml=1
 if y:x.l=-1;k(y);x.l=y.l+1;x.d-=y.ml+1
for x in m:k(m[x])
r=set(m[x].p for x in m if m[x].l<2)
if len(r)>1:e('Multiple roots')
r=L(s=r.pop(),p=0,m=0,l=0,d=0,y=0)
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:y.y=x.y+x.d;uu(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(p,x):
 j=xj=p.y+rm+r.x-x.x;s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*j+'└'+'♦'*(x.dm-p.m-1);z='─'
 if"'"in s or s[-2:]=='NL'or x.l<1:q+=z*(x.x-l[0].x);z=' '
 q+=z*(r.x-len(q));o.append(list("%3d│ "%x.m+q+' %-20s[%3d][%3d]'%(s,x.dm-p.m,x.x)));j+=5
 for i in range(p.i+1,x.i):o[i][j]='├│'[o[i][j]in' │']
 o[p.i][j]='┐┬'[o[p.i][j]in'─┬']
 for y in l:f(x,y)
f(r,r)
print('\n'.join(''.join(x)for x in o))

Python 3, 1023 bytes

Yay, under 1 KiB! Probably still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin.read().splitlines():a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:
 y=m.get(x.p);x.l=1;x.d=x.m
 if y:x.l=-1;k(y);x.l=y.l+1;x.d-=y.m
for x in m:k(m[x])
r=set(m[x].p for x in m if m[x].l<2)
if len(r)>1:e('Multiple roots')
r=L(s=r.pop(),p=0,m=0,l=0,d=0,y=0)
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:y.y=x.y+x.d;u(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(p,x):
 j=x.y+r.x-x.x;s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*j+'└'+'♦'*(x.d-1);z='─'
 if"'"in s or s[-2:]=='NL'or x.l<1:q+=z*(x.x-l[0].x);z=' '
 q+=z*(r.x-len(q));o.append(list("%3d│ "%x.m+q+' %-20s[%3d][%3d]'%(s,x.d,x.x)));j+=5
 for i in range(p.i+1,x.i):o[i][j]='├│'[o[i][j]in' │']
 o[p.i][j]='┐┬'[o[p.i][j]in'─┬']
 for y in l:f(x,y)
f(r,r)
print('\n'.join(''.join(x)for x in o))

Python 3, 974 bytes

Yay, under 1 KB! Probably still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin:a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:
 y=m.get(x.p);x.l=1
 if y:x.l=-1;k(y);x.l=y.l+1
for x in m:k(m[x])
r=set(m[x].p for x in m if m[x].l<2)
if len(r)>1:e('Multiple roots')
r=L(s=r.pop(),p=0,m=0,l=0)
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:u(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(p,x):
 j=p.m+r.x-x.x;s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*j+'└'+'♦'*(x.m-p.m-1);z='─'
 if"'"in s or s[-2:]=='NL'or x.l<1:q+=z*(x.x-l[0].x);z=' '
 q+=z*(r.x-len(q));o.append(list("%3d│ "%x.m+q+' %-20s[%3d][%3d]'%(s,x.m-p.m,x.x)));j+=5
 for i in range(p.i+1,x.i):o[i][j]='├│'[o[i][j]in' │']
 o[p.i][j]='┐┬'[o[p.i][j]in'─┬']
 for y in l:f(x,y)
f(r,r)
print('\n'.join(''.join(x)for x in o))
golfed
Source Link

Python 3, 10721023 bytes

StillYay, under 1 KiB! Probably still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin.read().splitlines():a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:
 y=m.get(x.p);x.l=1;x.d=x.m
 if y:x.l=-1;k(y);x.l=y.l+1;x.d-=y.m
for x in m:k(m[x])
r=set(m[x].p for x in m if m[x].l<2)
if len(r)>1:e('Multiple roots')
r=L(s=r.pop(),p=0,m=0,l=0,d=0,y=0)
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:y.y=x.y+x.d;u(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(p,x):
 s=xj=x.y+r.x-x.x;s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*(x.y+r.x-x.x)+'└'+'♦'*'*j+'└'+'♦'*(x.d-1);z='─'
 if"'"in s or s[-2:]=='NL'or x.l<1:q+=z*(x.x-l[0].x);z=' '
 q+=z*(r.x-len(q));o.append(list("%3d│ "%x.m+q+' %-20s[%3d][%3d]'%(s,x.d,x.x)));j+=5
 for y in l:f(y)
f(r)
for xi in m.valuesrange():
 y=mp.get(i+1,x.pi)
 if y:o[i][j]='├│'[o[i][j]in' │']
 j=x.y+ro[p.x-xi][j]='┐┬'[o[p.x+5i][j]in'─┬']
 for iy in rangel:f(y.i+1,x.i,y):o[i][j]='├│'[o[i][j]in' │']
 o[y.i][j]='┐┬'[o[y.i][j]in'─┬']f(r,r)
print('\n'.join(''.join(x)for x in o))

Python 3, 1072 bytes

Still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin.read().splitlines():a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:
 y=m.get(x.p);x.l=1;x.d=x.m
 if y:x.l=-1;k(y);x.l=y.l+1;x.d-=y.m
for x in m:k(m[x])
r=set(m[x].p for x in m if m[x].l<2)
if len(r)>1:e('Multiple roots')
r=L(s=r.pop(),p=0,m=0,l=0,d=0,y=0)
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:y.y=x.y+x.d;u(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(x):
 s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*(x.y+r.x-x.x)+'└'+'♦'*(x.d-1);z='─'
 if"'"in s or s[-2:]=='NL'or x.l<1:q+=z*(x.x-l[0].x);z=' '
 q+=z*(r.x-len(q));o.append(list("%3d│ "%x.m+q+' %-20s[%3d][%3d]'%(s,x.d,x.x)))
 for y in l:f(y)
f(r)
for x in m.values():
 y=m.get(x.p)
 if y: j=x.y+r.x-x.x+5
 for i in range(y.i+1,x.i):o[i][j]='├│'[o[i][j]in' │']
 o[y.i][j]='┐┬'[o[y.i][j]in'─┬']
print('\n'.join(''.join(x)for x in o))

Python 3, 1023 bytes

Yay, under 1 KiB! Probably still room for golfing...

import sys
class L:
 def __init__(x,**k):x.__dict__.update(k)
m={}
def e(x):print('ERROR: '+x);exit()
try:
 for x in sys.stdin.read().splitlines():a,b,c=x.split(';');m[a]=L(s=a,p=b,m=int(c),l=0)
except:e('Invalid input')
def k(x):
 if x.l<0:e('Endless loop')
 if x.l<1:
 y=m.get(x.p);x.l=1;x.d=x.m
 if y:x.l=-1;k(y);x.l=y.l+1;x.d-=y.m
for x in m:k(m[x])
r=set(m[x].p for x in m if m[x].l<2)
if len(r)>1:e('Multiple roots')
r=L(s=r.pop(),p=0,m=0,l=0,d=0,y=0)
m[r.s]=r
c={}
def u(x):
 c[x.s]=[m[y]for y in m if m[y].p==x.s];x.x=x.m
 for y in c[x.s]:y.y=x.y+x.d;u(y);x.x=max(x.x,y.x)
u(r)
o=[]
def f(p,x):
 j=x.y+r.x-x.x;s=x.s;x.i=len(o);l=sorted(c[s],key=lambda t:(t.x,t.s));q=' '*j+'└'+'♦'*(x.d-1);z='─'
 if"'"in s or s[-2:]=='NL'or x.l<1:q+=z*(x.x-l[0].x);z=' '
 q+=z*(r.x-len(q));o.append(list("%3d│ "%x.m+q+' %-20s[%3d][%3d]'%(s,x.d,x.x)));j+=5
 for i in range(p.i+1,x.i):o[i][j]='├│'[o[i][j]in' │']
 o[p.i][j]='┐┬'[o[p.i][j]in'─┬']
 for y in l:f(x,y)
f(r,r)
print('\n'.join(''.join(x)for x in o))
golfed
Source Link
Loading
golfed a bit
Source Link
Loading
golfed a bit
Source Link
Loading
Source Link
Loading

AltStyle によって変換されたページ (->オリジナル) /