solution_114a.py

#!/usr/bin/python3
# ==================================================================
# day of the week hash demo
# convert day string to day-of-the-week number
# ==================================================================

import random
import sys
import user_interface as ui

# ---- days of the week dictionary (string:day-of-the-week)

days = { 'Monday':1, 'Tuesday':2, 'Wednesday':3, 'Thursday':4,
         'Friday':5, 'Saturday':6, 'Sunday':7 }

# ---- aphabet dictionary (character:position-in-alphabet)

abc = { 'A':1,  'B':2,  'C':3,  'D':4,  'E':5,  'F':6,
        'g':7,  'H':8,  'I':9,  'J':10, 'K':11, 'L':12,
        'M':13, 'N':14, 'O':15, 'P':16, 'Q':17, 'R':18,
        'S':19, 'T':20, 'U':21, 'V':22, 'W':23, 'X':24,
        'Y':25, 'Z':26 }

# ------------------------------------------------------------------
# ---- hash maximum value
# ------------------------------------------------------------------

def hash_max():

    max = 0

    for day in days.keys():
        h = abc[day[0].upper()] + (abc[day[1].upper()] * 2)
        if h > max:
            max = h

    return max

# ------------------------------------------------------------------
# ---- create a hash table based on days-of-the-week
# ------------------------------------------------------------------

def create_hash_table(max):

    ht = [None] * max

    for s in list(days.keys()):
        hash = abc[s[0].upper()] + (abc[s[1].upper()] * 2)
        ht[hash] = days[s] 

    return ht 

# ------------------------------------------------------------------
# ---- return a day's hash value
# ------------------------------------------------------------------

def hash(day,ht):

    # ---- calculate hash

    h = abc[day[0].upper()] + (abc[day[1].upper()] * 2)

    # ---- check if hash is out of bounds

    if h >= len(ht):
        print('internal error - hash table size exceeded - ({h})')
        sys.quit() 

    # ---- return

    return ht[h]

# ------------------------------------------------------------------

if __name__ == '__main__':

    print()

    # ---- maximum hash value

    hmax = hash_max()

    print(f'hash max: {hmax}')

    # ---- alphabet dictionary

    print()
    for s in days:
        c = s[0].upper()
        print(f'abc[{c}] = {abc[c]}')

    # -- hash table

    ht = create_hash_table(hmax+1)

    print()
    print('hash table')
    print(f'size: {len(ht)}')
    for i,v in enumerate(ht):
       if v is not None:
           print(f'hash: ht[{i}] = {v}')

    # ---- process user input

    while(True):

        print()
        s = ui.get_user_input('Enter a test count: ')
        if not s:
            sys.exit()

        tf,cnt = ui.is_integer(s)

        if tf is not True:
            print(f'bad integer entered ({s}')
            continue

        if cnt  < 1 or cnt > 100:
            print(f'bad count entered {cnt} (limit 1 to 100)')
            continue

        break

    # ---- create random list of days

    k = list(days.keys())

    rdays = []

    for _ in range(cnt):
        rdays.append(random.choice(k))

    # ---- get hashed value

    print()

    for d in rdays:
        v = hash(d,ht)
        print(f'random-day = {d:9}  v = {v} (day-of-the-week)')

    # --------------------------------------------------------------
    # ---- now that the testing has been none
    # ---- let's collect statistics
    # --------------------------------------------------------------