1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| from random import seed, shuffle import sys
try: your_list = [int(x) for x in input('Enter a permutation of 0, ..., n ' 'for some n >= 0: ' ).split() ] if not your_list: raise ValueError your_list_as_set = set(your_list) if len(your_list_as_set) != len(your_list) \ or your_list_as_set != set(range(len(your_list))): raise ValueError except ValueError: print('Incorrect input, giving up.') sys.exit() try: for_seed, length = \ (int(x) for x in input('Enter two integers, ' 'the second one between 0 and 10: ' ).split() ) if not 0 <= length <= 10: raise ValueError except ValueError: print('Incorrect input, giving up.') sys.exit() seed(for_seed) my_list = list(range(length)) shuffle(my_list) print('Here is your list:') print(' ', your_list) print('Here is my list:') print(' ', my_list)
def removeMinMax(your_list): while your_list and ( your_list[0] == min(your_list) or your_list[0] == max(your_list) or your_list[-1] == min(your_list) or your_list[-1] == max(your_list)): min_value = min(your_list) max_value = max(your_list) if min_value == your_list[-1] or max_value == your_list[-1]: your_list.pop() elif min_value == your_list[0] or max_value == your_list[0]: your_list.pop(0)
removeMinMax(your_list)
print() print('Removing again and again the currently largest\n' 'or smallest element in your list for as long as\n' 'it currently starts or ends the list, we get:' ) print(your_list) print() print("That's how to travel in my list:")
dict_space = {} last_advent = 0
for cur in sorted(my_list): dict_space[int(cur)] = 2 * my_list.index(int(cur))
tmp = my_list while tmp: cur = str(min(my_list)) if last_advent > dict_space[int(cur)]: print(' ' * dict_space[int(cur)] + cur + '-' * (last_advent - dict_space[int(cur)])) elif last_advent < dict_space[int(cur)]: if int(cur) == 0: print(' ' * dict_space[int(cur)] + cur + '-' * (last_advent - dict_space[int(cur)])) else: print(' ' * last_advent + '-' * (dict_space[int(cur)] - last_advent) + cur) last_advent = dict_space[int(cur)] tmp.remove(int(cur))
|