Magnificent demos in python.
- ▶ before we start...
- ▶ walk through the indices
- ▶ ancient sorting algorithm
- ▶ Daily Prophet
- ▶ code and the key of secrets
- ▶ Iterating through the non-iterable!
- ▶ while in for
- ▶ mathmatican's special
- ▶ very last end
Don't!
Ever!
Dream!
About!
Using!
These!
Code!
In!
Work!
We sometimes use these code to iterate through indices:for x in range(len(iterable)):
dosomething
Well, why not use some wrappings?
indices = lambda iterable: range(len(iterable))
for x in indices(iterable):
dosomething
def abacus_sort(array: list):
col_sight = [0] * max(array)
row_sight = [0] * len(array)
for i in array:
for col in range(i):
col_sight[col] += 1
for col in col_sight:
for row in range(col):
row_sight[row] += 1
row_sight.reverse()
return row_sight
This algorithm simulates an abacus. The integers are number of beads on each row.
It sets the abacus as the array we put inside, then
flip it and make it stand on the ground. Now you see, the abacus has been sorted automatically!
class Prophet:
@staticmethod
def see_append(array, item):
return array + item
@staticmethod
def see_insert(array, idx, item):
return array[:idx] + [item] + array[idx + 1:]
@staticmethod
def see_pop(array, idx):
return array[:idx] + array[idx + 1:]
You can use it as you imported a module named Prophet
.
Yes!
class AnykeyMap:
def __init__(self, mappings):
self.keys = ()
self.values = ()
for key, value in mappings:
self.keys += (key,)
self.values += (value,)
def __getitem__(self, key):
return self.values(self.keys.index(key))
def __setitem__(self, key, value):
idx = self.keys.index(key)
self.values = self.values[:idx] + (value,) + self.values[idx + 1:]
def __delitem__(self, key):
idx = self.keys.index(key)
self.keys = self.keys[:idx] + self.keys[idx + 1:]
self.values = self.values[:idx] + self.values[idx + 1:]
You can find something similar with the Prophet
class. Unfortunately, for reducing the memory, we need to deal with tuples,
not lists.
class IterableInt:
def __init__(self, integer):
self.integer = integer
self.string = str(integer)
self.curr = 0
def __next__(self):
if self.curr >= len(self.string):
raise StopIteration
return_val = int(self.string[self.curr])
self.curr += 1
return return_val
def __iter__(self):
return self
Yes!
class While_loop:
def __init__(self, expr):
self.expr = expr
def __next__(self):
if not eval(self.expr):
raise StopIteration
return
def __iter__(self):
return self
And a demo for the demo:
from random import randint
i = 1
for _ in While_loop('i != 0'):
i = randint(0, 10)
print(i)
You'll see these if run it for several times (may not identical, this is the result I got):
first:
7
0
second:
1
9
0
third:
3
9
3
2
1
4
0
real magic!
How to get a line's expression by two dots on it?def line_expr(p1, p2):
x1, y1 = p1
x2, y2 = p2
m = (y2 - y1) / (x2 - x1)
b = y2 - m * x2
return lambda x: m * x + b
But I thought it will be long...
Well, if you're a nut and you do want to use those code, you don't need to copy.Use pip or original install command to make it!