Skip to main content

Magnificent demos in Python.

Project description

Un Fin De Semana Especial

Magnificent demos in python.

before we start...

WARNING:

Don't!

Ever!

Dream!

About!

Using!

These!

Code!

In!

Work!

walk through the indices

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 

ancient sorting algorithm

Abacus is a really old thing for us. Well, sometimes ancient gives us new ideas.
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!

Daily Prophet

If you want to see what a list looks like when append a thing, or insert a thing, or pop a thing?
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.

code and the key of secrets

If you want to have a map that uses list or set as key, is it possible?

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.

Iterating through the non-iterable!

When you want to iterate through an integer by indices like lists and strings, how?
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

while in for

Is it possible to implement a while loop by for loop?

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!

mathmatican's special

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...

very last end

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ufdse-0.0.2.tar.gz (5.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page