import collections participant = ["leo", "kiki", "eden"] completion = ["eden", "kiki"] def solution(participant, completion): answer = collections.Counter(participant) - collections.Counter(completion) # print(collections.Counter(participant)) # without listing, it looks like below # Counter({'leo': 1, 'kiki': 1, 'eden': 1}) # print(list(collections.Counter(participant))) # with listing # ['leo', 'kiki', 'eden'] # list(obj) distributes each item in the obj and makes new list out of it # unlikely with posted answer, we know the list has only one key value pair # indexing it out is unnecessary return list(answer.keys())[0] def solution2(participant, completion): participant.sort() completion.sort() for i in range(len(completion)): if participant[i] != completion[i]: return participant[i] # it works unless unfinished participant is not the latest elem # if the loop is done and have not caught the latest answer, # simply return latest one return participant[len(participant) - 1] # def solution3(participant, completion):# participant.sort()# completion.sort()## participant = collections.deque(participant)# completion = collections.deque(completion)## for i in range(len(completion)):# print(str(i))# if participant[i] == completion[i]:# print("p : " + participant.popleft())# print("c : " + completion.popleft())# else:# return participant[i] # it works unless unfinished participant is not the latest elem # if the loop is done and have not caught the latest answer, # simply return latest one # return participant[len(participant) - 1] # deque had index error when i poplefted element. # had problem from second loop print(solution(participant, completion))
<HTML> <body> </body> <script> function Calculator() { let methods = { "-" : (a, b) => a - b, "+" : (a, b) => a + b }; //methods is an object which keeps key for operators //and value to return the actual operation values //each returns the result of operation that key(operator) does this.calculate = function (str){ //calculate is one element in the function Calculator //it takes the string and returns the value //in the function element list is delimeted by , not ; let split = str.split(" "), a = +split[0], op = split[1], b = split [2] if(!methods[op] || isNaN(a) || isNaN(b)) { return NaN; // error handling } return methods[op](a,b); } this.addMethod = function(name, func){ methods[name] = func; //this is how to add new key and ele to object } } let powerCalc = new Calculator; powerCalc.addMethod("*&
댓글
댓글 쓰기