-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefix.py
31 lines (25 loc) · 1.03 KB
/
prefix.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
import requests
# Step 4 of the CODE2040 API Challenge
# I've decided that since these are just supposed to be quick Python
# scripts, I won't do all of the boilerplate and make a main method
# and helper functions
api_token = "dc9daf749ac1b14787b989fe1b97d42c"
challenge_endpoint = "http://challenge.code2040.org/api/prefix"
validation_endpoint = "http://challenge.code2040.org/api/prefix/validate"
ret_list = []
# Post token to endpoint
json = {"token" : api_token}
res = requests.post(challenge_endpoint, data=json)
json_dict = res.json();
# Get the prefix and the array from the json dictionary
prefix = json_dict.get("prefix")
prefix_len = len(prefix)
arr = json_dict.get("array")
# Get the elements that do not have the specified prefix in the array
for elt in arr:
# Compare the part of the element
if elt[:prefix_len] != prefix:
ret_list.append(elt)
# Validate your array by sending it back to the validation endpoint
ret_json = {"token" : api_token, "array" : ret_list}
finalResponse = requests.post(validation_endpoint, json=ret_json)