-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.py
49 lines (34 loc) · 1.38 KB
/
API.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
# STAGE 1: Reverse a string
# I suggested two ways to solve this problem:
# First way:
def getString(x):
y=[]
for item in range(len(x)):
y.append(x[len(x)-item-1])
return ''.join(y)
# Second way:
def GetString(x):
return x[::-1]
# STAGE II: Needle in a haystack
# The task is to to tell the API where the needle is in the array.
def FindNeedle():
for item in dico["haystack"]: # I assumed that the dictionary variable given by the API is dico
if item==dico["needle"]:
return (dico["haystack"]).index(item)
# STAGE III: Prefix
# The task is to to return an array containing only the strings that do not start with the prefix given in the dictionary.
def NotInArray():
y=[]
(dico["prefix"])=(dico["prefix"]).lower() # I considered that the prefix with lower or uppercase are equivalent.
# I assumed that the dictionary variable given by the API is dico
for item in dico["array"]:
if item[:len(dico["prefix"])]!=dico["prefix"]:
y.append(item)
return y
# STAGE IV: The dating game
# The job is to add the interval to the date formatted as an ISO 8601 datestamp
from datetime import datetime
from datetime import timedelta
def DatingGame():
dt = datetime.strptime((dico["datestamp"]), "%Y-%m-%d %H:%M")
return str(dt+timedelta(seconds=(dico["interval"])))