Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ Challenge 5: Optimize You're Quotes

Name: Anonymous 2018-01-08 14:27

INPUT: A multi-line string specifying quoted poasts on a 2ch-like board, one poast per line. Lines are prefixed with ``>>''.

OUTPUT: A single, optimized quote link, created by replacing consecutive poast numbers with a range.

EXAMPLE INPUT ⅰ
>>1
>>2
>>3

EXAMPLE OUTPUT ⅰ
>>1-3

EXAMPLE INPUT ⅱ
>>1
>>3
>>2
>>5

EXAMPLE OUTPUT ⅱ
>>1-3,5

EXAMPLE INPUT ⅲ
>>6
>>2
>>3
>>1
>>5

EXAMPLE OUTPUT ⅲ
>>1-3,5-6

🌑 In case of only two consecutive poasts, still replace them with a range.
🌑 Numbers are positive integers from 1 too 1001 (inclusive on both sides).
🌑 Use standard input and standard output as interfaces.
🌑 Shortest code wins. Good luck!

Name: Anonymous 2018-01-08 14:52

will work on it in a while

Name: Anonymous 2018-01-09 9:19

straightforward py3 version, could be reduced further but all my ideas for exec abuse actually make it longer:

import sys,itertools;a=sorted([int(x[2:])for x in sys.stdin.readlines()]);b="";c=0
for x in itertools.count():
if x in a:
if c:continue
c=1;b+=','+str(x)
else:
if not c:continue
c=0;b+='-'+str(x-1)
if x>=max(a):break
print('>>'+b[1:])

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List