Created
November 12, 2019 01:45
-
-
Save alacret/2d9f672539c07d0d1f0e4517483a6c68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def extract_name(line): | |
start = line.find('<') | |
stop = line.find('>') | |
return line[start + 1:stop], line[:start] + line[stop + 1:] | |
def extract_phone(line): | |
""" | |
TA-1234123 +1-123-123-1233 | |
""" | |
plus_pos = line.find('+') | |
first_dash_pos = line.find('-', plus_pos) | |
phone = line[plus_pos + 1 : first_dash_pos] + line[first_dash_pos: first_dash_pos + 13] | |
new_line = line[:plus_pos] + line[plus_pos + len(phone) + 1:] | |
return phone, new_line | |
WHITELIST = ['.','-'] | |
def extract_address(line): | |
cleaned = [''] | |
prev = '' | |
for i in range(len(line)): | |
c = line[i] | |
if c.isalpha() or c.isnumeric() or c in WHITELIST: | |
cleaned.append(c) | |
if c == '_': | |
cleaned.append(' ') | |
if c == ' ' and cleaned[-1] != ' ': | |
cleaned.append(' ') | |
jointed = ''.join(cleaned).strip() | |
return jointed | |
def extract_data(line): | |
name, new_line = extract_name(line) | |
phone, new_line = extract_phone(new_line) | |
address = extract_address(new_line) | |
return phone, address, name | |
def search_number(notebook, num): | |
results = [] | |
for phone, name, address in notebook: | |
if phone == num: | |
results.append([phone,name,address]) | |
return results | |
def phone(notebook_raw, num): | |
lines = notebook_raw.split('\n') | |
notebook = [] | |
for line in lines: | |
phone, address, name = extract_data(line) | |
notebook.append([phone,name, address]) | |
results = search_number(notebook, num) | |
if len(results) == 0: | |
return "Error => Not found: " + num | |
if len(results) > 1: | |
return "Error => Too many people: " + num | |
phone, name, address = results[0] | |
return "Phone => %s, Name => %s, Address => %s" % (phone, name, address) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment