-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefix.py
More file actions
38 lines (32 loc) · 1.46 KB
/
prefix.py
File metadata and controls
38 lines (32 loc) · 1.46 KB
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
import requests
import json
def main():
"""
This file will be used to complete the fourth challenge for the Code2040 API Challenge
This challenge is asking me to return an array containing only the strings that do not start
with a prefix. We are given a prefix(string) and array of strings
"""
try:
# Initialize info to send post requests
url = 'http://challenge.code2040.org/api/prefix'
post_url = 'http://challenge.code2040.org/api/prefix/validate'
my_token = json.dumps({'token': '3de4e34b13ebc2912ab209a77aec363e'})
headers = {'content-type': 'application/json'}
my_list = []
# Get the dictionary that contain the prefix and array of strings
dictionary = requests.post(url, data=my_token, headers=headers).json()
prefix = dictionary['prefix']
array = dictionary['array']
# Search for words that do not start with the prefix, case insensitive
for index in range(0, len(array)):
if not array[index].lower().startswith(prefix.lower()):
my_list.append(array[index])
# Post the list back to the API
answer = json.dumps({'token': '3de4e34b13ebc2912ab209a77aec363e', 'array': my_list})
response = requests.post(post_url, data=answer, headers=headers)
print response.content
except Exception:
# TODO: add logic to handle possible exception types
raise
if __name__ == '__main__':
main()