-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkyu6_autocomplete_yay.py
51 lines (36 loc) · 1.84 KB
/
kyu6_autocomplete_yay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
replace_values = ['±', '§', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*',
'(', ')', '-', '_', '+', '=', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9']
def replace_special_characters(word):
for item in replace_values:
word.replace(item, '')
return word
def autocomplete(input_, dictionary):
""" It's time to create an autocomplete function! Yay!
The autocomplete function will take in an input string and a dictionary array and return the values from the
dictionary that start with the input string. If there are more than 5 matches, restrict your output to the first
5 results. If there are no matches, return an empty array;
For this kata, the dictionary will always be a valid array of strings. Please return all results in the
order given in the dictionary, even if they're not always alphabetical. The search should NOT be case sensitive,
but the case of the word should be preserved when it's returned;
For example, "Apple" and "airport" would both return for an input of 'a'. However, they should return as "Apple"
and "airport" in their original cases;
Examples:
autocomplete('ai', ['airplane','airport','apple','ball']) = ['airplane','airport']
Args:
input_ (str): word to complete;
dictionary (list):
Returns:
list: first 5 items;
"""
if input_ is None:
return dictionary[0:5]
cleared_dictionary = []
for word in dictionary:
cleared_dictionary.append(replace_special_characters(word))
result, check = [], replace_special_characters(input_.lower())
if input_ is not None:
for word, cleared_word in zip(dictionary, cleared_dictionary):
if check == cleared_word[0:len(check)].lower():
result.append(word)
return result[0:5]