-
Notifications
You must be signed in to change notification settings - Fork 0
/
needle.py
53 lines (38 loc) · 1.47 KB
/
needle.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
52
53
import requests
# Step 3 of CODE2040 API Challenge
api_token = "dc9daf749ac1b14787b989fe1b97d42c"
"""
Retrieves the needle and array from the haystack endpoint
String endpoint - the endpoint to retrieve the data from
Returns a dictionary with the needle and the haystack array
"""
def getNeedleAndArray(endpoint):
encoded_data = {'token' : api_token}
response = requests.post(endpoint, data=encoded_data)
response_dict = response.json()
return response_dict
"""
Finds the index of the input needle in the haystack
String needle - the needle we need to find
List haystack - a list of strings that we need to search through
Returns the index of the needle within the haystack
"""
def findNeedleIndex(needle, haystack):
index = None
if (needle in haystack):
index = haystack.index(needle)
return index
def main():
haystack_endpoint = "http://challenge.code2040.org/api/haystack"
validation_endpoint = "http://challenge.code2040.org/api/haystack/validate"
# Retrieve the haystack and needle from the API endpoint
endpoint_dict = getNeedleAndArray(haystack_endpoint)
needle = endpoint_dict.get("needle")
haystack = endpoint_dict.get("haystack")
# Find the index of the needle in the haystack
index = findNeedleIndex(needle, haystack)
json = {"token" : api_token, "needle" : index}
# Send that index to the validation endpoint
r = requests.post(validation_endpoint, data=json)
if __name__ == "__main__":
main()