Search

Obfuscation & The Hidden Gems of Python.

''' Did you know?
 
A few hack, cracks and obfuscated examples, which may actually lead to less confusion and a greater knowledge of why things work and how. However, these examples have a purpose, and should not be taken, in any way as correct or just another way to achieve a common goal. They are purposely, in some cases, convoluted.
 
I. String, things, hacking, and unpacking '''
 
import string
s = ‘The quick brown fox jumps over the lazy dog”
 
The string above (s) is called a pangram. This means it contains every letter of the alphabet at least once. Let’s  Prove that’s true.
 
Goal: To extract the alphabet in lexicographical form from the pangram below.
 
alphabet = string.ascii_lowercase # → ‘abcdefghijklmnopqrstuvwxyz’
 
#Algorithm
1. We begin by calling method lower() on our string to create all lowercase letters (A small amount of normalization).
 
s = s.lower() # --> ‘the quick brown fox jumps over the lazy dog” print(s)
 
2. The second thing we do is take care of duplicates. We can achieve this quickly 
by throwing the sequence to a set.
 
Sets by definition do not contain duplicates.
 
 s --> set([' ', 't', 'a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'm', 'l', 'o', 'n', 'q', 'p', 's', 'r', 'u', # 't', 'w', 'v', 'y', 'x', 'z'])
 
You might think that the logical next step would be to sort.
 
STOP  = list(set(s.lower())).sort() print (s ) 
 
What happened to our list? It's annihilated, why? Quite simple, lists are mutable and method sort does not return a list.
The values of the list are sorted in place. A new list is unnecessary, unlike a string.
So what doses sort return? Well nothing
 
really, which is exactly that, None. 
If you don't explicitly return a value from a method in python, none is returned....
 
Solution:
 
s = ‘The quick brown fox jumps over the lazy dog” s = ''.join(sorted(list(set(s.lower())))).lstrip() print(s)
'abcdefghijklmnopqrstuvwxyz'
s = ''.join(sorted(list(set(s.lower())))).lstrip()
print(s)
'abcdefghijklmnopqrstuvwxyz'
 
 
Until the next convolution.  
 
[chr(x) for x in range(97, 123)]
.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.. 
 
Mike 
 
if (isMyPost) { }