Not what we’re shooting for, at this stage. We need to ideate and test our assumptions as we go. I was convinced (even though assuming) that int
would be less overhead than str
just because of the nature of character data. Python uses Unicode so each character is at least two bytes. I also assumed that the system has overhead added to the data, regardless of type. Just seems integers would be better managed in memory than strings in terms of resource load.
Did get around the shoddy analysis of my earlier stated assumption. All it took in the end was brute force.
Given: alpha
is in session memory.
def encode(dic):
y = []
for k in alpha:
v = alpha[k]
y.append(int("0b1" + "".join(map(lambda x: '0' if x == ' ' else '1', [*v])), 2))
return dict(zip([*"abcdefghijklmnopqrstuvwxyz".upper()], y))
def decode(y):
z = bin(beta[y.upper()])
return "".join([*map(lambda x: y.upper() if x == '1' else ' ', [*z[3:]])])
def user_initials(f, l):
for i in range(7):
print (decode(f)[i*7:i*7+7], decode(l)[i*7:i*7+7])
beta = encode(alpha) # run once global
user_initials("c", "d")
CCC DDDD
C C D D
C D D
C D D
C D D
C C D D
CCC DDDD
Did you spot the brute force?
Looks like dictionaries have an overhead of 1176 bytes. To measure the true memory consumption, we would need to sum up all the items. This brings us back to @8-bit-gaming’s analysis of 98 bytes for the strings. and 32 bytes for the integers. It also supports my assumption that Unicode takes at least two bytes per character.
As it stands, our alpha table needs 4KB and our beta table needs 2KB when we include overhead.
Now that we have an implementation that produces an encoded dictionary from the alpha
lookup table, all we really need to save in our production code is the beta
dictionary; and park alpha
.
We would also only need the decode()
function.
beta = {'A': 634711601914436, 'B': 1093070503355000, 'C': 811594335396408, 'D': 1093070394303096, 'E': 1110524159860860, 'F': 1110524159860800, 'G': 811594461291064, 'H': 864372093166148, 'I': 1108861805398140, 'J': 1108861806454832, 'K': 864512694821956, 'L': 846641268531324, 'M': 865757136233028, 'N': 865475799097924, 'O': 811595417592376, 'P': 1093070503288896, 'Q': 811595417592884, 'R': 1093070503552068, 'S': 811594317636152, 'T': 1108861805398032, 'U': 864371975725624, 'V': 864371975722000, 'W': 864372009940548, 'X': 864364350022212, 'Y': 863395834497040, 'Z': 1108447341322364}
def user_initials(first, last):
def decode(y):
z = bin(beta[y.upper()])
return "".join([*map(lambda x: y.upper() if x == '1' else "\u0020", [*z[3:]])])
f, l = decode(first), decode(last)
for i in range(7):
j = i * 7
k = j + 7
print (f[j:k], l[j:k])
user_initials("c", "d")
Note: I use IDLE for all my coding. This does not work in Codebyte. It collapses side by side space characters.
I’m also content to post raw code. If the person wants to run it, then copy it and paste it wherever the user has a session environment going. Finding the Codebyte thing a little underwhelming and taxing at the same time. Give me code, not screen candy.
https://replit.com/@mtf/Two-Place-Initials