Create a gist now

Instantly share code, notes, and snippets.

anonymous /call_route_cost.py
Created Nov 3, 2017

Embed
What would you like to do?
call_route_cost.py
import sys
from datetime import datetime
_end = '_end_'
"""
Usingthecarrierroutelistabove,the cost of calling +14152345678
wouldbe$0.09,as it matches with the prefixes +1415 and
+1415234 (longestmatch),
but not with+141524
"""
def phone_route_cost_check(filename, number):
open_file = open(filename, 'r')
data = open_file.readlines()
number_cost = []
for line in data:
for i in range(0, len(line)):
if line[i] == ',':
print(i)
number = line[0:i]
price = line[i+1:-1]
number_cost.append((number, price))
trie = make_dictionary_trie(number_cost)
open_file.close()
def make_dictionary_trie(all_tups):
trie_root = dict()
for tup in all_tups:
current_node = trie_root
number = tup[0]
price = tup[1]
for letter in number:
current_node = current_node.setdefault(letter, {})
current_node[_end] = price
return trie_root
def search_trie_for(trie, prefix):
current_node = trie
for letter in prefix:
if letter in current_node:
current_node = current_node[letter]
else:
return False
else:
if _end in current_node:
return current_node[_end]
else:
return False
def autocomplete_for(trie, prefix):
current_node = trie
for letter in prefix:
if letter in current_node:
current_node = current_node[letter]
else:
return False
phone_route_cost_check('route-costs-100.txt', '+14152345678')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment