Skip to content

Instantly share code, notes, and snippets.

@chzyer
Last active December 23, 2015 16:59
Show Gist options
  • Save chzyer/6665823 to your computer and use it in GitHub Desktop.
Save chzyer/6665823 to your computer and use it in GitHub Desktop.
def make(progress):
data = sorted([[v, k] for k, v in progress.iteritems()])
idx = 0
for i in range(len(data)-1, -1, -1):
if i == 0: continue
data[i][0] -= data[i-1][0]
real_data = []
length = 0
for i in data:
if length < 0:
if i[0] + length > 0:
remain_length = -length
else:
remain_length = i[0]
real_data.append([remain_length, i[1]])
length += i[0]
while length > 0:
real_data.append([length if length <= 10 else 10, i[1]])
length -= 10
data = []
total = 0
html = ""
for per, color in real_data:
total += per
html += '''<div style="float:left;width:%spx;height:20px;background:%s"></div>''' % (per*2, color)
if total % 10 == 0:
data.append(html)
html = ""
return '<div style="float:left;background:#000;width:1px;height:20px"></div>'.join(data)
progress = {
'blue': 37,
'red': 58,
'pink': 75,
'yellow': 78,
'green': 81,
'white': 100,
}
print make(progress)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment