a use for my string maker

With a lot of help from Zed....

I finally got a grasp on this dictionary thing....

The hash will assign some crazy integer to a string....  the modulo will constrain it to a number within the realm of the number of items in my list.....

For testing my strings,  I found a use for my string maker....
this thing:



def create_variable_large(count, container):
    #a - odd
    #b - even
    var = "wp"
    var2 = "pw"
    foo = "gr"
    foo2 = "rg"
    bar = "te"
    bar2 = "et"
    spam = count
    while spam > 0:
        if spam < 10 and spam > 0:
            if spam % 2:
                var = var + 'b'
                var2 = var + 'a'
            else:
                var = var + 'a'
                var2 = var2 + 'b'
            spam -= 1
            container.set(var, var2)
        elif spam < 20 and spam >= 10:
         
            if spam % 2:
                foo = foo + 'c'
                foo2 = foo + 'd'
            else:
                foo = foo + 'd'
                foo2 = foo2 + 'c'
            spam -= 1
            container.set(foo, foo2)
        elif spam <= 30 and spam >= 20:
         
            if spam % 2:
                bar = bar + 'f'
                bar2 = bar2 + 'e'
            else:
                bar = bar + 'e'
                bar2 = bar2 + 'f'
            spam -= 1
            container.set(bar, bar2)
        else:
            spam -= 1

    return container


So I set my dictionary to 10.... modulated to 10....(thanks Zed!)   and then had my DLL print off to show me to which list each string was going to... so when it would run 'get()'  the while loop would show where it was going.... if my hash_key was 8.... the while loop ran to 8....  the string was going to 8.... and the string was deposited there by all the methods of the Dictionary class methods.

Also.....  the hash isn't magic... This also works... though I know it has major flaws... it works...
This part tripped me up for the longest time... all this method is doing is picking a list to decide which list is going to be a bucket to put the key/value pair in....

This all goes wonky(random part) for Dictionary's get().... but... that's another bit I have to sort out. Hash() while stays constant on one run... does change after it's been closed and reopened. I suppose that doesn't matter.  Just wanted to show that under all the mystical powerful smoke and mirrors... its just making a number between 0 and 255...  although hash will be more sticky and constant then randint. :)

def hash_key(self, key):
        # the dictionary is a doubly linked list, with a bunch of doubly linked lists inside it....
        # if the DLList(DLList()*30)  say,  it'll be magic=randint(0, 29)
        # the list it picks = bucket
        magic = randint(0, 255)

        return magic

Original:

  def hash_key(self, key):
        """Given a key this will create a number and then convert it to 
                an index for the aMap's buckets."""     
        ## this decides which list it will go into   that list = bucket....
        return hash(key) % self.map.count()


The <DLL> get() in dictionary's get_bucket()  and the <DLL> push() in  dictionary's set() are the only Doubly Linked methods being used. 

I used my string maker to make 30 sets....  30 different strings deposited in the 10 different DLLists... depending on the hash_keys....  Now, they aren't even ....  or well distributed,  but in later lessons we improve this dictionary and make it better.  This is just to give us a basic understanding on how they work and see how to implement them.

Little bit of magic I'm still trying to figure out is this bit node.value[0]:
We haven't used any list type stuff to manipulate so far... but now it's in this.... so wondering what purpose this is
serving.... More to do before I fully understand it, but I'm not banging my head on the wall anymore.

Oh, and this whole time,  I thought 'dump()' method was supposed to empty the Doubly linked list.... it's supposed to print it off.... No wonder I would watch Zed's videos and just scratch my head.  Like... how the hell is he dumping the list, and it's still there!!!  He's a fricking wizard!!! Oh...  I did it wrong. It's not supposed to empty the list... it's supposed to empty the contents onto the screen...."""Debugging function that dumps the contents of the list.""" Well hell....

Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

playing with color in powershell python