Created
February 1, 2015 14:47
-
-
Save antonva/46639cf4f80a6a0c9df8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def compr(string_list): | |
# conversion yfir i tolur. | |
# l thvi ad 'arrays' i python eru i raun listar | |
l = [] | |
for i in string_list.split(): | |
l.append(int(i)) | |
#compression list | |
cl = [] | |
# Traditional for loopa fyrir list indexa | |
for i in range(0,len(l)): | |
# Frumskilyrdi, baetum fyrsta stakinu vid sem lista/array | |
if i == 0: | |
cl.append([l[i]]) | |
else: | |
#fyrir hvern lista i compression listanum | |
for c in cl: | |
#continuity check | |
if l[i] == l[i-1] + 1: | |
# ef fyrra stakid er i thessum lista og hann er | |
# staerri en 1 stak, tha skiptum vid ut stakinu. | |
if l[i-1] in c and len(c) > 1: | |
c[c.index(l[i-1])] = l[i] | |
# Annars er klarlega bara 1 stak i listanum og vid | |
# baetum hinu vid. | |
elif l[i-1] in c: | |
c.append(l[i]) | |
# Dirty conditionals svo vid seum ekki ad tvitelja | |
# inni i tvofoldu for lykkjunni. | |
elif l[i] in c or [l[i]] in cl: | |
pass | |
# Ef ekkert gengur, baetum vid vid listann. | |
else: | |
cl.append([l[i]]) | |
# Pretty print | |
p = "" | |
for c in cl: | |
if len(c) > 1: | |
p += str(c[0])+"-"+str(c[1])+" " | |
else: | |
p += str(c[0]) + " " | |
print(p) | |
# main. ignore this. | |
if __name__ == '__main__': | |
s = input() | |
compr(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment