-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneedle.py
More file actions
40 lines (38 loc) · 1.69 KB
/
needle.py
File metadata and controls
40 lines (38 loc) · 1.69 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
39
40
# ==========================================================
# Author: Edwardo S. Rivera
# Date: November 28,2014
# email: [email protected]
# Description: This program finds the index of an element in
# a given array. Part of the CODE 2040 API Challenge. Stage 2.
# ==========================================================
from json import loads,dumps
from requests import post
from mytoken import mytoken
# Use this for other language like C++ :). Not used here
def findNed(ned,array):
""" Returns the index where ned is inside the array"""
for i in range(0,len(array)):
if array[i] == ned:
return i
return -1 # Index not found: give a negative number
if __name__ == '__main__':
# My token
tok = mytoken()
# Do the post to the corresponding link (see reverse.py for more details)
response = post("http://challenge.code2040.org/api/haystack",data= dumps(tok))
# The content is in JSON format, so unpack and get the Python dictionary
info = loads(response.content)
# Get the needle and print it. Remember to convert unicode to ascii
ned = info['result']['needle'].encode('ascii')
print "Neddle is: ", ned
# Use map: pass a lambda function to create a list of python strings (not unicode).
l = map(lambda x: x.encode('ascii'),info['result']['haystack'])
print "List is: ", l
#Find the index where ned is (the challenge). Lists in Python provides a method for this (index)
ned_index = l.index(ned)
print "index is: ", ned_index
# Pack result. Send the post back to the server.
myresult = {'token':tok['token'],'needle':ned_index}
response2 = post("http://challenge.code2040.org/api/validateneedle",data= dumps(myresult))
# Print the server response
print loads(response2.content).get('result')