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

Commonmark migration
Source Link

#Python 2, 486 bytes

Python 2, 486 bytes

This is the reference solution which I golfed more (currently -98 bytes).

import sys;sys.setrecursionlimit(2000)
def r(s):
 n=s[0]
 if n in A:f=lambda i:r([s[int(t)]if'0'<t[0]<':'else t for t in A[n][i]]);return f(1+(f(0)or 0))
 k=s[1]
 if'i'<n:print k,'=',V.get(k,0)
 elif'd'<n:V[k]=-~V[k]if k in V else 1
 elif'a'<n:
 if~-(k in V)or V[k]<1:return 1
 V[k]-=1
 else:A[k]=s[2:]
A={};V={};c=filter(bool,([l,l[:l.find('#')]]['#'in l]for l in input().split('\n')))
while c:
 s=c[0].split();c=c[1:]
 if'a'!=s[0]:r(s)
 else:r(['a',s[1]]+map(str.split,c[:3]));c=c[3:]

Changes (that I recall):

  • automatic boolean-integer conversion ([l,l[:l.find('#')]]['#'in l]).
  • set or increment in one statement (V[k]=-~V[k]if k in V else 1)
  • more aliases to longer expressions (k=s[1])
  • no counter in the main loop, clearing the input list instead
  • print automatically adding spaces (print k,'=',V.get(k,0))
  • checking digits 1-9 ('0'<t[0]<':')
  • flipping the return values of r around to save returns
  • removing repetition of slicing and splitting (map(str.split,c[:3])))

#Python 2, 486 bytes

This is the reference solution which I golfed more (currently -98 bytes).

import sys;sys.setrecursionlimit(2000)
def r(s):
 n=s[0]
 if n in A:f=lambda i:r([s[int(t)]if'0'<t[0]<':'else t for t in A[n][i]]);return f(1+(f(0)or 0))
 k=s[1]
 if'i'<n:print k,'=',V.get(k,0)
 elif'd'<n:V[k]=-~V[k]if k in V else 1
 elif'a'<n:
 if~-(k in V)or V[k]<1:return 1
 V[k]-=1
 else:A[k]=s[2:]
A={};V={};c=filter(bool,([l,l[:l.find('#')]]['#'in l]for l in input().split('\n')))
while c:
 s=c[0].split();c=c[1:]
 if'a'!=s[0]:r(s)
 else:r(['a',s[1]]+map(str.split,c[:3]));c=c[3:]

Changes (that I recall):

  • automatic boolean-integer conversion ([l,l[:l.find('#')]]['#'in l]).
  • set or increment in one statement (V[k]=-~V[k]if k in V else 1)
  • more aliases to longer expressions (k=s[1])
  • no counter in the main loop, clearing the input list instead
  • print automatically adding spaces (print k,'=',V.get(k,0))
  • checking digits 1-9 ('0'<t[0]<':')
  • flipping the return values of r around to save returns
  • removing repetition of slicing and splitting (map(str.split,c[:3])))

Python 2, 486 bytes

This is the reference solution which I golfed more (currently -98 bytes).

import sys;sys.setrecursionlimit(2000)
def r(s):
 n=s[0]
 if n in A:f=lambda i:r([s[int(t)]if'0'<t[0]<':'else t for t in A[n][i]]);return f(1+(f(0)or 0))
 k=s[1]
 if'i'<n:print k,'=',V.get(k,0)
 elif'd'<n:V[k]=-~V[k]if k in V else 1
 elif'a'<n:
 if~-(k in V)or V[k]<1:return 1
 V[k]-=1
 else:A[k]=s[2:]
A={};V={};c=filter(bool,([l,l[:l.find('#')]]['#'in l]for l in input().split('\n')))
while c:
 s=c[0].split();c=c[1:]
 if'a'!=s[0]:r(s)
 else:r(['a',s[1]]+map(str.split,c[:3]));c=c[3:]

Changes (that I recall):

  • automatic boolean-integer conversion ([l,l[:l.find('#')]]['#'in l]).
  • set or increment in one statement (V[k]=-~V[k]if k in V else 1)
  • more aliases to longer expressions (k=s[1])
  • no counter in the main loop, clearing the input list instead
  • print automatically adding spaces (print k,'=',V.get(k,0))
  • checking digits 1-9 ('0'<t[0]<':')
  • flipping the return values of r around to save returns
  • removing repetition of slicing and splitting (map(str.split,c[:3])))
added syntax highlighting.
Source Link
Kade
  • 7.9k
  • 1
  • 26
  • 53

#Python 2, 486 bytes

This is the reference solution which I golfed more (currently -98 bytes).

import sys;sys.setrecursionlimit(2000)
def r(s):
 n=s[0]
 if n in A:f=lambda i:r([s[int(t)]if'0'<t[0]<':'else t for t in A[n][i]]);return f(1+(f(0)or 0))
 k=s[1]
 if'i'<n:print k,'=',V.get(k,0)
 elif'd'<n:V[k]=-~V[k]if k in V else 1
 elif'a'<n:
 if~-(k in V)or V[k]<1:return 1
 V[k]-=1
 else:A[k]=s[2:]
A={};V={};c=filter(bool,([l,l[:l.find('#')]]['#'in l]for l in input().split('\n')))
while c:
 s=c[0].split();c=c[1:]
 if'a'!=s[0]:r(s)
 else:r(['a',s[1]]+map(str.split,c[:3]));c=c[3:]
import sys;sys.setrecursionlimit(2000)
def r(s):
 n=s[0]
 if n in A:f=lambda i:r([s[int(t)]if'0'<t[0]<':'else t for t in A[n][i]]);return f(1+(f(0)or 0))
 k=s[1]
 if'i'<n:print k,'=',V.get(k,0)
 elif'd'<n:V[k]=-~V[k]if k in V else 1
 elif'a'<n:
 if~-(k in V)or V[k]<1:return 1
 V[k]-=1
 else:A[k]=s[2:]
A={};V={};c=filter(bool,([l,l[:l.find('#')]]['#'in l]for l in input().split('\n')))
while c:
 s=c[0].split();c=c[1:]
 if'a'!=s[0]:r(s)
 else:r(['a',s[1]]+map(str.split,c[:3]));c=c[3:]

Changes (that I recall):

  • automatic boolean-integer conversion ([l,l[:l.find('#')]]['#'in l]).
  • set or increment in one statement (V[k]=-~V[k]if k in V else 1)
  • more aliases to longer expressions (k=s[1])
  • no counter in the main loop, clearing the input list instead
  • print automatically adding spaces (print k,'=',V.get(k,0))
  • checking digits 1-9 ('0'<t[0]<':')
  • flipping the return values of r around to save returns
  • removing repetition of slicing and splitting (map(str.split,c[:3])))

#Python 2, 486 bytes

This is the reference solution which I golfed more (currently -98 bytes).

import sys;sys.setrecursionlimit(2000)
def r(s):
 n=s[0]
 if n in A:f=lambda i:r([s[int(t)]if'0'<t[0]<':'else t for t in A[n][i]]);return f(1+(f(0)or 0))
 k=s[1]
 if'i'<n:print k,'=',V.get(k,0)
 elif'd'<n:V[k]=-~V[k]if k in V else 1
 elif'a'<n:
 if~-(k in V)or V[k]<1:return 1
 V[k]-=1
 else:A[k]=s[2:]
A={};V={};c=filter(bool,([l,l[:l.find('#')]]['#'in l]for l in input().split('\n')))
while c:
 s=c[0].split();c=c[1:]
 if'a'!=s[0]:r(s)
 else:r(['a',s[1]]+map(str.split,c[:3]));c=c[3:]

Changes (that I recall):

  • automatic boolean-integer conversion ([l,l[:l.find('#')]]['#'in l]).
  • set or increment in one statement (V[k]=-~V[k]if k in V else 1)
  • more aliases to longer expressions (k=s[1])
  • no counter in the main loop, clearing the input list instead
  • print automatically adding spaces (print k,'=',V.get(k,0))
  • checking digits 1-9 ('0'<t[0]<':')
  • flipping the return values of r around to save returns
  • removing repetition of slicing and splitting (map(str.split,c[:3])))

#Python 2, 486 bytes

This is the reference solution which I golfed more (currently -98 bytes).

import sys;sys.setrecursionlimit(2000)
def r(s):
 n=s[0]
 if n in A:f=lambda i:r([s[int(t)]if'0'<t[0]<':'else t for t in A[n][i]]);return f(1+(f(0)or 0))
 k=s[1]
 if'i'<n:print k,'=',V.get(k,0)
 elif'd'<n:V[k]=-~V[k]if k in V else 1
 elif'a'<n:
 if~-(k in V)or V[k]<1:return 1
 V[k]-=1
 else:A[k]=s[2:]
A={};V={};c=filter(bool,([l,l[:l.find('#')]]['#'in l]for l in input().split('\n')))
while c:
 s=c[0].split();c=c[1:]
 if'a'!=s[0]:r(s)
 else:r(['a',s[1]]+map(str.split,c[:3]));c=c[3:]

Changes (that I recall):

  • automatic boolean-integer conversion ([l,l[:l.find('#')]]['#'in l]).
  • set or increment in one statement (V[k]=-~V[k]if k in V else 1)
  • more aliases to longer expressions (k=s[1])
  • no counter in the main loop, clearing the input list instead
  • print automatically adding spaces (print k,'=',V.get(k,0))
  • checking digits 1-9 ('0'<t[0]<':')
  • flipping the return values of r around to save returns
  • removing repetition of slicing and splitting (map(str.split,c[:3])))
Source Link
PurkkaKoodari
  • 17.9k
  • 2
  • 37
  • 92

#Python 2, 486 bytes

This is the reference solution which I golfed more (currently -98 bytes).

import sys;sys.setrecursionlimit(2000)
def r(s):
 n=s[0]
 if n in A:f=lambda i:r([s[int(t)]if'0'<t[0]<':'else t for t in A[n][i]]);return f(1+(f(0)or 0))
 k=s[1]
 if'i'<n:print k,'=',V.get(k,0)
 elif'd'<n:V[k]=-~V[k]if k in V else 1
 elif'a'<n:
 if~-(k in V)or V[k]<1:return 1
 V[k]-=1
 else:A[k]=s[2:]
A={};V={};c=filter(bool,([l,l[:l.find('#')]]['#'in l]for l in input().split('\n')))
while c:
 s=c[0].split();c=c[1:]
 if'a'!=s[0]:r(s)
 else:r(['a',s[1]]+map(str.split,c[:3]));c=c[3:]

Changes (that I recall):

  • automatic boolean-integer conversion ([l,l[:l.find('#')]]['#'in l]).
  • set or increment in one statement (V[k]=-~V[k]if k in V else 1)
  • more aliases to longer expressions (k=s[1])
  • no counter in the main loop, clearing the input list instead
  • print automatically adding spaces (print k,'=',V.get(k,0))
  • checking digits 1-9 ('0'<t[0]<':')
  • flipping the return values of r around to save returns
  • removing repetition of slicing and splitting (map(str.split,c[:3])))

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