Last active
October 11, 2019 12:50
-
-
Save mehuaniket/886ed8bff88ed6a7a44c6b3075c91098 to your computer and use it in GitHub Desktop.
Item order in database (chrono_order)
This file contains hidden or 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 random | |
| MIN_INDEX = 0 | |
| MAX_INDEX = 100 | |
| RECURSION_COUNT = 10000 | |
| CLOSE_SIZE = 20 | |
| ITEMS = [] | |
| for i in range(MIN_INDEX, MAX_INDEX + 1): | |
| ITEMS.append(i * 100) | |
| for i in range(1, RECURSION_COUNT): | |
| ITEMS.sort() | |
| # we will randomaly pick any element from whole list | |
| item_position = random.randint(MIN_INDEX, MAX_INDEX) | |
| print("running step for {} for item {}".format(i, item_position)) | |
| # to make it harder we will only pick new position close to element that we pick | |
| close_up = item_position - CLOSE_SIZE if (item_position - CLOSE_SIZE) >= MIN_INDEX else MIN_INDEX | |
| up_pick = random.randint(close_up, item_position) | |
| close_down = item_position + CLOSE_SIZE if (item_position + CLOSE_SIZE) <= MAX_INDEX else MAX_INDEX | |
| down_pick = random.randint(item_position, close_down) | |
| new_postion = random.randint(up_pick, down_pick) | |
| print("up pick {} new position {} down pick {}".format(up_pick, new_postion, down_pick)) | |
| # we will move our position to that | |
| if new_postion == MIN_INDEX: | |
| ITEMS[new_postion] = 0 | |
| if ITEMS[new_postion] == ITEMS[new_postion + 1]: | |
| break | |
| elif new_postion == MAX_INDEX: | |
| ITEMS[new_postion] = ITEMS[new_postion - 1] + 200 | |
| if ITEMS[new_postion] == ITEMS[new_postion - 1]: | |
| break | |
| else: | |
| ITEMS[new_postion] = int((ITEMS[new_postion + 1] + ITEMS[new_postion - 1])/2) | |
| # we will check for the value for uniqueness | |
| if (ITEMS[new_postion] == ITEMS[new_postion -1]) and (ITEMS[new_postion] == ITEMS[new_postion + 1]): | |
| break | |
| # print(ITEMS[new_postion]) | |
| print("Last state of ITEMS") | |
| ITEMS.sort() | |
| print(ITEMS) | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just to check the feasibility of the item order in database. Oftentimes, Maintaining a sequential value can lead to updating all values(all rows) in the data table.
This program demonstrates that if we take 100 items and reshuffle them thousand times with the above logic it doesn't break.
I hope it helps.