Un Fin De Semana Especial
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
WARNING: before we start...
Don't!
Ever!
Dream!
About!
Using!
These!
Code!
In!
Work!
We sometimes use these code to iterate through indices: walk through the 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
Abacus is a really old thing for us. Well, sometimes ancient gives us new ideas. ancient sorting algorithm
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!
If you want to see what a list looks like when append a thing, or insert a thing, or pop a thing? Daily Prophet
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
.
If you want to have a map that uses list or set as key, is it possible? code and the key of secrets
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.
When you want to iterate through an integer by indices like lists and strings, how? Iterating through the non-iterable!
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
Is it possible to implement a while loop by for loop? while in for
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? mathmatican's special
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. very last end
Use pip or original install command to make it!