Time: 2017-08-30 00:23:10

Category: Linux Tags: bash

I have posted earlier about circular list in python. I thought to try in BASH. Here it is.

color=(black yellow red green blue cyan)
nums=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)
for ((i=0;i<lennums;i++));do
 if [ $i -gt $lencolor ]; then
  a=$(expr $i % $lencolor);
  echo ${color[a]};
 elif [ $i -le $lencolor ]; then
  a=$(expr $i % $lencolor);
  echo ${color[a]};

Time: 2017-08-30 00:17:17

Category: Linux Tags: python

I was trying to develop my python blog. I wanted to make a dashboard with some statistics showing on it, some kind of bar chart. I did chose chartjs for it. But when I implemented the chartjs, I realized when the post increases the bar colors remain unchanged after 10 post. I did set only 10 in the javascript. The other post shows grey. :(

What to do then?

I had to generate color. Let, I have some favorite color to paint the bars of the bar chart.

colorlist = [`blue`,`red`,`green`,`yellow`,`cyan`,`orange`,`azure`]

So, I have these seven very bright colors. How can I make another list by the number of post? That means I wanted to make a list of colors with the number of blog posts. If the number of blog post increases, the number of colors will increase too. But it is quite weird. If blog post increases to 100, then the color should be 100 too. That is not good.

I decided to make an interesting list. Why not generate the colorlist using the already one but with appending at the end. That means if there are 8 blog posts then the colorlist will give 7 colors to the first 7, the 8th one will get the `blue` color. And if another blog post added, then the new one will get `red`.

So, It is about a circular list using a base colorlist. There will be a new colorlist whenever the blogpost list increases or decreases.

The code:

colorlist = [`blue`,`red`,`green`,`yellow`,`cyan`,`orange`,`azure`]
post_list = [1,2,3,4,5,6,7,8,9,10]
newcolorlist = [colorlist[i % len(colorlist)] for i in xrange(len(post_list))]

Here, taking the length of post_list and by the integer number of the length of post_list, I had to take the xrange function to generate the list containing that much number. Then, for loop using i for every number in the xrange list. Using this i, I had to use modulo operator to get the remainder. Also, I had to get the length of the colorlist. The modulo of `i % len(colorlist)` will always give the numbers from 0 to '`number of post_list` - 1 '. I used these number 0-6 to use as position of item in the list.

This one do generate a circular list of list. There are other convenient ones.

from random import randint
from time import time
from itertools import cycle, islice

taglist = [str(randint(1,10000)) for i in xrange(100000)]
num_list = range(567890)

time1 = time()
for i in xrange(10):
  newtaglist1 = [taglist[i % len(taglist)] for i in xrange(len(num_list))]
print 'XRANGE Runtime:', time() - time1

time1 = time()
for i in range(10):
  newtaglist1 = [taglist[i % len(taglist)] for i in xrange(len(num_list))]
print 'RANGE Runtime:', time() - time1

time1 = time()
for i in xrange(10):
  newtaglist2 = list(islice(cycle(taglist), len(num_list)))
print 'SLICE Runtime:', time() - time1

time1 = time()
for i in xrange(10):
  newtaglist3 = (taglist * -(len(num_list) // -len(taglist)))[:len(num_list)]
print 'ANOTHER XRANGE Runtime:', time() - time1

time1 = time()
for i in xrange(10):
  q, r = divmod(len(num_list), len(taglist))
  newtaglist4 = taglist * q + taglist[:r]
print 'DIVMOD Runtime:', time() - time1

assert newtaglist1 == newtaglist2 == newtaglist3  == newtaglist4
The Output:

XRANGE Runtime: 0.91922712326
RANGE Runtime: 1.47419095039
SLICE Runtime: 0.138383865356
ANOTHER XRANGE Runtime: 0.102878093719
DIVMOD Runtime: 0.103218078613

So, using DIVMOD seems reasonable for future.