python convert array to linked list

Solutions on MaxInterview for python convert array to linked list by the best coders in the world

showing results for - "python convert array to linked list"
Sofie
20 Jan 2020
1# credit to the Stack Overflow user in the source link
2
3linked_list = {'a':'b', 'b': 'c', 'c': 'd', 'd': None}
4def next_ll(state=['a']):
5  value = state[0]
6  if value is not None:
7    state[0] = linked_list[value]
8    return value
9
10[x for x in iter(next_ll, None)]
11>>> ['a', 'b', 'c', 'd']
Giuseppe
20 Aug 2016
1class MaxPriority():
2    def __init__(self):
3        self.heap = []
4        self.tempNumber = []
5        self.tempWord = []
6        self.counter = int(0)
7        self.maxsize = int(0)
8        self.size = int(0)
9
10    def root(self,index):
11        if index == 0:
12            return True
13        return False
14    
15    def leaf(self,index):
16        if index >= self.size//2 and index <= self.size:
17            return True
18        return False
19
20    def getParent(self, index):
21        if self.root(index):
22            return None
23        return (index-1)//2
24
25    def leftChild(self,index):
26        if self.leaf(index):
27            return None
28        return (index*2)+1
29
30    def rightChild(self,index):
31        if self.leaf(index):
32            return None
33        elif (index*2)+2 >= self.size:
34                return None
35        return (index*2)+2
36
37    def swap(self,src,dest):
38        self.heap[src], self.heap[dest] = self.heap[dest], self.heap[src]
39
40    def heapifyUp(self):
41        temp = self.size-1
42        if self.size>1:
43            while(self.heap[self.getParent(temp)] < self.heap[temp]):
44                self.swap(temp,self.getParent(temp))
45                temp = self.getParent(temp)
46                if self.getParent(temp) == None:
47                    break
48
49    def heapifyDown(self):
50        temp = 0
51        if self.size == 2:
52            if self.rightChild(temp) != None:
53                if self.heap[self.leftChild(temp)] > self.heap[self.rightChild(temp)]:
54                    self.swap(temp,self.leftChild(temp))
55                    temp = self.leftChild(temp)
56                else:
57                    self.swap(temp,self.rightChild(temp))
58                    temp = self.rightChild(temp)
59            else:
60                if self.heap[self.leftChild(temp)] > self.heap[temp]:
61                    self.swap(temp,self.leftChild(temp))
62        elif self.size>=3:
63            while(self.heap[temp] < self.heap[self.leftChild(temp)] or self.heap[temp] < self.heap[self.rightChild(temp)]):
64                if self.heap[self.leftChild(temp)] > self.heap[self.rightChild(temp)]:
65                    self.swap(temp,self.leftChild(temp))
66                    temp = self.leftChild(temp)
67                else:
68                    self.swap(temp,self.rightChild(temp))
69                    temp = self.rightChild(temp)
70                if self.leftChild(temp) == None or self.rightChild(temp) == None:
71                    break
72    
73    def add(self,word,number):
74        self.tempWord.append(word)
75        self.tempNumber.append(number)
76        self.size+=1
77        if self.size > self.maxsize:
78            self.heap.append(number)
79            self.maxsize+=1
80        else:
81            self.heap[self.size-1] = number
82        self.heapifyUp()
83
84    def delete(self):
85        self.heap[0] = self.heap[self.size-1]
86        self.size-=1
87        self.heapifyDown()
88
89    def selesaikan(self):
90        if self.size == 0:
91            print("Priority is empty !!")
92        elif self.size == 1:
93            print("Only one priority !!")
94        else:
95            self.counter=self.size
96            print("\n== URUTAN PRIORITAS TUGAS TOTO BERDASARKAN MAX HEAP ==")
97            for x in range (0,self.size):
98                for i in range(0,1):
99                    for j in range(0,self.counter):
100                        if(str(self.heap[i]) == str(self.tempNumber[j])):
101                            print("-> Prioritas " + str(x+1) + " = " + str(self.tempWord[j]))
102                self.delete()
103
104    def peek(self):
105        if self.size>0:
106            return self.heap[0]
107        return None
108
109tugas = MaxPriority()
110print("\n== MEMPRORITASKAN TUGAS TOTO BERDASARKAN MAX HEAP ==")
111n_data = int(input("-> Masukkan banyak data yang akan disusun : "))
112
113for i in range (0,n_data):
114    name= input("\n1) Nama tugas : ")
115    priorityy = int(input("2) Prioritas : "))
116    print("-> Data berhasil disimpan!!")
117    tugas.add(name,priorityy)
118
119tugas.selesaikan()
120
121
queries leading to this page
how to convert array to linked list pythonconvert array into a linked list pythonlinked list to arrayconvert linked list to array pconvert linked list to arraylsitlinked list to string pythonpython convert linked list to arrayhow to convert a list to a linked list pythonwhy to convert linked list to arraysconvert a linked list to a list pythonpython linked list to arrayconvert linked list to array in javahow to convert a linked list to an array in javaconverting list to linked list pythonhow to change a list to a linked list in pythonconvert array to linked listlinked list to listconvert a linked list into arrayconvert array to linked list javaarray to linked list pythonconvert list to linked list pythonconvert array into a linked listconvert array of nodes to linked list pyhtonconvert linkedlist to arrayconvert list into linked listconvert a linkedlist to list pythonarray to linkedlist pythonconverting a list to linked list in pythonlinked list to list pythonhow to turn a linked list into a list in pythonlinked list to array in c 2b 2bhow to convert a list into linked llistin pythonconvert linked list to array c 2b 2bhow to convert array to linked list in javaturn array into linked listcan i convert array to linked list in javaconvert linked list to list pythonhow to convert array to linked listhow to convert a linked list to a list in pythonlist to linked list pythonconverting array to linked list pythonturn array into linked list pythonpython convert array to linked listturn list into linked list pythonhow to make linked list into arrayconvert linked list to arrayjava convert array to linked listconvert array of nodes to linked listhow to convert linked list to list in pythonconvert a list to linked listarray to linked listconvert array of list to linked list pyhtonconvert linked list to array pythonconvert linked list to array javahow to convert list to linked list in pythonconvert array to linked list pythonlinked list to an arraypython list to linked listhow to make an array to linkedlist in javaload array into linked listlinked list converted to arrays python convert array to linked list