Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self,value=None,next=None):
- self.value=value
- self.next=next
- def print_list(self):
- node=self
- while node:
- print(node.value)
- node=node.next
- class LinkedList:
- def __init__(self, head):
- self.head = head
- def print_list(self):
- # Write a function that returns a LinkedList given a Python list as input.
- # [4,1,5] -> (4) -> (1) -> (5) return the head of that list.
- # i,j,k are reserved for idices. E.g. for i in range(), for j in range()
- def linkedlist(lis):
- head=Node(0,None)
- temp=head
- for num in lis:
- temp.next=Node(num,None)
- temp=temp.next
- return head.next
- lis=[1,2,3]
- myList = linkedlist(lis)
- myList.print_list()
- # Homework.
- # Write your own stack/queue using LinkedList nodes.
- # Deque (doubly ended queue).
- # Given a list of N (x,y) coordiantes, return the k farthest from the origin.
- #heap_>until k elem_>just post smallest one
- import heapq
- def get_k_farthest(coordindates, k):
- heap=[]
- for x,y in coordindates:
- heapq.heappush(heap,((x**2+y**2),(x,y)))#N log k N-> num of elem, k ->height
- if len(heap)>k:
- heapq.heappop(heap)
- top_k=[]
- for i in range(len(heap)):
- top_k.append(heap[i][1])
- return top_k
- # O ( N log N)
- def dareks_k_farthest(coordinates,k):
- heap = []
- # O(n log n)
- for x,y in coordinates: # O(n)
- heapq.heappush(heap, (-(x**2+y**2),(x,y))) # O(log n)
- top_k = []
- # k log (n)
- for i in range(k): # O(k)
- dist, coord = heapq.heappop(heap) # O(log N)
- top_k.append(coord)
- return top_k
- # Given a list of numbers sort it.
- # If a number is divisible by 10, it should have highest prioriy.
- # To break ties, then you should take the larger number.
- # [900, 20, 1, 990,70,400,5,44] - input
- # [1, 5, 44, 20,,70, 400,900,990] - output
- # [20,900,
- # i
- def sortNumsLambda(nums):
- nums.sort(key = lambda num: (num % 10 == 0, num))
- def numKey(num):
- return (num % 10 == 0, num)
- def sortNums(nums):
- nums.sort(key = numKey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement