Created
March 19, 2016 17:17
-
-
Save koaning/955c7ef41428f5252639 to your computer and use it in GitHub Desktop.
ascii fun
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
https://asciinema.org/ | |
Fun with Asciinema | |
``` | |
from random import choice | |
import time | |
from colorama import Fore, Back, Style | |
def col(): | |
key1 = choice(Fore.__dict__.keys()) | |
return Fore.__dict__[key1] | |
for i in range(60): | |
time.sleep(0.1) | |
print ''.join([col() + 'rainbow' for i in range(10)]) | |
``` | |
This one is curtosy from rosetta code. | |
``` | |
import numpy as np | |
from random import choice | |
import time | |
def rand_sort(arr): | |
num_iters = 0 | |
while arr != sorted(arr): | |
num_iters += 1 | |
i, j = choice(range(len(arr))), choice(range(len(arr))) | |
if (arr[i] < arr[j]) and (i > j): | |
arr[i], arr[j] = arr[j], arr[i] | |
print arr | |
time.sleep(0.1) | |
print 'this took {} iterations'.format(num_iters) | |
arr = list(np.random.randint(1, 100, 15)) | |
rand_sort(arr) | |
arr = list(np.random.randint(1, 100, 32)) | |
rand_sort(arr) | |
``` | |
We could combine the two giggles to have the sorting have color as well. | |
``` | |
def color_arr(arr): | |
to_print = [str(a).zfill(2) for a in arr] | |
sorted_print = sorted(to_print) | |
for i, val in enumerate(to_print): | |
if sorted_print[i] == val: | |
to_print[i] = Fore.GREEN + val + Fore.WHITE | |
else: | |
to_print[i] = Fore.RED + val + Fore.WHITE | |
return ','.join(to_print) | |
def rand_sort(arr): | |
num_iters = 0 | |
while arr != sorted(arr): | |
num_iters += 1 | |
i, j = choice(range(len(arr))), choice(range(len(arr))) | |
if (arr[i] < arr[j]) and (i > j): | |
arr[i], arr[j] = arr[j], arr[i] | |
print color_arr(arr) | |
time.sleep(0.1) | |
print 'this took {} iterations'.format(num_iters) | |
arr = list(np.random.randint(1, 100, 30)) | |
rand_sort(arr) | |
``` | |
We can even go all the way by running a state simulation | |
``` | |
import itertools as it | |
def av_neighbors(i,j,m): | |
prod = it.product(np.arange(i-1, i+2), np.arange(j-1, j+2)) | |
prod = filter(lambda (x,y): x > 0 and x < m.shape[0], prod) | |
prod = filter(lambda (x,y): y > 0 and y < m.shape[1], prod) | |
t = 0 | |
for (i,j) in prod: | |
t += m[i, j] | |
return -1 if np.round(t) < 0 else np.min([np.round(t), 1]) | |
def neighbor_map(m): | |
n = np.zeros(m.shape) | |
for i in range(m.shape[0]): | |
for j in range(m.shape[1]): | |
n[i,j] = av_neighbors(i,j,m) | |
return n | |
def print_mat(m): | |
row_strs = [] | |
for i, row in enumerate(m): | |
row_list = [] | |
for j, col in enumerate(row): | |
val = str(int(m[i,j])).replace('-', '').zfill(2) | |
if m[i,j] > 0: | |
row_list.append(Fore.GREEN + val + Fore.WHITE) | |
if m[i,j] < 0: | |
row_list.append(Fore.RED + val + Fore.WHITE) | |
if m[i,j] == 0: | |
row_list.append(Fore.BLACK + val + Fore.WHITE) | |
row_strs.append(','.join(row_list)) | |
print '\n'.join(row_strs) | |
for samp in range(10): | |
m = np.zeros([30,30]) | |
for iteration in range(50): | |
m = m + np.random.randint(-1, 2, m.shape) | |
print_mat(m) | |
time.sleep(0.07) | |
%clear | |
if np.random.random() < 0.3: | |
m = m + neighbor_map(m) | |
print_mat(m) | |
time.sleep(0.07) | |
%clear | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment