· 6 years ago · May 09, 2019, 04:10 AM
1# Question 1 and comments about your function go here
2def positionQueries(data, order):
3 # takes 2 lists as input such that the first list named data contains a list of unsorted integers and the second
4 # list named order contains position queries.
5
6 # report bad input by default
7 res = 'bad input'
8
9 if len(data) > len(order):
10 sData = sorted(data)
11 newList = list()
12
13 i = 0
14 while i < len(order):
15 newList.append(sData[order[i] - 1])
16 i += 1
17 res = newList
18
19 return res
20
21
22# Question 2 and comments about your function go here
23def theRoundTrip(inst):
24 # takes all the movements the robot made in a day as input and output True of bool type if the robot returned to
25 # its starting position after its journey. Otherwise return False
26 u = 0
27 r = 0
28 illegalParam = 0
29
30 # count all params, and justify the position
31 for i in inst:
32 if i is 'U':
33 u += 1
34 elif i is 'D':
35 u -= 1
36 elif i is 'R':
37 r += 1
38 elif i is 'L':
39 r -= 1
40 else:
41 illegalParam += 1
42
43 res = u == 0 and r == 0
44
45 # report bad input iff any illegal param present
46 if illegalParam > 0:
47 res = 'bad input'
48 return res
49
50
51# Question 3 and comments about your function go here
52def isSecretKey(sub_keys, secret_key):
53 n = len(sub_keys)
54 # checking for every pair if their sum leads to secret_key
55 for i in range(n):
56 for j in range(i + 1, n):
57 if (sub_keys[i] + sub_keys[j]) == secret_key:
58 return True
59
60 return False
61
62
63# Question 4 and comments about your function go here
64def isAlienLanguage(sentense):
65 # separate each word by only one space
66 tokens = sentense.lower().split(" ")
67 res = True
68
69 # justify it is an alien word or not
70 def isAlienWord(word):
71 return word[0] is word[-1]
72
73 # justify the first token is alien word or not recursively
74 if len(tokens) > 0 and isAlienWord(tokens[0]) is False:
75 res = False
76 elif len(tokens) > 0:
77 del tokens[0]
78 res = isAlienWord(tokens)
79
80 return res
81
82
83# Question 5 Combining your answers
84def main():
85 results = list()
86 results.append(positionQueries([1, 2, 5, 0, 3], [1, 2]))
87 results.append(theRoundTrip(['U', 'R', 'L', 'D']))
88 results.append(isSecretKey([1, 2, 6, 7, 9], 3))
89 results.append(isAlienLanguage("aabbbaa mom"))
90 for i in results:
91 print(i)
92
93
94# Clean entry point to your code
95if __name__ == '__main__':
96 main()