mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
'''
|
||||
A binary search Tree
|
||||
'''
|
||||
from __future__ import print_function
|
||||
class Node:
|
||||
|
||||
def __init__(self, label, parent):
|
||||
@@ -66,8 +65,8 @@ class BinarySearchTree:
|
||||
else:
|
||||
parent_node.setRight(new_node)
|
||||
#Set parent to the new node
|
||||
new_node.setParent(parent_node)
|
||||
|
||||
new_node.setParent(parent_node)
|
||||
|
||||
def delete(self, label):
|
||||
if (not self.empty()):
|
||||
#Look for the node with that label
|
||||
@@ -92,7 +91,7 @@ class BinarySearchTree:
|
||||
self.delete(tmpNode.getLabel())
|
||||
#Assigns the value to the node to delete and keesp tree structure
|
||||
node.setLabel(tmpNode.getLabel())
|
||||
|
||||
|
||||
def getNode(self, label):
|
||||
curr_node = None
|
||||
#If the tree is not empty
|
||||
@@ -177,7 +176,7 @@ class BinarySearchTree:
|
||||
#Returns a list of nodes in the order that the users wants to
|
||||
return traversalFunction(self.root)
|
||||
|
||||
#Returns an string of all the nodes labels in the list
|
||||
#Returns an string of all the nodes labels in the list
|
||||
#In Order Traversal
|
||||
def __str__(self):
|
||||
list = self.__InOrderTraversal(self.root)
|
||||
@@ -203,7 +202,7 @@ def testBinarySearchTree():
|
||||
/ \ \
|
||||
1 6 14
|
||||
/ \ /
|
||||
4 7 13
|
||||
4 7 13
|
||||
'''
|
||||
|
||||
r'''
|
||||
@@ -236,11 +235,11 @@ def testBinarySearchTree():
|
||||
print("The label -1 exists")
|
||||
else:
|
||||
print("The label -1 doesn't exist")
|
||||
|
||||
|
||||
if(not t.empty()):
|
||||
print(("Max Value: ", t.getMax().getLabel()))
|
||||
print(("Min Value: ", t.getMin().getLabel()))
|
||||
|
||||
|
||||
t.delete(13)
|
||||
t.delete(10)
|
||||
t.delete(8)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from __future__ import print_function
|
||||
class FenwickTree:
|
||||
|
||||
def __init__(self, SIZE): # create fenwick tree with size SIZE
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from __future__ import print_function
|
||||
import math
|
||||
|
||||
class SegmentTree:
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from __future__ import print_function
|
||||
import math
|
||||
|
||||
class SegmentTree:
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
from __future__ import print_function, division
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
#This heap class start from here.
|
||||
# This heap class start from here.
|
||||
class Heap:
|
||||
def __init__(self): #Default constructor of heap class.
|
||||
def __init__(self): # Default constructor of heap class.
|
||||
self.h = []
|
||||
self.currsize = 0
|
||||
|
||||
@@ -79,7 +72,7 @@ class Heap:
|
||||
print(self.h)
|
||||
|
||||
def main():
|
||||
l = list(map(int, raw_input().split()))
|
||||
l = list(map(int, input().split()))
|
||||
h = Heap()
|
||||
h.buildHeap(l)
|
||||
h.heapSort()
|
||||
|
||||
@@ -4,14 +4,13 @@
|
||||
- Each link references the next link and the previous one.
|
||||
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
|
||||
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent'''
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
class LinkedList: #making main class named linked list
|
||||
def __init__(self):
|
||||
self.head = None
|
||||
self.tail = None
|
||||
|
||||
|
||||
def insertHead(self, x):
|
||||
newLink = Link(x) #Create a new link with a value attached to it
|
||||
if(self.isEmpty() == True): #Set the first element added to be the tail
|
||||
@@ -20,52 +19,52 @@ class LinkedList: #making main class named linked list
|
||||
self.head.previous = newLink # newLink <-- currenthead(head)
|
||||
newLink.next = self.head # newLink <--> currenthead(head)
|
||||
self.head = newLink # newLink(head) <--> oldhead
|
||||
|
||||
|
||||
def deleteHead(self):
|
||||
temp = self.head
|
||||
self.head = self.head.next # oldHead <--> 2ndElement(head)
|
||||
self.head = self.head.next # oldHead <--> 2ndElement(head)
|
||||
self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed
|
||||
if(self.head is None):
|
||||
self.tail = None #if empty linked list
|
||||
return temp
|
||||
|
||||
|
||||
def insertTail(self, x):
|
||||
newLink = Link(x)
|
||||
newLink.next = None # currentTail(tail) newLink -->
|
||||
self.tail.next = newLink # currentTail(tail) --> newLink -->
|
||||
newLink.previous = self.tail #currentTail(tail) <--> newLink -->
|
||||
self.tail = newLink # oldTail <--> newLink(tail) -->
|
||||
|
||||
|
||||
def deleteTail(self):
|
||||
temp = self.tail
|
||||
self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None
|
||||
self.tail.next = None # 2ndlast(tail) --> None
|
||||
return temp
|
||||
|
||||
|
||||
def delete(self, x):
|
||||
current = self.head
|
||||
|
||||
|
||||
while(current.value != x): # Find the position to delete
|
||||
current = current.next
|
||||
|
||||
|
||||
if(current == self.head):
|
||||
self.deleteHead()
|
||||
|
||||
|
||||
elif(current == self.tail):
|
||||
self.deleteTail()
|
||||
|
||||
|
||||
else: #Before: 1 <--> 2(current) <--> 3
|
||||
current.previous.next = current.next # 1 --> 3
|
||||
current.next.previous = current.previous # 1 <--> 3
|
||||
|
||||
|
||||
def isEmpty(self): #Will return True if the list is empty
|
||||
return(self.head is None)
|
||||
|
||||
|
||||
def display(self): #Prints contents of the list
|
||||
current = self.head
|
||||
while(current != None):
|
||||
current.displayLink()
|
||||
current = current.next
|
||||
current = current.next
|
||||
print()
|
||||
|
||||
class Link:
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
class Node: # create a Node
|
||||
def __init__(self, data):
|
||||
self.data = data # given data
|
||||
@@ -10,7 +7,7 @@ class Node: # create a Node
|
||||
class Linked_List:
|
||||
def __init__(self):
|
||||
self.Head = None # Initialize Head to None
|
||||
|
||||
|
||||
def insert_tail(self, data):
|
||||
if(self.Head is None): self.insert_head(data) #If this is first node, call insert_head
|
||||
else:
|
||||
@@ -37,7 +34,7 @@ class Linked_List:
|
||||
self.Head = self.Head.next
|
||||
temp.next = None
|
||||
return temp
|
||||
|
||||
|
||||
def delete_tail(self): # delete from tail
|
||||
tamp = self.Head
|
||||
if self.Head != None:
|
||||
@@ -46,7 +43,7 @@ class Linked_List:
|
||||
else:
|
||||
while tamp.next.next is not None: # find the 2nd last element
|
||||
tamp = tamp.next
|
||||
tamp.next, tamp = None, tamp.next #(2nd last element).next = None and tamp = last element
|
||||
tamp.next, tamp = None, tamp.next #(2nd last element).next = None and tamp = last element
|
||||
return tamp
|
||||
|
||||
def isEmpty(self):
|
||||
@@ -79,7 +76,7 @@ def main():
|
||||
print("\nPrint List : ")
|
||||
A.printList()
|
||||
print("\nInserting 1st at Tail")
|
||||
a3=input()
|
||||
a3=input()
|
||||
A.insert_tail(a3)
|
||||
print("Inserting 2nd at Tail")
|
||||
a4=input()
|
||||
@@ -96,6 +93,6 @@ def main():
|
||||
A.reverse()
|
||||
print("\nPrint List : ")
|
||||
A.printList()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from __future__ import print_function
|
||||
# Python code to demonstrate working of
|
||||
# extend(), extendleft(), rotate(), reverse()
|
||||
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
from __future__ import print_function
|
||||
from __future__ import absolute_import
|
||||
|
||||
from .stack import Stack
|
||||
|
||||
__author__ = 'Omkar Pathak'
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import print_function
|
||||
from __future__ import absolute_import
|
||||
import string
|
||||
|
||||
from .stack import Stack
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
from __future__ import print_function
|
||||
# Function to print element and NGE pair for all elements of list
|
||||
def printNGE(arr):
|
||||
|
||||
|
||||
for i in range(0, len(arr), 1):
|
||||
|
||||
|
||||
next = -1
|
||||
for j in range(i+1, len(arr), 1):
|
||||
if arr[i] < arr[j]:
|
||||
next = arr[j]
|
||||
break
|
||||
|
||||
|
||||
print(str(arr[i]) + " -- " + str(next))
|
||||
|
||||
|
||||
# Driver program to test above function
|
||||
arr = [11,13,21,3]
|
||||
printNGE(arr)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from __future__ import print_function
|
||||
__author__ = 'Omkar Pathak'
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ The span Si of the stock's price on a given day i is defined as the maximum
|
||||
number of consecutive days just before the given day, for which the price of the stock
|
||||
on the current day is less than or equal to its price on the given day.
|
||||
'''
|
||||
from __future__ import print_function
|
||||
def calculateSpan(price, S):
|
||||
|
||||
n = len(price)
|
||||
|
||||
Reference in New Issue
Block a user