Skip to content

Instantly share code, notes, and snippets.

@heyalexej
Created March 22, 2018 17:16
Show Gist options
  • Save heyalexej/6465774d304112358f02c39657b70a4c to your computer and use it in GitHub Desktop.
Save heyalexej/6465774d304112358f02c39657b70a4c to your computer and use it in GitHub Desktop.
goofing around finding a cool number for a backup SIM
#!/usr/bin/env python3.6
from string import ascii_lowercase as letters
import json, _jsonnet, re, pickle
from requests import get
import spacy
nlp = spacy.load('en')
def letter_combinations(digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
num_to_chars = {
'0': [''],
'1': [''],
'2': ['a', 'b', 'c'],
'3': ['d', 'e', 'f'],
'4': ['g', 'h', 'i'],
'5': ['j', 'k', 'l'],
'6': ['m', 'n', 'o'],
'7': ['p', 'q', 'r', 's'],
'8': ['t', 'u', 'v'],
'9': ['w', 'x', 'y', 'z'],
}
result = num_to_chars[digits[0]]
for i in range(1, len(digits)):
curr_result = []
chars = num_to_chars[digits[i]]
for r in result:
for char in chars:
curr_result.append(r + char)
result = curr_result
return result
def dump_data(data, path):
"""
stores an object in a pickle
"""
try:
pickle.dump(data, open(path, 'wb'))
print('dumped data')
except Exception as e:
print('exception', e)
def load_data(path: str) -> str:
"""
reads an object from a pickle.
"""
try:
data = pickle.load(open(path, 'rb'))
print('loaded data from disk')
return data
except Exception as e:
print('exception', e)
def easynum(num: str) -> str:
"""
checks for some types of memorable numbers
https://www.slideshare.net/cntryrckr69/what-makes-a-number-memorable
"""
if len(set(num)) <= 2: # common characters eg. 1114111
return "common"
if num == num[::-1]: # it's the same backwards eg. 1234321
return "a palindrome"
if all(x <= y for x, y in zip(list(num), list(num)[1:])): # increasing, e.g. 123456789
return "increasing"
if all(x >= y for x, y in zip(list(num), list(num)[1:])): # decreasing
return "decreasing"
def fetch_data():
"""
fetches currently available numbers at AIS
transforms the js object into a dict
extracts and returns only the numbers
"""
url = 'https://store.ais.co.th/en/sim-card/sim-net-marathon/sim-net-marathon-2500-baht-4-mbps-12-months.html'
r = get(url)
raw_body = r.text
# fugly fugly regex
find_bundle = re.search(r'^\s+var bundle = new Product\.Bundle\((.*)\);$', raw_body, re.M)
bundle = find_bundle.group(1)
# from js object
js_obj = bundle
# to python object
py_obj = json.loads(_jsonnet.evaluate_snippet('snippet', js_obj))
nums = []
for i in py_obj['options']['56']['selections'].items():
number = i[1]['name']
nums.append(number)
print('fetched {} numbers'.format(len(nums)))
return nums
def main():
data_path = '/tmp/nums.pickle'
nums = load_data(data_path)
if nums is not None:
len_nums = len(nums)
print(f'checking {len_nums} AIS numbers')
else:
nums = fetch_data()
dump_data(nums, data_path)
cuts = range(0,5)
for cut in cuts:
for num in nums:
fmtnum = f'{num[0:3]} {num[3:6]}-{num[6:]}'
truncnumf = num[cut:]
truncnuml = num[:-cut]
check_easyf = easynum(truncnumf)
no_one_zero = str.maketrans('', '', '01')
strpnum = num.translate(no_one_zero)
check_word = letter_combinations(num)
if check_word:
word = [w for w in check_word if w in nlp.vocab]
if word:
print(f'{fmtnum} contains the word {word} on the keypad')
if check_easyf:
print(f'{fmtnum} is {check_easyf} [{truncnumf}] with the first {cut} digits removed!')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment