기본 콘텐츠로 건너뛰기

11월, 2019의 게시물 표시

programmers lv3 : Connecting islands commenting

def solution ( n , costs): answer = 0 V = set () for v1 , v2 , cost in costs: V.add(v1) V.add(v2) # collectiong total index and number of islands without duplication # used as unvisited set sortedCosts = sorted (costs , key = lambda x: x[ 2 ]) # make a sorted list, key : node connection, value : cost # it keeps the sequence of each elem in inner list, # but each inner list is sorted by lower cost # means this sort meet the minial cost condition # x:x[2] means, key value to sort by is x[2] item in inner list visited = set () visited.add(V.pop()) # put the first node index and thats it while V: for i in range ( len (sortedCosts)): v1 , v2 , cost = sortedCosts[i] # how to spead and assign multiple items in one line if v1 in visited and v2 in visited: # means same connection with lower cost has already been confirmed # if the connection

programmers lv2 : Pipe cutting laser

def mysolution (arrangement): arr = list (arrangement) stack = [] piece = 0 answer = 0 for i in range ( len (arr)): if arr[i] == '(' : stack.append(arr[i]) elif arr[i] == ')' : if stack[ len (stack) - 2 ] == '(' : piece += len (stack) - 1 else : piece += 1 stack.pop() print (piece) return answer def solution (arrangement): arr = enumerate (arrangement) stack = [] piece = 0 for idx , ele in arr : if ele == '(' : stack.append(idx) # ??? else : if stack[- 1 ] + 1 == idx: # when it is a match, laser # list[-1] means latest index in the list stack.pop() piece += len (stack) # exactly. its flawless now else : # when it is the end of a pipe stack.pop() piece += 1 retur

programmers lv1 : un-finished participant

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 particip

Word reversing Iterator implemented by Generator(yield)

def reverse (data): for index in range ( len (data)- 1 , - 1 , - 1 ): # repeat as many as the length of data, # decrement -1 each time till the index becomes -1 #since index starts with 0 yield data[index] for char in reverse( 'gold' ): print (char) Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called on it, the generator resumes where it left off (it remembers all the data values and which statement was last executed). An example shows that generators can be trivially easy to create. Anything that can be done with generators can also be done with class-based iterators as described in the previous section. What makes generators so compact is that the __iter__() and __next__() methods are created automatically. Another key feature is that the local variables and execution state are automatically saved

Iterator implemented by class

class Reverse: def __init__ ( self , data): self .data = data self .index = len (data) def __iter__ ( self ): return self def __next__ ( self ): if self .index == 0 : raise StopIteration self .index = self .index - 1 return self .data[ self .index] rev = Reverse( 'spam' ) iter (rev) for char in rev: print (char)

Code of the day

Hello no one, since technically no one is reading my post but myself...then let's rephrase, hi me! I have been studying python to prepare for a programming competition. and I found some beautiful code. wanted to remember, I am writing down them here. >>> [( x , y ) for x in [ 1 , 2 , 3 ] for y in [ 3 , 1 , 4 ] if x != y ] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] is same as >>> combs = [] >>> for x in [ 1 , 2 , 3 ]: ... for y in [ 3 , 1 , 4 ]: ... if x != y : ... combs . append (( x , y )) ... >>> combs [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]