Skip to content

Commit a6d57bc

Browse files
authoredOct 13, 2021
Add files via upload
1 parent b234401 commit a6d57bc

11 files changed

+518
-0
lines changed
 

‎10_length_of_last_word_of_sentence.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def length_of_last_word(s):
2+
words = s.split()
3+
if len(words) == 0:
4+
return 0
5+
return len(words[-1])
6+
7+
print(length_of_last_word("i love you"))
8+
print(length_of_last_word("Python"))
9+
print(length_of_last_word(""))
10+
print(length_of_last_word(" "))

‎11_sum_binary_number.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Python program to add two binary numbers.
2+
3+
# Driver code
4+
# Declaring the variables
5+
a = "1101"
6+
b = "100"
7+
8+
# Calculating binary value using function
9+
sum = bin(int(a, 2) + int(b, 2))
10+
11+
# Printing result
12+
print(sum[2:])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Python program to count
2+
# ways to reach nth stair
3+
4+
# Recursive function to find
5+
# Nth fibonacci number
6+
def fib(n):
7+
if n <= 1:
8+
return n
9+
return fib(n-1) + fib(n-2)
10+
11+
# Returns no. of ways to
12+
# reach sth stair
13+
def countWays(s):
14+
return fib(s + 1)
15+
16+
# Driver program
17+
s = 4
18+
print("Number of ways = ")
19+
print(countWays(s))
20+
21+
# Contributed by Harshit Agrawal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#Represent a node of the singly linked list
2+
class Node:
3+
def __init__(self,data):
4+
self.data = data
5+
self.next = None
6+
7+
class RemoveDuplicate:
8+
#Represent the head and tail of the singly linked list
9+
def __init__(self):
10+
self.head = None;
11+
self.tail = None;
12+
#addNode() will add a new node to the list
13+
def addNode(self, data):
14+
#Create a new node
15+
newNode = Node(data);
16+
17+
#Checks if the list is empty
18+
if(self.head == None):
19+
#If list is empty, both head and tail will point to new node
20+
self.head = newNode;
21+
self.tail = newNode;
22+
else:
23+
#newNode will be added after tail such that tail's next will point to newNode
24+
self.tail.next = newNode;
25+
#newNode will become new tail of the list
26+
self.tail = newNode;
27+
28+
#removeDuplicate() will remove duplicate nodes from the list
29+
def removeDuplicate(self):
30+
#Node current will point to head
31+
current = self.head;
32+
index = None;
33+
temp = None;
34+
35+
if(self.head == None):
36+
return;
37+
else:
38+
while(current != None):
39+
#Node temp will point to previous node to index.
40+
temp = current;
41+
#Index will point to node next to current
42+
index = current.next;
43+
44+
while(index != None):
45+
#If current node's data is equal to index node's data
46+
if(current.data == index.data):
47+
#Here, index node is pointing to the node which is duplicate of current node
48+
#Skips the duplicate node by pointing to next node
49+
temp.next = index.next;
50+
else:
51+
#Temp will point to previous node of index.
52+
temp = index;
53+
index = index.next;
54+
current = current.next;
55+
56+
#display() will display all the nodes present in the list
57+
def display(self):
58+
#Node current will point to head
59+
current = self.head;
60+
if(self.head == None):
61+
print("List is empty");
62+
return;
63+
while(current != None):
64+
#Prints each node by incrementing pointer
65+
print(current.data);
66+
current = current.next;
67+
68+
69+
sList = RemoveDuplicate();
70+
71+
#Adds data to the list
72+
sList.addNode(1);
73+
sList.addNode(2);
74+
sList.addNode(3);
75+
sList.addNode(2);
76+
sList.addNode(2);
77+
sList.addNode(4);
78+
sList.addNode(1);
79+
80+
print("Originals list: ");
81+
sList.display();
82+
83+
#Removes duplicate nodes
84+
sList.removeDuplicate();
85+
86+
print("List after removing duplicates: ");
87+
sList.display();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#Represent a node of the singly linked list
2+
class Node:
3+
def __init__(self,data):
4+
self.data = data;
5+
self.next = None;
6+
7+
class RemoveDuplicate:
8+
#Represent the head and tail of the singly linked list
9+
def __init__(self):
10+
self.head = None;
11+
self.tail = None;
12+
#addNode() will add a new node to the list
13+
def addNode(self, data):
14+
#Create a new node
15+
newNode = Node(data);
16+
17+
#Checks if the list is empty
18+
if(self.head == None):
19+
#If list is empty, both head and tail will point to new node
20+
self.head = newNode;
21+
self.tail = newNode;
22+
else:
23+
#newNode will be added after tail such that tail's next will point to newNode
24+
self.tail.next = newNode;
25+
#newNode will become new tail of the list
26+
self.tail = newNode;
27+
#removeDuplicate() will remove duplicate nodes from the list
28+
def removeDuplicate(self):
29+
#Node current will point to head
30+
current = self.head;
31+
index = None;
32+
temp = None;
33+
34+
if(self.head == None):
35+
return;
36+
else:
37+
while(current != None):
38+
#Node temp will point to previous node to index.
39+
temp = current;
40+
#Index will point to node next to current
41+
index = current.next;
42+
43+
while(index != None):
44+
#If current node's data is equal to index node's data
45+
if(current.data == index.data):
46+
#Here, index node is pointing to the node which is duplicate of current node
47+
#Skips the duplicate node by pointing to next node
48+
temp.next = index.next;
49+
else:
50+
#Temp will point to previous node of index.
51+
temp = index;
52+
index = index.next;
53+
current = current.next;
54+
#display() will display all the nodes present in the list
55+
def display(self):
56+
#Node current will point to head
57+
current = self.head;
58+
if(self.head == None):
59+
print("List is empty");
60+
return;
61+
while(current != None):
62+
#Prints each node by incrementing pointer
63+
print(current.data);
64+
current = current.next;
65+
66+
67+
sList = RemoveDuplicate();
68+
69+
#Adds data to the list
70+
sList.addNode(1);
71+
sList.addNode(2);
72+
sList.addNode(3);
73+
sList.addNode(2);
74+
sList.addNode(2);
75+
sList.addNode(4);
76+
sList.addNode(1);
77+
78+
print("Originals list: ");
79+
sList.display();
80+
81+
#Removes duplicate nodes
82+
sList.removeDuplicate();
83+
84+
print("List after removing duplicates: ");
85+
sList.display();

‎15_merge_two_array_and_sort.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Python program to merge two unsorted lists
2+
# in sorted order
3+
# Function to merge array in sorted order
4+
def unsortedarray (a, b, res, n, m):
5+
# Sorting a[] and b[]
6+
a.sort()
7+
b.sort()
8+
# Merge two sorted arrays into res[]
9+
i, j, k = 0, 0, 0
10+
while (i < n and j < m):
11+
if (a[i] <= b[j]):
12+
res[k] = a[i]
13+
i += 1
14+
k += 1
15+
else:
16+
res[k] = b[j]
17+
j += 1
18+
k += 1
19+
while (i < n): # Merging remaining
20+
# elements of a[] (if any)
21+
res[k] = a[i]
22+
i += 1
23+
k += 1
24+
while (j < m): # Merging remaining
25+
# elements of b[] (if any)
26+
res[k] = b[j]
27+
j += 1
28+
k += 1
29+
# Driver code
30+
A=list()
31+
n=int(input("Enter the size of the First List ::"))
32+
print("Enter the Element of First List ::")
33+
for i in range(int(n)):
34+
k=int(input(""))
35+
A.append(k)
36+
B=list()
37+
m=int(input("Enter the size of the Second List ::"))
38+
print("Enter the Element of Second List ::")
39+
for i in range(int(n)):
40+
k=int(input(""))
41+
B.append(k)
42+
# Final merge list
43+
res = [0 for i in range(n + m)]
44+
unsortedarray(A, B, res, n, m)
45+
print ("Sorted merged list :")
46+
for i in range(n + m):
47+
print (res[i],)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Python program to check if a given Binary Tree is
2+
# symmetric or not
3+
4+
# Node structure
5+
class Node:
6+
7+
# Utility function to create new node
8+
def __init__(self, key):
9+
self.key = key
10+
self.left = None
11+
self.right = None
12+
13+
# Returns True if trees with roots as root1 and root 2
14+
# are mirror
15+
def isMirror(root1 , root2):
16+
# If both trees are empty, then they are mirror images
17+
if root1 is None and root2 is None:
18+
return True
19+
20+
""" For two trees to be mirror images, the following three
21+
conditions must be true
22+
1 - Their root node's key must be same
23+
2 - left subtree of left tree and right subtree
24+
of the right tree have to be mirror images
25+
3 - right subtree of left tree and left subtree
26+
of right tree have to be mirror images
27+
"""
28+
if (root1 is not None and root2 is not None):
29+
if root1.key == root2.key:
30+
return (isMirror(root1.left, root2.right)and
31+
isMirror(root1.right, root2.left))
32+
33+
# If neither of the above conditions is true then root1
34+
# and root2 are not mirror images
35+
return False
36+
37+
def isSymmetric(root):
38+
39+
# Check if tree is mirror of itself
40+
return isMirror(root, root)
41+
42+
# Driver Program
43+
# Let's construct the tree show in the above figure
44+
root = Node(1)
45+
root.left = Node(2)
46+
root.right = Node(2)
47+
root.left.left = Node(3)
48+
root.left.right = Node(4)
49+
root.right.left = Node(4)
50+
root.right.right = Node(3)
51+
print("1" if isSymmetric(root) == True else "0")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Print Pascal's Triangle in Python
2+
from math import factorial
3+
4+
# input n
5+
n = 5
6+
for i in range(n):
7+
for j in range(n-i+1):
8+
9+
# for left spacing
10+
print(end=" ")
11+
12+
for j in range(i+1):
13+
14+
# nCr = n!/((n-r)!*r!)
15+
print([factorial(i)//(factorial(j)*factorial(i-j))],end=" ")
16+
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# index row in Pascal's triangle
2+
3+
# Function to find the elements
4+
# of rowIndex in Pascal's Triangle
5+
def getRow(rowIndex) :
6+
7+
currow = []
8+
9+
# 1st element of every row is 1
10+
currow.append(1)
11+
12+
# Check if the row that has to
13+
# be returned is the first row
14+
if (rowIndex == 0) :
15+
16+
return currow
17+
18+
# Generate the previous row
19+
prev = getRow(rowIndex - 1)
20+
21+
for i in range(1, len(prev)) :
22+
23+
# Generate the elements
24+
# of the current row
25+
# by the help of the
26+
# previous row
27+
curr = prev[i - 1] + prev[i]
28+
currow.append(curr)
29+
30+
currow.append(1)
31+
32+
# Return the row
33+
return currow
34+
35+
n = 0
36+
arr = getRow(n)
37+
38+
for i in range(len(arr)) :
39+
40+
if (i == (len(arr) - 1)) :
41+
print(arr[i])
42+
else :
43+
print(arr[i] , end = ", ")
44+

‎30_31_inorder_preorder_postorder.py

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
class BinaryTree:
2+
def __init__(self):
3+
self.root = None
4+
self.size = 0
5+
6+
# Return True if the element is in the tree
7+
def search(self, e):
8+
current = self.root # Start from the root
9+
10+
while current != None:
11+
if e < current.element:
12+
current = current.left
13+
elif e > current.element:
14+
current = current.right
15+
else: # element matches current.element
16+
return True # Element is found
17+
18+
return False
19+
20+
# Insert element e into the binary search tree
21+
# Return True if the element is inserted successfully
22+
def insert(self, e):
23+
if self.root == None:
24+
self.root = self.createNewNode(e) # Create a new root
25+
else:
26+
# Locate the parent node
27+
parent = None
28+
current = self.root
29+
while current != None:
30+
if e < current.element:
31+
parent = current
32+
current = current.left
33+
elif e > current.element:
34+
parent = current
35+
current = current.right
36+
else:
37+
return False # Duplicate node not inserted
38+
39+
# Create the new node and attach it to the parent node
40+
if e < parent.element:
41+
parent.left = self.createNewNode(e)
42+
else:
43+
parent.right = self.createNewNode(e)
44+
45+
self.size += 1 # Increase tree size
46+
return True # Element inserted
47+
48+
# Create a new TreeNode for element e
49+
def createNewNode(self, e):
50+
return TreeNode(e)
51+
"""
52+
# Return the size of the tree
53+
def getSize(self):
54+
return self.size"""
55+
56+
# Inorder traversal from the root
57+
def inorder(self):
58+
self.inorderHelper(self.root)
59+
60+
# Inorder traversal from a subtree
61+
def inorderHelper(self, r):
62+
if r != None:
63+
self.inorderHelper(r.left)
64+
print(r.element, end = " ")
65+
self.inorderHelper(r.right)
66+
67+
# Postorder traversal from the root
68+
def postorder(self):
69+
self.postorderHelper(self.root)
70+
71+
# Postorder traversal from a subtree
72+
def postorderHelper(self, root):
73+
if root != None:
74+
self.postorderHelper(root.left)
75+
self.postorderHelper(root.right)
76+
print(root.element, end = " ")
77+
78+
# Preorder traversal from the root
79+
def preorder(self):
80+
self.preorderHelper(self.root)
81+
82+
# Preorder traversal from a subtree
83+
def preorderHelper(self, root):
84+
if root != None:
85+
print(root.element, end = " ")
86+
self.preorderHelper(root.left)
87+
self.preorderHelper(root.right)
88+
89+
90+
# Return true if the tree is empty
91+
def isEmpty(self):
92+
return self.size == 0
93+
94+
# Remove all elements from the tree
95+
def clear(self):
96+
self.root == None
97+
self.size == 0
98+
99+
# Return the root of the tree
100+
def getRoot(self):
101+
return self.root
102+
103+
class TreeNode:
104+
def __init__(self, e):
105+
self.element = e
106+
self.left = None # Point to the left node, default None
107+
self.right = None # Point to the right node, default None
108+
109+
####################### Main test binary tree
110+
111+
def main(size = 7):
112+
int_list=input('Enter a list of integers seperated by spaces: ')
113+
114+
numbers =[int_list]
115+
116+
print ("\n\nInserting the following values:")
117+
intTree = BinaryTree()
118+
for e in numbers:
119+
intTree.insert(e)
120+
print("\nPreorder traversal:")
121+
intTree.preorder()
122+
print("\n\nInorder traversal:")
123+
intTree.inorder()
124+
print("\n\nPostorder traversal:")
125+
intTree.postorder()
126+
127+
128+
if __name__ == "__main__":
129+
main()

‎9 sum_of_sub_array.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def maxSubArray(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
dp = [0 for i in range(len(nums))]
8+
dp[0] = nums[0]
9+
for i in range(1,len(nums)):
10+
dp[i] = max(dp[i-1]+nums[i],nums[i])
11+
#print(dp)
12+
return max(dp)
13+
nums = [-2,1,-3,7,-2,2,1,-5,4]
14+
ob1 = Solution()
15+
print(ob1.maxSubArray(nums))

0 commit comments

Comments
 (0)
Please sign in to comment.