Created
January 27, 2018 17:29
-
-
Save joeweller/4aab82ded392f856d8a7cb7d13f315ed to your computer and use it in GitHub Desktop.
A visual representation: populating python3 dicts with RNG through list elimination
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
import os | |
import random | |
import time as Time | |
"""an efficiency visualisation: indexing dict keys and randomly choosing | |
one to change its value until all are changed | |
-removes key from index after value change: no collisions | |
-100 dict key value pairs""" | |
def main(): | |
full_index = [] # dict key index list | |
number_grid = {} # empty dict | |
# create dict with key name and default value. append key to index | |
for i in range(0, 100): | |
number_grid[i] = " " | |
full_index.append(i) | |
draw_grid(number_grid) # draw first visual grid with empty values | |
added = 0 # counter: increment after a key value change | |
count = 0 # counter: increment after every loop iteration | |
# main loop: stop when index is empty | |
while len(full_index): | |
count += 1 | |
Time.sleep(0.025) # slow down loop for easier visuals | |
os.system("clear") # clear screen | |
tmp = random.choice(full_index) # choose a random value from key index list | |
number_grid[tmp] = "x" # change value of dict key | |
added += 1 | |
full_index.remove(tmp) # remove the key from index list | |
draw_grid(number_grid) # print updated values to screen | |
# value info | |
print("Added Numbers: {}/100\nCount: {}\nIndex Left: {}".format(added, count, len(full_index))) | |
# loop ended | |
input("\n- Ended -") | |
return 1 | |
def draw_grid(grid): | |
c = 0 # counter: starting position for current format row | |
# console print loop | |
# complete: 10 rows of 10 columns | |
for i in range(0, 10): | |
# tidy single digit formatting on first line | |
# print 2 rows of 10 items each iteration. 1st row: dict keys. 2nd: dict values | |
if c == 0: | |
print("{} {} {} {} {} {} {} {} {} {}".format(*tuple(i for i in range(c, (c+10))))) | |
else: | |
print("{} {} {} {} {} {} {} {} {} {}".format(*tuple(i for i in range(c, (c+10))))) | |
print("{} {} {} {} {} {} {} {} {} {} \n".format(*tuple(grid[i] for i in range(c, (c+10))))) | |
c += 10 # iterate for next 10 items in dict | |
return | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment