├── README.md ├── Python ├── spy.py ├── spy1.py ├── hellostring.py ├── sp.py ├── hello1.py ├── 03_typecasting.py ├── 05_pr_01_add_2_numbers.py ├── 06_pr_02_remainder.py ├── selectionSort.exe ├── 04_input_function.py ├── 02_list_slicing.py ├── 07_pr05_avg.py ├── 01_strings.py ├── Squarerootgen.py ├── 02_pr_01_twinkle.py ├── Insertion Sort.py ├── new_Work.py ├── recursionFibonna.py ├── 01_variable.py ├── recursionTowerOfHanoi.py ├── 03_string_functions.py ├── 01_lists.py ├── 02_string_slicing.py ├── patterndiamond.py ├── mergeSortedArray.py ├── weather-information.py ├── 02_operators.py ├── patternimportant.py ├── Fetch HTTP status code.py ├── BubbleSort.py ├── Magic8ball.py ├── 08_simple_calculator.py ├── Binary search.py ├── recursionQuickSort.py ├── BucketSort.py ├── HeapSort.py ├── infix_to_postifx.py ├── RadixSort.py ├── tictactoe.py ├── TimSort.py ├── Searching_Sorting_Algorithms.py ├── webbrowser.py ├── sorting algo 1.ipynb └── trees.ipynb ├── .vs ├── ProjectSettings.json ├── slnx.sqlite └── Hacktober20fest21 │ └── v17 │ └── .suo ├── 04_escape_sequences.py ├── Java ├── java-output-formatting-English.pdf ├── SubArray.java ├── MaximumSubarraySum.java ├── SelectionSort.java ├── InsertionSort.java └── substringmin.java ├── CSharp └── HelloWorld.cs ├── CPP ├── Valid_sum.cpp ├── Bucket_Sort.cpp ├── Find the repeating and the missing .cpp ├── Insertion sort.cpp ├── BucketSort.cpp ├── Quick_Sort.cpp ├── adding-height-using-operator-overloading.cpp ├── Trapping Rain Water.cpp ├── Majority element.cpp ├── Max_Circular_Subarray_Sum.cpp ├── Nikunj_And_Donuts.cpp ├── HeapSort.cpp ├── radixSort.cpp ├── Wildcard Matching.cpp ├── Deletion from a Circular Linked List.cpp └── Infix to postfix.cpp ├── recursionQuickSort.py ├── LICENSE ├── HiringTest.java └── TimSort.cpp /README.md: -------------------------------------------------------------------------------- 1 | # Hacktober20fest21 -------------------------------------------------------------------------------- /Python/spy.py: -------------------------------------------------------------------------------- 1 | print("hello guys") 2 | -------------------------------------------------------------------------------- /Python/spy1.py: -------------------------------------------------------------------------------- 1 | print("hello123") 2 | -------------------------------------------------------------------------------- /Python/hellostring.py: -------------------------------------------------------------------------------- 1 | print("python") 2 | -------------------------------------------------------------------------------- /Python/sp.py: -------------------------------------------------------------------------------- 1 | print("python is very easy") 2 | -------------------------------------------------------------------------------- /Python/hello1.py: -------------------------------------------------------------------------------- 1 | print("hello") 2 | Print ("world") 3 | -------------------------------------------------------------------------------- /.vs/ProjectSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "CurrentProjectSetting": null 3 | } -------------------------------------------------------------------------------- /04_escape_sequences.py: -------------------------------------------------------------------------------- 1 | story = "Harry is good.\nHe\tis ve\\ry good" 2 | print(story) -------------------------------------------------------------------------------- /Python/03_typecasting.py: -------------------------------------------------------------------------------- 1 | a = "35fgrfg34" 2 | a = int(a) 3 | print(type(a)) 4 | print(a + 5) -------------------------------------------------------------------------------- /Python/05_pr_01_add_2_numbers.py: -------------------------------------------------------------------------------- 1 | a = 30 2 | b = 15 3 | print("The sum of a and b is", a + b) -------------------------------------------------------------------------------- /.vs/slnx.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harshitmody72/Hacktober20fest21/HEAD/.vs/slnx.sqlite -------------------------------------------------------------------------------- /Python/06_pr_02_remainder.py: -------------------------------------------------------------------------------- 1 | a = 458 2 | b = 15 3 | 4 | print("The remainder when a is divided by b is", a%b) -------------------------------------------------------------------------------- /Python/selectionSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harshitmody72/Hacktober20fest21/HEAD/Python/selectionSort.exe -------------------------------------------------------------------------------- /.vs/Hacktober20fest21/v17/.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harshitmody72/Hacktober20fest21/HEAD/.vs/Hacktober20fest21/v17/.suo -------------------------------------------------------------------------------- /Python/04_input_function.py: -------------------------------------------------------------------------------- 1 | a = input("Enter a number: ") 2 | a = int(a) # Convert a to an Integer(if possible) 3 | print(type(a)) -------------------------------------------------------------------------------- /Java/java-output-formatting-English.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harshitmody72/Hacktober20fest21/HEAD/Java/java-output-formatting-English.pdf -------------------------------------------------------------------------------- /Python/02_list_slicing.py: -------------------------------------------------------------------------------- 1 | # List slicing 2 | friends = ["Harry", "Tom", "Rohan", "Sam", "Divya", 45] 3 | print(friends[0:4]) 4 | print(friends[-4:]) 5 | -------------------------------------------------------------------------------- /Python/07_pr05_avg.py: -------------------------------------------------------------------------------- 1 | a = input("Enter first number: ") 2 | b = input("Enter second number: ") 3 | a = int(a) 4 | b = int(b) 5 | avg = (a + b)/2 6 | print("The average of a and b is", avg) -------------------------------------------------------------------------------- /Python/01_strings.py: -------------------------------------------------------------------------------- 1 | # b = "Harry's" # --> Use this if you have single quotes in your strings 2 | # b = 'Harry"s' 3 | b = '''Harry"s and 4 | Harry's''' 5 | print(b) 6 | # print(type(b)) 7 | -------------------------------------------------------------------------------- /Python/Squarerootgen.py: -------------------------------------------------------------------------------- 1 | import cmath 2 | num = eval(input('Enter a number: ')) 3 | num_sqrt = cmath.sqrt(num) 4 | print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag)) 5 | -------------------------------------------------------------------------------- /Python/02_pr_01_twinkle.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This problem is a solution of 3 | Problem 1 of CodeWithHarry Practice Set! 4 | ''' 5 | # This is also a comment just like the above line 6 | 7 | print('''Twinkle, twinkle, little star, 8 | How I wonder what you are! 9 | Up above the world so high, 10 | Like a diamond in the sky.''') 11 | -------------------------------------------------------------------------------- /Python/Insertion Sort.py: -------------------------------------------------------------------------------- 1 | # Python implementation of Insertion Sort Algorithm 2 | 3 | arr=[54,23,78,43,98,101,57,28,65] 4 | for i in range(1, len(arr)): 5 | key = arr[i] 6 | j = i-1 7 | while j >= 0 and key < arr[j] : 8 | arr[j + 1] =arr[j] 9 | j -= 1 10 | arr[j + 1] = key 11 | print(arr) 12 | -------------------------------------------------------------------------------- /Python/new_Work.py: -------------------------------------------------------------------------------- 1 | # s = None 2 | # for i in [9,8,7]: 3 | # if s == i: 4 | # s=i 5 | # 6 | # print(s) 7 | 8 | 9 | class Person: 10 | def math(self): 11 | pass 12 | 13 | def ritu(self): 14 | pass 15 | 16 | 17 | poin = Person() 18 | poin.ritu() 19 | har = Person() 20 | har.rit -------------------------------------------------------------------------------- /Python/recursionFibonna.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(3000) #using to increase depth of recursion 3 | 4 | def fib(n): 5 | if n==1 or n==2: 6 | return 1 7 | fib_n_1=fib(n-1) 8 | fib_n_2=fib(n-2) 9 | output=fib_n_1 + fib_n_2 10 | return output 11 | 12 | n=int(input()) 13 | 14 | print(fib(n)) -------------------------------------------------------------------------------- /Python/01_variable.py: -------------------------------------------------------------------------------- 1 | a_122 = '''harry''' 2 | # a = 'harry' 3 | # a = "harry" 4 | b = 345 5 | c = 45.32 6 | d = True 7 | # d = None 8 | 9 | # Printing the variables 10 | print(a) 11 | print(b) 12 | print(c) 13 | print(d) 14 | 15 | # Printing the type of variables 16 | print(type(a)) 17 | print(type(b)) 18 | print(type(c)) 19 | print(type(d)) 20 | -------------------------------------------------------------------------------- /CSharp/HelloWorld.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public static class HelloWorld 4 | { 5 | public static void Main(string[] args) 6 | { 7 | // Output to the console like so 8 | System.Console.Write("Hello, World!"); 9 | 10 | // Prevents the console from closing until the user presses a key 11 | System.Console.Read(); 12 | } 13 | } -------------------------------------------------------------------------------- /Python/recursionTowerOfHanoi.py: -------------------------------------------------------------------------------- 1 | def towerofhanoi(n, source, aux, dest): 2 | if n==0: 3 | return 4 | if n==1: 5 | print(source,dest) 6 | return 7 | towerofhanoi(n-1,source,dest,aux) 8 | print(source,dest) 9 | towerofhanoi(n-1,aux,source,dest) 10 | 11 | n=int(input()) 12 | towerofhanoi(n, 'a', 'b', 'c') 13 | -------------------------------------------------------------------------------- /Python/03_string_functions.py: -------------------------------------------------------------------------------- 1 | story = "once upon a time there was a youtuber named Harry who uploaded python course with notes Harry" 2 | 3 | # String Functions 4 | # print(len(story)) 5 | # print(story.endswith("notes")) 6 | # print(story.count("c")) 7 | # print(story.capitalize()) 8 | # print(story.find("upon")) 9 | print(story.replace("Harry", "CodeWithHarry")) 10 | -------------------------------------------------------------------------------- /Python/01_lists.py: -------------------------------------------------------------------------------- 1 | # Create a list using [] 2 | a = [1, 2 , 4, 56, 6] 3 | 4 | # Print the list using print() function 5 | print(a) 6 | 7 | # Access using index using a[0], a[1], a[2] 8 | print(a[2]) 9 | 10 | # Change the value of list using 11 | a[0] = 98 12 | print(a) 13 | 14 | # We can create a list with items of different types 15 | c = [45, "Harry", False, 6.9] 16 | print(c) 17 | 18 | -------------------------------------------------------------------------------- /Python/02_string_slicing.py: -------------------------------------------------------------------------------- 1 | # greeting = "Good Morning, " 2 | # name = "Harry" 3 | # print(type(name)) 4 | 5 | # Concatenating two strings 6 | # c = greeting + name 7 | # print(c) 8 | name = "Harry" 9 | # print(name[4]) 10 | # name[3] = "d" --> Does not work 11 | 12 | # print(name[1:4]) 13 | # print(name[:4]) # is same as name[0:4] 14 | # print(name[1:]) # is same as name[1:5] 15 | # c = name[-4:-1] # is same is name[1:4] 16 | # print(c) 17 | 18 | name = "HarryIsGood" 19 | # d = name[0::3] 20 | d = name[:0:-1] 21 | print(d) -------------------------------------------------------------------------------- /Python/patterndiamond.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | n1=(n//2)+1 3 | for i in range(1,n1+1,1): 4 | for space1 in range(n1-i): 5 | print(' ',end='') 6 | for star1 in range(i): 7 | print('*',end='') 8 | for star2 in range(i-1): 9 | print('*',end='') 10 | print() 11 | n2=n//2 12 | for j in range(1,n2+1,1): 13 | for space2 in range(j): 14 | print(' ',end='') 15 | for star3 in range(n2-j+1): 16 | print('*',end='') 17 | for star4 in range(n2-j): 18 | print('*',end='') 19 | print() 20 | -------------------------------------------------------------------------------- /Java/SubArray.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | import java.util.Arrays; 4 | 5 | public class SubArray { 6 | public static void main(String[] args) { 7 | System.out.println("All Non-empty Subarrays"); 8 | subArray(arr.length); 9 | } 10 | static int[] arr = new int[]{1,2,3,4,5}; 11 | static void subArray(int n){ 12 | for(int i = 0; i < n; i++){ 13 | for(int j = i; j < n; j++){ 14 | for(int k = i; k <= j; k++){ 15 | System.out.print(arr[k] + " "); 16 | } 17 | System.out.println(); 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Python/mergeSortedArray.py: -------------------------------------------------------------------------------- 1 | def mergesortedarrays(arr1,arr2): 2 | i=0 3 | j=0 4 | arr=[] 5 | while i=7) 20 | # b = (14<7) 21 | # b = (14>7) 22 | # b = (14==7) 23 | b = (14!=7) 24 | print(b) 25 | 26 | # Logical Operators 27 | bool1 = True 28 | bool2 = False 29 | print("The value of bool1 and bool2 is", (bool1 and bool2)) 30 | print("The value of bool1 or bool2 is", (bool1 or bool2)) 31 | print("The value of not bool2 is", (not bool2)) -------------------------------------------------------------------------------- /Python/patternimportant.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | n1=n//2 3 | if n%2==0: 4 | for i in range(n1): 5 | for j in range((n*i*2)+1,(n*i*2)+(n+1)): 6 | print(j,' ', end='') 7 | print() 8 | for i in range(n1): 9 | k=n*((n-1)-(2*i))+1 10 | for j in range(k,k+n): 11 | print(j,' ',end='') 12 | print() 13 | else: 14 | n2=(n+1)//2 15 | for i in range(n2): 16 | for j in range((n*i*2)+1,(n*i*2)+(n+1)): 17 | print(j,' ', end='') 18 | print() 19 | for i in range(n2-1): 20 | k=n*((n-2)-(2*i))+1 21 | for j in range(k,k+n): 22 | print(j,' ',end='') 23 | print() 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Python/Fetch HTTP status code.py: -------------------------------------------------------------------------------- 1 | from urllib.request import urlopen 2 | from urllib.error import URLError, HTTPError 3 | import emoji 4 | 5 | URL = input("Enter the URL: ") 6 | 7 | try: 8 | response = urlopen(URL) 9 | print('Status code : ' + str(response.code) + ' ' + emoji.emojize(':thumbs_up:')) 10 | print('Message : ' + 'Request succeeded. Request returned message - ' + response.reason) 11 | except HTTPError as e: 12 | print('Status : ' + str(e.code) + ' ' + emoji.emojize(':thumbs_down:')) 13 | print('Message : Request failed. Request returned reason - ' + e.reason) 14 | except URLError as e: 15 | print('Status :', str(e.reason).split(']')[0].replace('[','') + ' ' + emoji.emojize(':thumbs_down:')) 16 | print('Message : '+ str(e.reason).split(']')[1]) -------------------------------------------------------------------------------- /Java/SelectionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | class SelectionSort { 4 | void selectionSort(int array[]) { 5 | int size = array.length; 6 | 7 | for (int step = 0; step < size - 1; step++) { 8 | int min_idx = step; 9 | 10 | for (int i = step + 1; i < size; i++) { 11 | 12 | if (array[i] < array[min_idx]) { 13 | min_idx = i; 14 | } 15 | } 16 | int temp = array[step]; 17 | array[step] = array[min_idx]; 18 | array[min_idx] = temp; 19 | } 20 | } 21 | 22 | 23 | public static void main(String args[]) { 24 | int[] data = { 20, 12, 10, 15, 2 }; 25 | SelectionSort ss = new SelectionSort(); 26 | ss.selectionSort(data); 27 | System.out.println("Sorted Array in Ascending Order: "); 28 | System.out.println(Arrays.toString(data)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Python/BubbleSort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of Bubble Sort 2 | 3 | def bubbleSort(arr): 4 | n = len(arr) 5 | 6 | # Traverse through all array elements 7 | for i in range(n-1): 8 | # range(n) also work but outer loop will repeat one time more than needed. 9 | 10 | # Last i elements are already in place 11 | for j in range(0, n-i-1): 12 | 13 | # traverse the array from 0 to n-i-1 14 | # Swap if the element found is greater 15 | # than the next element 16 | if arr[j] > arr[j + 1] : 17 | arr[j], arr[j + 1] = arr[j + 1], arr[j] 18 | 19 | # Driver code to test above 20 | arr = [64, 34, 25, 12, 22, 11, 90] 21 | 22 | bubbleSort(arr) 23 | 24 | print ("Sorted array is:") 25 | for i in range(len(arr)): 26 | print ("% d" % arr[i]) 27 | -------------------------------------------------------------------------------- /CPP/Valid_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Solution{ 6 | 7 | public: 8 | long long ValidPair(int a[], int n) 9 | { 10 | 11 | long long ans =0; 12 | int start=0,end=n-1; 13 | sort(a,a+n); 14 | while(start0) 17 | { 18 | ans=ans+(end-start); 19 | end--; 20 | } 21 | else{ 22 | start++; 23 | } 24 | } 25 | return ans; 26 | } 27 | }; 28 | 29 | 30 | 31 | int main() 32 | { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | int array[n]; 40 | for (int i = 0; i < n; ++i) 41 | cin>>array[i]; 42 | Solution obj; 43 | cout << obj.ValidPair(array, n) <<"\n"; 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Python/Magic8ball.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import random 3 | 4 | ans = True 5 | 6 | while ans: 7 | question = raw_input("Ask the magic 8 ball a question: (press enter to quit) ") 8 | 9 | answers = random.randint(1,8) 10 | 11 | if question == "": 12 | sys.exit() 13 | 14 | elif answers == 1: 15 | print "It is certain" 16 | 17 | elif answers == 2: 18 | print "Outlook good" 19 | 20 | elif answers == 3: 21 | print "You may rely on it" 22 | 23 | elif answers == 4: 24 | print "Ask again later" 25 | 26 | elif answers == 5: 27 | print "Concentrate and ask again" 28 | 29 | elif answers == 6: 30 | print "Reply hazy, try again" 31 | 32 | elif answers == 7: 33 | print "My reply is no" 34 | 35 | elif answers == 8: 36 | print "My sources say no" 37 | -------------------------------------------------------------------------------- /CPP/Bucket_Sort.cpp: -------------------------------------------------------------------------------- 1 | /*Implementation of bucket sort */ 2 | #include 3 | using namespace std; 4 | 5 | void bucketSort(int arr[], int n, int k){ 6 | int max_value = arr[0]; 7 | for(int i = 0; i bkt[k]; 12 | for(int i = 0; i>n; 30 | int arr[n]; 31 | for(int i = 0; i>arr[i]; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /CPP/Find the repeating and the missing .cpp: -------------------------------------------------------------------------------- 1 | // C++ program to Find the repeating 2 | // and missing elements 3 | 4 | #include 5 | using namespace std; 6 | 7 | void printTwoElements(int arr[], int size) 8 | { 9 | int i; 10 | cout << " The repeating element is "; 11 | 12 | for (i = 0; i < size; i++) { 13 | if (arr[abs(arr[i]) - 1] > 0) 14 | arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1]; 15 | else 16 | cout << abs(arr[i]) << "\n"; 17 | } 18 | 19 | cout << "and the missing element is "; 20 | for (i = 0; i < size; i++) { 21 | if (arr[i] > 0) 22 | cout << (i + 1); 23 | } 24 | } 25 | 26 | /* Driver code */ 27 | int main() 28 | { 29 | int arr[] = { 7, 3, 4, 5, 5, 6, 2 }; 30 | int n = sizeof(arr) / sizeof(arr[0]); 31 | printTwoElements(arr, n); 32 | } 33 | 34 | // The repeating element is 5 35 | // and the missing element is 1 36 | -------------------------------------------------------------------------------- /Python/08_simple_calculator.py: -------------------------------------------------------------------------------- 1 | #python program - make simple calculator 2 | 3 | print("1. adiition") 4 | print("2. subtraction") 5 | print("3. multiplication") 6 | print("4. division") 7 | print("5. exit") 8 | choice = int(input("enter your choice: ")) 9 | if (choice>=1 and choice<=4): 10 | print("enter two numbers: ") 11 | num1 = int(input()) 12 | num2 = int(input()) 13 | if choice ==1: 14 | res = num1 + num2 15 | print("result = ", res) 16 | elif choice == 2: 17 | res = num1 - num2 18 | print("result=",res) 19 | elif choice==3: 20 | res=num1*num2 21 | print("result=",res) 22 | else: 23 | res=num1/num2 24 | print("result=",res) 25 | elif choice==5: 26 | exit() 27 | else: 28 | print("wrong input..!!") 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Python/Binary search.py: -------------------------------------------------------------------------------- 1 | def binarySearch (arr, l, r, x): 2 | 3 | # Check base case 4 | if r >= l: 5 | 6 | mid = l + (r - l) // 2 7 | 8 | # If element is present at the middle itself 9 | if arr[mid] == x: 10 | return mid 11 | 12 | # If element is smaller than mid, then it 13 | # can only be present in left subarray 14 | elif arr[mid] > x: 15 | return binarySearch(arr, l, mid-1, x) 16 | 17 | # Else the element can only be present 18 | # in right subarray 19 | else: 20 | return binarySearch(arr, mid + 1, r, x) 21 | 22 | else: 23 | # Element is not present in the array 24 | return -1 25 | 26 | # Driver Code 27 | arr = [ 2, 3, 4, 10, 40 ] 28 | x = 10 29 | 30 | # Function call 31 | result = binarySearch(arr, 0, len(arr)-1, x) 32 | 33 | if result != -1: 34 | print ("Element is present at index % d" % result) 35 | else: 36 | print ("Element is not present in array") 37 | -------------------------------------------------------------------------------- /CPP/Insertion sort.cpp: -------------------------------------------------------------------------------- 1 | // Insertion sort in C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Function to print an array 7 | void printArray(int array[], int size) { 8 | for (int i = 0; i < size; i++) { 9 | cout << array[i] << " "; 10 | } 11 | cout << endl; 12 | } 13 | 14 | void insertionSort(int array[], int size) { 15 | for (int step = 1; step < size; step++) { 16 | int key = array[step]; 17 | int j = step - 1; 18 | 19 | // Compare key with each element on the left of it until an element smaller than 20 | // it is found. 21 | // For descending order, change keyarray[j]. 22 | while (key < array[j] && j >= 0) { 23 | array[j + 1] = array[j]; 24 | --j; 25 | } 26 | array[j + 1] = key; 27 | } 28 | } 29 | 30 | // Driver code 31 | int main() { 32 | int data[] = {9, 5, 1, 4, 3}; 33 | int size = sizeof(data) / sizeof(data[0]); 34 | insertionSort(data, size); 35 | cout << "Sorted array in ascending order:\n"; 36 | printArray(data, size); 37 | } -------------------------------------------------------------------------------- /recursionQuickSort.py: -------------------------------------------------------------------------------- 1 | def partition(li,s,e): 2 | #target of partition is to place pivot at its correct position and all elements less than pviot before pivot 3 | #and all greater than pivot after after pivot 4 | 5 | pivot=li[s] 6 | c=0 7 | for z in range(s,e+1): 8 | if li[z]=pivot: 21 | j=j-1 22 | else: 23 | li[i],li[j]=li[j],li[i] 24 | i=i+1 25 | j=j-1 26 | 27 | return s+c 28 | def quickSort(arr, start, end): 29 | if start >= end: 30 | return 31 | pivot_index=partition(arr,start,end) 32 | quickSort(arr,start,pivot_index-1) 33 | quickSort(arr,pivot_index+1,end) 34 | 35 | n=int(input()) 36 | arr=list(int(i) for i in input().strip().split(' ')) 37 | quickSort(arr, 0, n-1) 38 | print(*arr) 39 | -------------------------------------------------------------------------------- /Python/recursionQuickSort.py: -------------------------------------------------------------------------------- 1 | def partition(li,s,e): 2 | #target of partition is to place pivot at its correct position and all elements less than pviot before pivot 3 | #and all greater than pivot after after pivot 4 | 5 | pivot=li[s] 6 | c=0 7 | for z in range(s,e+1): 8 | if li[z]=pivot: 21 | j=j-1 22 | else: 23 | li[i],li[j]=li[j],li[i] 24 | i=i+1 25 | j=j-1 26 | 27 | return s+c 28 | def quickSort(arr, start, end): 29 | if start >= end: 30 | return 31 | pivot_index=partition(arr,start,end) 32 | quickSort(arr,start,pivot_index-1) 33 | quickSort(arr,pivot_index+1,end) 34 | 35 | n=int(input()) 36 | arr=list(int(i) for i in input().strip().split(' ')) 37 | quickSort(arr, 0, n-1) 38 | print(*arr) 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Harshit Mody 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Python/BucketSort.py: -------------------------------------------------------------------------------- 1 | # Python3 program to sort an array 2 | # using bucket sort 3 | def insertionSort(b): 4 | for i in range(1, len(b)): 5 | up = b[i] 6 | j = i - 1 7 | while j >= 0 and b[j] > up: 8 | b[j + 1] = b[j] 9 | j -= 1 10 | b[j + 1] = up 11 | return b 12 | 13 | def bucketSort(x): 14 | arr = [] 15 | slot_num = 10 # 10 means 10 slots, each 16 | # slot's size is 0.1 17 | for i in range(slot_num): 18 | arr.append([]) 19 | 20 | # Put array elements in different buckets 21 | for j in x: 22 | index_b = int(slot_num * j) 23 | arr[index_b].append(j) 24 | 25 | # Sort individual buckets 26 | for i in range(slot_num): 27 | arr[i] = insertionSort(arr[i]) 28 | 29 | # concatenate the result 30 | k = 0 31 | for i in range(slot_num): 32 | for j in range(len(arr[i])): 33 | x[k] = arr[i][j] 34 | k += 1 35 | return x 36 | 37 | # Driver Code 38 | x = [0.897, 0.565, 0.656, 39 | 0.1234, 0.665, 0.3434] 40 | print("Sorted Array is") 41 | print(bucketSort(x)) 42 | -------------------------------------------------------------------------------- /Java/InsertionSort.java: -------------------------------------------------------------------------------- 1 | public class InsertionSortExample { 2 | public static void insertionSort(int array[]) { 3 | int n = array.length; 4 | for (int j = 1; j < n; j++) { 5 | int key = array[j]; 6 | int i = j-1; 7 | while ( (i > -1) && ( array [i] > key ) ) { 8 | array [i+1] = array [i]; 9 | i--; 10 | } 11 | array[i+1] = key; 12 | } 13 | } 14 | 15 | public static void main(String a[]){ 16 | int[] arr1 = {9,14,3,2,43,11,58,22}; 17 | System.out.println("Before Insertion Sort"); 18 | for(int i:arr1){ 19 | System.out.print(i+" "); 20 | } 21 | System.out.println(); 22 | 23 | insertionSort(arr1);//sorting array using insertion sort 24 | 25 | System.out.println("After Insertion Sort"); 26 | for(int i:arr1){ 27 | System.out.print(i+" "); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CPP/BucketSort.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to sort an 2 | // array using bucket sort 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | // Function to sort arr[] of 9 | // size n using bucket sort 10 | void bucketSort(float arr[], int n) 11 | { 12 | 13 | // 1) Create n empty buckets 14 | vector b[n]; 15 | 16 | // 2) Put array elements 17 | // in different buckets 18 | for (int i = 0; i < n; i++) { 19 | int bi = n * arr[i]; // Index in bucket 20 | b[bi].push_back(arr[i]); 21 | } 22 | 23 | // 3) Sort individual buckets 24 | for (int i = 0; i < n; i++) 25 | sort(b[i].begin(), b[i].end()); 26 | 27 | // 4) Concatenate all buckets into arr[] 28 | int index = 0; 29 | for (int i = 0; i < n; i++) 30 | for (int j = 0; j < b[i].size(); j++) 31 | arr[index++] = b[i][j]; 32 | } 33 | 34 | /* Driver program to test above function */ 35 | int main() 36 | { 37 | float arr[] 38 | = { 0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434 }; 39 | int n = sizeof(arr) / sizeof(arr[0]); 40 | bucketSort(arr, n); 41 | 42 | cout << "Sorted array is \n"; 43 | for (int i = 0; i < n; i++) 44 | cout << arr[i] << " "; 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /CPP/Quick_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /* Function to print an array */ 6 | void printArray(int arr[], int size) 7 | { 8 | int i; 9 | for (i=0; i < size; i++) 10 | printf("%d ", arr[i]); 11 | printf("\n"); 12 | } 13 | 14 | class Solution 15 | { public: 16 | int partition (int a[], int l, int h) 17 | {int pivot = a[l]; 18 | int i=l-1, j=h+1; 19 | while(true){ 20 | do{ 21 | i++; 22 | }while(a[i]pivot); 27 | 28 | if(i>=j){ 29 | return j; 30 | } 31 | swap(a[i], a[j]); 32 | 33 | } 34 | } 35 | 36 | public: 37 | //Function to sort an array using quick sort algorithm. 38 | void quickSort(int a[], int l, int h) 39 | {if(l 2 | using namespace std; 3 | 4 | class Distance 5 | { 6 | private: 7 | int feet, inches; 8 | public: 9 | void set_data(int f,int i) 10 | { 11 | feet=f; 12 | inches=i; 13 | if(inches>=12) 14 | { 15 | feet=feet+(inches/12); 16 | inches=inches%12; 17 | } 18 | } 19 | Distance operator+(Distance b) 20 | { 21 | Distance temp; 22 | 23 | feet=feet+b.feet; 24 | inches=inches+b.inches; 25 | if(inches>=12) 26 | { 27 | feet=feet+(inches/12); 28 | inches=inches%12; 29 | } 30 | 31 | temp.feet=feet; 32 | temp.inches=inches; 33 | return temp; 34 | } 35 | void display() 36 | { 37 | cout<<"Feet: "< 3 | using namespace std; 4 | 5 | int maxWater(int arr[], int n) 6 | { 7 | 8 | // indices to traverse the array 9 | int left = 0; 10 | int right = n-1; 11 | 12 | // To store Left max and right max 13 | // for two pointers left and right 14 | int l_max = 0; 15 | int r_max = 0; 16 | 17 | // To store the total amount 18 | // of rain water trapped 19 | int result = 0; 20 | while (left <= right) 21 | { 22 | 23 | // We need check for minimum of left 24 | // and right max for each element 25 | if(r_max <= l_max) 26 | { 27 | 28 | // Add the difference between 29 | // current value and right max at index r 30 | result += max(0, r_max-arr[right]); 31 | 32 | // Update right max 33 | r_max = max(r_max, arr[right]); 34 | 35 | // Update right pointer 36 | right -= 1; 37 | } 38 | else 39 | { 40 | 41 | // Add the difference between 42 | // current value and left max at index l 43 | result += max(0, l_max-arr[left]); 44 | 45 | // Update left max 46 | l_max = max(l_max, arr[left]); 47 | 48 | // Update left pointer 49 | left += 1; 50 | } 51 | } 52 | return result; 53 | } 54 | 55 | // Driver code 56 | int main() { 57 | int arr[] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; 58 | int n = sizeof(arr)/sizeof(arr[0]); 59 | cout << maxWater(arr, n) << endl; 60 | return 0; 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /CPP/Majority element.cpp: -------------------------------------------------------------------------------- 1 | // C++ Program for finding out 2 | // majority element in an array 3 | #include 4 | using namespace std; 5 | 6 | /* Function to find the candidate for Majority */ 7 | int findCandidate(int a[], int size) 8 | { 9 | int maj_index = 0, count = 1; 10 | for (int i = 1; i < size; i++) { 11 | if (a[maj_index] == a[i]) 12 | count++; 13 | else 14 | count--; 15 | if (count == 0) { 16 | maj_index = i; 17 | count = 1; 18 | } 19 | } 20 | return a[maj_index]; 21 | } 22 | 23 | /* Function to check if the candidate 24 | occurs more than n/2 times */ 25 | bool isMajority(int a[], int size, int cand) 26 | { 27 | int count = 0; 28 | for (int i = 0; i < size; i++) 29 | 30 | if (a[i] == cand) 31 | count++; 32 | 33 | if (count > size / 2) 34 | return 1; 35 | 36 | else 37 | return 0; 38 | } 39 | 40 | /* Function to print Majority Element */ 41 | void printMajority(int a[], int size) 42 | { 43 | /* Find the candidate for Majority*/ 44 | int cand = findCandidate(a, size); 45 | 46 | /* Print the candidate if it is Majority*/ 47 | if (isMajority(a, size, cand)) 48 | cout << " " << cand << " "; 49 | 50 | else 51 | cout << "No Majority Element"; 52 | } 53 | 54 | /* Driver code */ 55 | int main() 56 | { 57 | int a[] = { 1, 3, 3, 1, 2 }; 58 | int size = (sizeof(a)) / sizeof(a[0]); 59 | 60 | // Function calling 61 | printMajority(a, size); 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /CPP/Max_Circular_Subarray_Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution{ 4 | public: 5 | // arr: input array 6 | // num: size of array 7 | //Function to find maximum circular subarray sum. 8 | int normal(int arr[], int n){ 9 | int res=arr[0], maxend=arr[0]; 10 | for(int i=1; i> T; 41 | 42 | while (T--) 43 | { 44 | int num; 45 | 46 | //size of array 47 | cin>>num; 48 | int arr[num]; 49 | 50 | //inserting elements 51 | for(int i = 0; i>arr[i]; 53 | 54 | Solution ob; 55 | //calling function 56 | cout << ob.circularSubarraySum(arr, num) << endl; 57 | 58 | } 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /CPP/Nikunj_And_Donuts.cpp: -------------------------------------------------------------------------------- 1 | Question :- 2 | 3 | Nikunj loves donuts, but he also likes to stay fit. He eats n donuts in one sitting, and each donut has a calorie count, ci. After eating a donut with k calories, he must walk at least 2^j x k(where j is the number donuts he has already eaten) miles to maintain his weight. 4 | Given the individual calorie counts for each of the n donuts, find and print a long integer denoting the minimum number of miles Nikunj must walk to maintain his weight. Note that he can eat the donuts in any order. 5 | Input 6 | The first line contains an integer, n, denoting the number of donuts. 7 | The second line contains n space-separated integers describing the respective calorie counts of each donut I, i.e ci. 8 | Output 9 | Print a long integer denoting the minimum number of miles Nikunj must walk to maintain his weight. 10 | Constraints 11 | 1 ≤ n ≤ 40 12 | 1 ≤ ci ≤ 1000 13 | Sample Input 14 | 3 15 | 1 3 2 16 | Sample Output 17 | 11 18 | 19 | Code:- 20 | 21 | 22 | 23 | #include 24 | #include 25 | #include 26 | #define ll unsigned long long int 27 | using namespace std; 28 | ll minimum_miles(ll *arr, ll n) 29 | { 30 | ll total_miles=0; 31 | for(ll i=0; i>n; 41 | ll *arr=new ll [n]; 42 | for(ll i=0; i>arr[i]; 45 | } 46 | sort(arr, arr+n); 47 | cout< 3 | 4 | using namespace std; 5 | 6 | // To heapify a subtree rooted with node i which is 7 | // an index in arr[]. n is size of heap 8 | void heapify(int arr[], int n, int i) 9 | { 10 | int largest = i; // Initialize largest as root 11 | int l = 2 * i + 1; // left = 2*i + 1 12 | int r = 2 * i + 2; // right = 2*i + 2 13 | 14 | // If left child is larger than root 15 | if (l < n && arr[l] > arr[largest]) 16 | largest = l; 17 | 18 | // If right child is larger than largest so far 19 | if (r < n && arr[r] > arr[largest]) 20 | largest = r; 21 | 22 | // If largest is not root 23 | if (largest != i) { 24 | swap(arr[i], arr[largest]); 25 | 26 | // Recursively heapify the affected sub-tree 27 | heapify(arr, n, largest); 28 | } 29 | } 30 | 31 | // main function to do heap sort 32 | void heapSort(int arr[], int n) 33 | { 34 | // Build heap (rearrange array) 35 | for (int i = n / 2 - 1; i >= 0; i--) 36 | heapify(arr, n, i); 37 | 38 | // One by one extract an element from heap 39 | for (int i = n - 1; i > 0; i--) { 40 | // Move current root to end 41 | swap(arr[0], arr[i]); 42 | 43 | // call max heapify on the reduced heap 44 | heapify(arr, i, 0); 45 | } 46 | } 47 | 48 | /* A utility function to print array of size n */ 49 | void printArray(int arr[], int n) 50 | { 51 | for (int i = 0; i < n; ++i) 52 | cout << arr[i] << " "; 53 | cout << "\n"; 54 | } 55 | 56 | // Driver code 57 | int main() 58 | { 59 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 60 | int n = sizeof(arr) / sizeof(arr[0]); 61 | 62 | heapSort(arr, n); 63 | 64 | cout << "Sorted array is \n"; 65 | printArray(arr, n); 66 | } 67 | -------------------------------------------------------------------------------- /Python/RadixSort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of Radix Sort 2 | # A function to do counting sort of arr[] according to 3 | # the digit represented by exp. 4 | 5 | def countingSort(arr, exp1): 6 | 7 | n = len(arr) 8 | 9 | # The output array elements that will have sorted arr 10 | output = [0] * (n) 11 | 12 | # initialize count array as 0 13 | count = [0] * (10) 14 | 15 | # Store count of occurrences in count[] 16 | for i in range(0, n): 17 | index = arr[i] // exp1 18 | count[index % 10] += 1 19 | 20 | # Change count[i] so that count[i] now contains actual 21 | # position of this digit in output array 22 | for i in range(1, 10): 23 | count[i] += count[i - 1] 24 | 25 | # Build the output array 26 | i = n - 1 27 | while i >= 0: 28 | index = arr[i] // exp1 29 | output[count[index % 10] - 1] = arr[i] 30 | count[index % 10] -= 1 31 | i -= 1 32 | 33 | # Copying the output array to arr[], 34 | # so that arr now contains sorted numbers 35 | i = 0 36 | for i in range(0, len(arr)): 37 | arr[i] = output[i] 38 | 39 | # Method to do Radix Sort 40 | def radixSort(arr): 41 | 42 | # Find the maximum number to know number of digits 43 | max1 = max(arr) 44 | 45 | # Do counting sort for every digit. Note that instead 46 | # of passing digit number, exp is passed. exp is 10^i 47 | # where i is current digit number 48 | exp = 1 49 | while max1 / exp > 0: 50 | countingSort(arr, exp) 51 | exp *= 10 52 | 53 | 54 | # Driver code 55 | arr = [170, 45, 75, 90, 802, 24, 2, 66] 56 | 57 | # Function Call 58 | radixSort(arr) 59 | 60 | for i in range(len(arr)): 61 | print(arr[i]) 62 | 63 | # This code is contributed by Mohit Kumra 64 | # Edited by Patrick Gallagher 65 | -------------------------------------------------------------------------------- /Java/substringmin.java: -------------------------------------------------------------------------------- 1 | public class Characters 2 | 3 | public static void main(String[] args) { 4 | String str = "grass is greener on the other side"; 5 | int[] freq = new int[str.length()]; 6 | char minChar = str.charAt(0), maxChar = str.charAt(0); 7 | int i, j, min, max; 8 | 9 | //Converts given string into character array 10 | char string[] = str.toCharArray(); 11 | 12 | //Count each word in given string and store in array freq 13 | for(i = 0; i < string.length; i++) { 14 | freq[i] = 1; 15 | for(j = i+1; j < string.length; j++) { 16 | if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') { 17 | freq[i]++; 18 | 19 | //Set string[j] to 0 to avoid printing visited character 20 | string[j] = '0'; 21 | } 22 | } 23 | } 24 | 25 | //Determine minimum and maximum occurring characters 26 | min = max = freq[0]; 27 | for(i = 0; i freq[i] && freq[i] != '0') { 32 | min = freq[i]; 33 | minChar = string[i]; 34 | } 35 | //If max is less than frequency of a character 36 | //then, store frequency in max and corresponding character in maxChar 37 | if(max < freq[i]) { 38 | max = freq[i]; 39 | maxChar = string[i]; 40 | } 41 | } 42 | 43 | System.out.println("Minimum occurring character: " + minChar); 44 | System.out.println("Maximum occurring character: " + maxChar); 45 | } 46 | -------------------------------------------------------------------------------- /CPP/radixSort.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of Radix Sort 2 | #include 3 | using namespace std; 4 | 5 | // A utility function to get maximum value in arr[] 6 | int getMax(int arr[], int n) 7 | { 8 | int mx = arr[0]; 9 | for (int i = 1; i < n; i++) 10 | if (arr[i] > mx) 11 | mx = arr[i]; 12 | return mx; 13 | } 14 | 15 | // A function to do counting sort of arr[] according to 16 | // the digit represented by exp. 17 | void countSort(int arr[], int n, int exp) 18 | { 19 | int output[n]; // output array 20 | int i, count[10] = { 0 }; 21 | 22 | // Store count of occurrences in count[] 23 | for (i = 0; i < n; i++) 24 | count[(arr[i] / exp) % 10]++; 25 | 26 | // Change count[i] so that count[i] now contains actual 27 | // position of this digit in output[] 28 | for (i = 1; i < 10; i++) 29 | count[i] += count[i - 1]; 30 | 31 | // Build the output array 32 | for (i = n - 1; i >= 0; i--) { 33 | output[count[(arr[i] / exp) % 10] - 1] = arr[i]; 34 | count[(arr[i] / exp) % 10]--; 35 | } 36 | 37 | // Copy the output array to arr[], so that arr[] now 38 | // contains sorted numbers according to current digit 39 | for (i = 0; i < n; i++) 40 | arr[i] = output[i]; 41 | } 42 | 43 | // The main function to that sorts arr[] of size n using 44 | // Radix Sort 45 | void radixsort(int arr[], int n) 46 | { 47 | // Find the maximum number to know number of digits 48 | int m = getMax(arr, n); 49 | 50 | // Do counting sort for every digit. Note that instead 51 | // of passing digit number, exp is passed. exp is 10^i 52 | // where i is current digit number 53 | for (int exp = 1; m / exp > 0; exp *= 10) 54 | countSort(arr, n, exp); 55 | } 56 | 57 | // A utility function to print an array 58 | void print(int arr[], int n) 59 | { 60 | for (int i = 0; i < n; i++) 61 | cout << arr[i] << " "; 62 | } 63 | 64 | // Driver Code 65 | int main() 66 | { 67 | int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; 68 | int n = sizeof(arr) / sizeof(arr[0]); 69 | 70 | // Function Call 71 | radixsort(arr, n); 72 | print(arr, n); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /CPP/Wildcard Matching.cpp: -------------------------------------------------------------------------------- 1 | 2 | Question:- 3 | 4 | Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where: 5 | 6 | '?' Matches any single character. 7 | '*' Matches any sequence of characters (including the empty sequence). 8 | The matching should cover the entire input string (not partial). 9 | 10 | 11 | 12 | Example 1: 13 | 14 | Input: s = "aa", p = "a" 15 | Output: false 16 | Explanation: "a" does not match the entire string "aa". 17 | Example 2: 18 | 19 | Input: s = "aa", p = "*" 20 | Output: true 21 | Explanation: '*' matches any sequence. 22 | Example 3: 23 | 24 | Input: s = "cb", p = "?a" 25 | Output: false 26 | Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. 27 | Example 4: 28 | 29 | Input: s = "adceb", p = "*a*b" 30 | Output: true 31 | Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce". 32 | Example 5: 33 | 34 | Input: s = "acdcb", p = "a*c?b" 35 | Output: false 36 | 37 | 38 | Constraints: 39 | 40 | 0 <= s.length, p.length <= 2000 41 | s contains only lowercase English letters. 42 | p contains only lowercase English letters, '?' or '*'. 43 | 44 | 45 | 46 | Code:- 47 | 48 | class Solution { 49 | public: 50 | bool isMatch(string s, string p) { 51 | vector>dp(p.size()+1,vector(s.size()+1,false)); 52 | 53 | for(int i=p.size();i>=0;i--){ 54 | for(int j=s.size();j>=0;j--){ 55 | if(i==p.size()&&j==s.size()){ 56 | dp[i][j]=true; 57 | }else if(i==p.size()){ 58 | dp[i][j]=false; 59 | }else if(j==s.size()){ 60 | if(p[i]=='*'){ 61 | dp[i][j]=dp[i+1][j]; 62 | }else{ 63 | dp[i][j]=false;} 64 | } 65 | else if(p[i]==s[j]||p[i]=='?'){ 66 | dp[i][j]=dp[i+1][j+1]; 67 | }else if(p[i]=='*'){ 68 | dp[i][j]=dp[i+1][j]||dp[i][j+1]; 69 | }else{ 70 | dp[i][j]=false; 71 | } 72 | } 73 | } 74 | return dp[0][0]; 75 | } 76 | }; 77 | -------------------------------------------------------------------------------- /CPP/Deletion from a Circular Linked List.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to delete a given key from 2 | // linked list. 3 | #include 4 | using namespace std; 5 | 6 | /* structure for a node */ 7 | class Node { 8 | public: 9 | int data; 10 | Node* next; 11 | }; 12 | 13 | /* Function to insert a node at the beginning of 14 | a Circular linked list */ 15 | void push(Node** head_ref, int data) 16 | { 17 | // Create a new node and make head as next 18 | // of it. 19 | Node* ptr1 = new Node(); 20 | ptr1->data = data; 21 | ptr1->next = *head_ref; 22 | 23 | /* If linked list is not NULL then set the 24 | next of last node */ 25 | if (*head_ref != NULL) 26 | { 27 | // Find the node before head and update 28 | // next of it. 29 | Node* temp = *head_ref; 30 | while (temp->next != *head_ref) 31 | temp = temp->next; 32 | temp->next = ptr1; 33 | } 34 | else 35 | ptr1->next = ptr1; /*For the first node */ 36 | 37 | *head_ref = ptr1; 38 | } 39 | 40 | /* Function to print nodes in a given 41 | circular linked list */ 42 | void printList(Node* head) 43 | { 44 | Node* temp = head; 45 | if (head != NULL) { 46 | do { 47 | cout << temp->data << " "; 48 | temp = temp->next; 49 | } while (temp != head); 50 | } 51 | 52 | cout << endl; 53 | } 54 | 55 | /* Function to delete a given node from the list */ 56 | void deleteNode(Node** head, int key) 57 | { 58 | 59 | // If linked list is empty 60 | if (*head == NULL) 61 | return; 62 | 63 | // If the list contains only a single node 64 | if((*head)->data==key && (*head)->next==*head) 65 | { 66 | free(*head); 67 | *head=NULL; 68 | return; 69 | } 70 | 71 | Node *last=*head,*d; 72 | 73 | // If head is to be deleted 74 | if((*head)->data==key) 75 | { 76 | 77 | // Find the last node of the list 78 | while(last->next!=*head) 79 | last=last->next; 80 | 81 | // Point last node to the next of head i.e. 82 | // the second node of the list 83 | last->next=(*head)->next; 84 | free(*head); 85 | *head=last->next; 86 | } 87 | 88 | // Either the node to be deleted is not found 89 | // or the end of list is not reached 90 | while(last->next!=*head&&last->next->data!=key) 91 | { 92 | last=last->next; 93 | } 94 | 95 | // If node to be deleted was found 96 | if(last->next->data==key) 97 | { 98 | d=last->next; 99 | last->next=d->next; 100 | free(d); 101 | } 102 | else 103 | cout<<"no such keyfound"; 104 | } 105 | 106 | /* Driver code */ 107 | int main() 108 | { 109 | /* Initialize lists as empty */ 110 | Node* head = NULL; 111 | 112 | /* Created linked list will be 2->5->7->8->10 */ 113 | push(&head, 2); 114 | push(&head, 5); 115 | push(&head, 7); 116 | push(&head, 8); 117 | push(&head, 10); 118 | 119 | cout << "List Before Deletion: "; 120 | printList(head); 121 | 122 | deleteNode(&head, 7); 123 | 124 | cout << "List After Deletion: "; 125 | printList(head); 126 | 127 | return 0; 128 | } 129 | 130 | 131 | -------------------------------------------------------------------------------- /Python/tictactoe.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '] 5 | player = 1 6 | 7 | ########win Flags########## 8 | Win = 1 9 | Draw = -1 10 | Running = 0 11 | Stop = 1 12 | ########################### 13 | Game = Running 14 | Mark = 'X' 15 | 16 | #This Function Draws Game Board 17 | def DrawBoard(): 18 | print(" %c | %c | %c " % (board[1],board[2],board[3])) 19 | print("___|___|___") 20 | print(" %c | %c | %c " % (board[4],board[5],board[6])) 21 | print("___|___|___") 22 | print(" %c | %c | %c " % (board[7],board[8],board[9])) 23 | print(" | | ") 24 | 25 | #This Function Checks position is empty or not 26 | def CheckPosition(x): 27 | if(board[x] == ' '): 28 | return True 29 | else: 30 | return False 31 | 32 | #This Function Checks player has won or not 33 | def CheckWin(): 34 | global Game 35 | #Horizontal winning condition 36 | if(board[1] == board[2] and board[2] == board[3] and board[1] != ' '): 37 | Game = Win 38 | elif(board[4] == board[5] and board[5] == board[6] and board[4] != ' '): 39 | Game = Win 40 | elif(board[7] == board[8] and board[8] == board[9] and board[7] != ' '): 41 | Game = Win 42 | #Vertical Winning Condition 43 | elif(board[1] == board[4] and board[4] == board[7] and board[1] != ' '): 44 | Game = Win 45 | elif(board[2] == board[5] and board[5] == board[8] and board[2] != ' '): 46 | Game = Win 47 | elif(board[3] == board[6] and board[6] == board[9] and board[3] != ' '): 48 | Game=Win 49 | #Diagonal Winning Condition 50 | elif(board[1] == board[5] and board[5] == board[9] and board[5] != ' '): 51 | Game = Win 52 | elif(board[3] == board[5] and board[5] == board[7] and board[5] != ' '): 53 | Game=Win 54 | #Match Tie or Draw Condition 55 | elif(board[1]!=' ' and board[2]!=' ' and board[3]!=' ' and board[4]!=' ' and board[5]!=' ' and board[6]!=' ' and board[7]!=' ' and board[8]!=' ' and board[9]!=' '): 56 | Game=Draw 57 | else: 58 | Game=Running 59 | 60 | print("Tic-Tac-Toe Game Designed By Sourabh Somani") 61 | print("Player 1 [X] --- Player 2 [O]\n") 62 | print() 63 | print() 64 | print("Please Wait...") 65 | time.sleep(3) 66 | while(Game == Running): 67 | os.system('cls') 68 | DrawBoard() 69 | if(player % 2 != 0): 70 | print("Player 1's chance") 71 | Mark = 'X' 72 | else: 73 | print("Player 2's chance") 74 | Mark = 'O' 75 | choice = int(input("Enter the position between [1-9] where you want to mark : ")) 76 | if(CheckPosition(choice)): 77 | board[choice] = Mark 78 | player+=1 79 | CheckWin() 80 | 81 | os.system('cls') 82 | DrawBoard() 83 | if(Game==Draw): 84 | print("Game Draw") 85 | elif(Game==Win): 86 | player-=1 87 | if(player%2!=0): 88 | print("Player 1 Won") 89 | else: 90 | print("Player 2 Won") 91 | -------------------------------------------------------------------------------- /Python/TimSort.py: -------------------------------------------------------------------------------- 1 | # Python3 program to perform basic timSort 2 | MIN_MERGE = 32 3 | 4 | def calcMinRun(n): 5 | """Returns the minimum length of a 6 | run from 23 - 64 so that 7 | the len(array)/minrun is less than or 8 | equal to a power of 2. 9 | 10 | e.g. 1=>1, ..., 63=>63, 64=>32, 65=>33, 11 | ..., 127=>64, 128=>32, ... 12 | """ 13 | r = 0 14 | while n >= MIN_MERGE: 15 | r |= n & 1 16 | n >>= 1 17 | return n + r 18 | 19 | 20 | # This function sorts array from left index to 21 | # to right index which is of size atmost RUN 22 | def insertionSort(arr, left, right): 23 | for i in range(left + 1, right + 1): 24 | j = i 25 | while j > left and arr[j] < arr[j - 1]: 26 | arr[j], arr[j - 1] = arr[j - 1], arr[j] 27 | j -= 1 28 | 29 | 30 | # Merge function merges the sorted runs 31 | def merge(arr, l, m, r): 32 | 33 | # original array is broken in two parts 34 | # left and right array 35 | len1, len2 = m - l + 1, r - m 36 | left, right = [], [] 37 | for i in range(0, len1): 38 | left.append(arr[l + i]) 39 | for i in range(0, len2): 40 | right.append(arr[m + 1 + i]) 41 | 42 | i, j, k = 0, 0, l 43 | 44 | # after comparing, we merge those two array 45 | # in larger sub array 46 | while i < len1 and j < len2: 47 | if left[i] <= right[j]: 48 | arr[k] = left[i] 49 | i += 1 50 | 51 | else: 52 | arr[k] = right[j] 53 | j += 1 54 | 55 | k += 1 56 | 57 | # Copy remaining elements of left, if any 58 | while i < len1: 59 | arr[k] = left[i] 60 | k += 1 61 | i += 1 62 | 63 | # Copy remaining element of right, if any 64 | while j < len2: 65 | arr[k] = right[j] 66 | k += 1 67 | j += 1 68 | 69 | 70 | # Iterative Timsort function to sort the 71 | # array[0...n-1] (similar to merge sort) 72 | def timSort(arr): 73 | n = len(arr) 74 | minRun = calcMinRun(n) 75 | 76 | # Sort individual subarrays of size RUN 77 | for start in range(0, n, minRun): 78 | end = min(start + minRun - 1, n - 1) 79 | insertionSort(arr, start, end) 80 | 81 | # Start merging from size RUN (or 32). It will merge 82 | # to form size 64, then 128, 256 and so on .... 83 | size = minRun 84 | while size < n: 85 | 86 | # Pick starting point of left sub array. We 87 | # are going to merge arr[left..left+size-1] 88 | # and arr[left+size, left+2*size-1] 89 | # After every merge, we increase left by 2*size 90 | for left in range(0, n, 2 * size): 91 | 92 | # Find ending point of left sub array 93 | # mid+1 is starting point of right sub array 94 | mid = min(n - 1, left + size - 1) 95 | right = min((left + 2 * size - 1), (n - 1)) 96 | 97 | # Merge sub array arr[left.....mid] & 98 | # arr[mid+1....right] 99 | if mid < right: 100 | merge(arr, left, mid, right) 101 | 102 | size = 2 * size 103 | 104 | 105 | # Driver program to test above function 106 | if __name__ == "__main__": 107 | 108 | arr = [-2, 7, 15, -14, 0, 15, 0, 109 | 7, -7, -4, -13, 5, 8, -14, 12] 110 | 111 | print("Given Array is") 112 | print(arr) 113 | 114 | # Function Call 115 | timSort(arr) 116 | 117 | print("After Sorting Array is") 118 | print(arr) 119 | # [-14, -14, -13, -7, -4, -2, 0, 0, 120 | 5, 7, 7, 8, 12, 15, 15] 121 | -------------------------------------------------------------------------------- /HiringTest.java: -------------------------------------------------------------------------------- 1 | // This is a codechef problem solution. 2 | 3 | 4 | package CodeChef; 5 | 6 | import java.util.*; 7 | import java.io.*; 8 | 9 | public class HiringTest { 10 | 11 | /********* SOLUTION STARTS HERE ************/ 12 | 13 | private static void solve(FastScanner s1, PrintWriter out) { 14 | int t = s1.nextInt(); 15 | while(t--!=0) { 16 | int n = s1.nextInt(); 17 | int m = s1.nextInt(); 18 | int x = s1.nextInt(); 19 | int y = s1.nextInt(); 20 | String arr[] = new String[n]; 21 | for(int i=0; i=x)||((countC==x-1)&&(countP>=y))){ 39 | System.out.print(1); 40 | } 41 | else { 42 | System.out.print(0); 43 | } 44 | } 45 | System.out.println(); 46 | } 47 | 48 | } 49 | 50 | /************* SOLUTION ENDS HERE **********/ 51 | 52 | /********** TEMPLATE STARTS HERE ***********/ 53 | 54 | public static void main(String[] args) throws IOException { 55 | FastScanner in = new FastScanner(System.in); 56 | PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false); 57 | solve(in, out); 58 | in.close(); 59 | out.close(); 60 | } 61 | 62 | static class FastScanner { 63 | BufferedReader reader; 64 | StringTokenizer st; 65 | 66 | FastScanner(InputStream stream) { 67 | reader = new BufferedReader(new InputStreamReader(stream)); 68 | st = null; 69 | } 70 | 71 | String next() { 72 | while (st == null || !st.hasMoreTokens()) { 73 | try { 74 | String line = reader.readLine(); 75 | if (line == null) { 76 | return null; 77 | } 78 | st = new StringTokenizer(line); 79 | } catch (Exception e) { 80 | throw new RuntimeException(); 81 | } 82 | } 83 | return st.nextToken(); 84 | } 85 | 86 | String nextLine() { 87 | String s = null; 88 | try { 89 | s = reader.readLine(); 90 | } catch (IOException e) { 91 | e.printStackTrace(); 92 | } 93 | return s; 94 | } 95 | 96 | int nextInt() { 97 | return Integer.parseInt(next()); 98 | } 99 | 100 | long nextLong() { 101 | return Long.parseLong(next()); 102 | } 103 | 104 | double nextDouble() { 105 | return Double.parseDouble(next()); 106 | } 107 | 108 | char nextChar() { 109 | return next().charAt(0); 110 | } 111 | 112 | int[] nextIntArray(int n) { 113 | int[] arr = new int[n]; 114 | int i = 0; 115 | while (i < n) { 116 | arr[i++] = nextInt(); 117 | } 118 | return arr; 119 | } 120 | 121 | long[] nextLongArray(int n) { 122 | long[] arr = new long[n]; 123 | int i = 0; 124 | while (i < n) { 125 | arr[i++] = nextLong(); 126 | } 127 | return arr; 128 | } 129 | 130 | int[] nextIntArrayOneBased(int n) { 131 | int[] arr = new int[n + 1]; 132 | int i = 1; 133 | while (i <= n) { 134 | arr[i++] = nextInt(); 135 | } 136 | return arr; 137 | } 138 | 139 | long[] nextLongArrayOneBased(int n) { 140 | long[] arr = new long[n + 1]; 141 | int i = 1; 142 | while (i <= n) { 143 | arr[i++] = nextLong(); 144 | } 145 | return arr; 146 | } 147 | 148 | void close() { 149 | try { 150 | reader.close(); 151 | } catch (IOException e) { 152 | e.printStackTrace(); 153 | } 154 | } 155 | } 156 | 157 | /*********** TEMPLATE ENDS HERE *************/ 158 | } 159 | -------------------------------------------------------------------------------- /Python/Searching_Sorting_Algorithms.py: -------------------------------------------------------------------------------- 1 | ## Linear Search 2 | def linear_Search(list1, n, key): 3 | 4 | # Searching list1 sequentially 5 | for i in range(0, n): 6 | if (list1[i] == key): 7 | return i 8 | return -1 9 | list1 = list(int(i) for i in input().strip().split(' ')) 10 | key = int(input()) 11 | n = len(list1) 12 | res = linear_Search(list1, n, key) 13 | print(res) 14 | 15 | 16 | ## Binary Search 17 | def binary_search(list1,low,high,n): 18 | if low<=high: 19 | mid=(low+high)//2 20 | if list1[mid]==n: 21 | return mid 22 | elif list1[mid]>n: 23 | return binary_search(list1,low,mid-1,n) 24 | else: 25 | return binary_search(list1,mid+1,high,n) 26 | else: 27 | return -1 28 | list1=list(int(i) for i in input().strip().split(" ")) 29 | n=int(input()) 30 | res=binary_search(list1,0,len(list1)-1,n) 31 | print(res) 32 | 33 | ## Selection Sort 34 | def selection(arr): 35 | for i in range(len(arr)): 36 | min=i 37 | for j in range(i+1,len(arr)): 38 | if arr[min]>arr[j]: 39 | min=j 40 | arr[i],arr[min]=arr[min],arr[i] 41 | return arr 42 | arr=[int(x) for x in input().split(" ")] 43 | print(selection(arr)) 44 | 45 | ## Insertion Sort 46 | def insertion(arr): 47 | for i in range(len(arr)): 48 | key=arr[i] 49 | j=i-1 50 | while j>=0 and keyarr[j+1]: 64 | arr[j+1],arr[j]=arr[j],arr[j+1] 65 | swapped=True 66 | if swapped==False: 67 | break 68 | return arr 69 | arr=[int(x) for x in input().split(" ")] 70 | print(bubble(arr)) 71 | 72 | ## Merge Sort 73 | def merge(arr): 74 | if len(arr)>1: 75 | mid=len(arr)//2 76 | L=arr[:mid] 77 | R=arr[mid:] 78 | merge(L) 79 | merge(R) 80 | i=j=k=0 81 | while i= pivot: 122 | j = j - 1 123 | else: 124 | a[i], a[j] = a[j], a[i] 125 | i = i + 1 126 | j = j - 1 127 | 128 | return pivot_index 129 | def quick_sort(a, si, ei): 130 | if si >= ei: 131 | return 132 | 133 | pivot_index = partition(a, si, ei) 134 | quick_sort(a, si, pivot_index-1) 135 | quick_sort(a, pivot_index+1, ei) 136 | 137 | ei=int(input()) 138 | a=list(int(i) for i in input().strip().split(' ')) 139 | quick_sort(a, 0, ei-1) 140 | print(*a) 141 | -------------------------------------------------------------------------------- /TimSort.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to perform TimSort. 2 | #include 3 | using namespace std; 4 | const int RUN = 32; 5 | 6 | // This function sorts array from left index to 7 | // to right index which is of size atmost RUN 8 | void insertionSort(int arr[], int left, int right) 9 | { 10 | for (int i = left + 1; i <= right; i++) 11 | { 12 | int temp = arr[i]; 13 | int j = i - 1; 14 | while (j >= left && arr[j] > temp) 15 | { 16 | arr[j+1] = arr[j]; 17 | j--; 18 | } 19 | arr[j+1] = temp; 20 | } 21 | } 22 | 23 | // Merge function merges the sorted runs 24 | void merge(int arr[], int l, int m, int r) 25 | { 26 | 27 | // Original array is broken in two parts 28 | // left and right array 29 | int len1 = m - l + 1, len2 = r - m; 30 | int left[len1], right[len2]; 31 | for (int i = 0; i < len1; i++) 32 | left[i] = arr[l + i]; 33 | for (int i = 0; i < len2; i++) 34 | right[i] = arr[m + 1 + i]; 35 | 36 | int i = 0; 37 | int j = 0; 38 | int k = l; 39 | 40 | // After comparing, we 41 | // merge those two array 42 | // in larger sub array 43 | while (i < len1 && j < len2) 44 | { 45 | if (left[i] <= right[j]) 46 | { 47 | arr[k] = left[i]; 48 | i++; 49 | } 50 | else 51 | { 52 | arr[k] = right[j]; 53 | j++; 54 | } 55 | k++; 56 | } 57 | 58 | // Copy remaining elements of left, if any 59 | while (i < len1) 60 | { 61 | arr[k] = left[i]; 62 | k++; 63 | i++; 64 | } 65 | 66 | // Copy remaining element of right, if any 67 | while (j < len2) 68 | { 69 | arr[k] = right[j]; 70 | k++; 71 | j++; 72 | } 73 | } 74 | 75 | // Iterative Timsort function to sort the 76 | // array[0...n-1] (similar to merge sort) 77 | void timSort(int arr[], int n) 78 | { 79 | 80 | // Sort individual subarrays of size RUN 81 | for (int i = 0; i < n; i+=RUN) 82 | insertionSort(arr, i, min((i+RUN-1), 83 | (n-1))); 84 | 85 | // Start merging from size RUN (or 32). 86 | // It will merge 87 | // to form size 64, then 128, 256 88 | // and so on .... 89 | for (int size = RUN; size < n; 90 | size = 2*size) 91 | { 92 | 93 | // pick starting point of 94 | // left sub array. We 95 | // are going to merge 96 | // arr[left..left+size-1] 97 | // and arr[left+size, left+2*size-1] 98 | // After every merge, we 99 | // increase left by 2*size 100 | for (int left = 0; left < n; 101 | left += 2*size) 102 | { 103 | 104 | // find ending point of 105 | // left sub array 106 | // mid+1 is starting point 107 | // of right sub array 108 | int mid = left + size - 1; 109 | int right = min((left + 2*size - 1), 110 | (n-1)); 111 | 112 | // merge sub array arr[left.....mid] & 113 | // arr[mid+1....right] 114 | if(mid < right) 115 | merge(arr, left, mid, right); 116 | } 117 | } 118 | } 119 | 120 | // Utility function to print the Array 121 | void printArray(int arr[], int n) 122 | { 123 | for (int i = 0; i < n; i++) 124 | printf("%d ", arr[i]); 125 | printf("\n"); 126 | } 127 | 128 | // Driver program to test above function 129 | int main() 130 | { 131 | int arr[] = {-2, 7, 15, -14, 0, 15, 0, 7, -7, 132 | -4, -13, 5, 8, -14, 12}; 133 | int n = sizeof(arr)/sizeof(arr[0]); 134 | printf("Given Array is\n"); 135 | printArray(arr, n); 136 | 137 | // Function Call 138 | timSort(arr, n); 139 | 140 | printf("After Sorting Array is\n"); 141 | printArray(arr, n); 142 | return 0; 143 | } 144 | -------------------------------------------------------------------------------- /Python/webbrowser.py: -------------------------------------------------------------------------------- 1 | # importing required libraries 2 | from PyQt5.QtCore import * 3 | from PyQt5.QtWidgets import * 4 | from PyQt5.QtGui import * 5 | from PyQt5.QtWebEngineWidgets import * 6 | from PyQt5.QtPrintSupport import * 7 | import os 8 | import sys 9 | 10 | # creating main window class 11 | class MainWindow(QMainWindow): 12 | 13 | # constructor 14 | def __init__(self, *args, **kwargs): 15 | super(MainWindow, self).__init__(*args, **kwargs) 16 | 17 | 18 | # creating a QWebEngineView 19 | self.browser = QWebEngineView() 20 | 21 | # setting default browser url as google 22 | self.browser.setUrl(QUrl("http://google.com")) 23 | 24 | # adding action when url get changed 25 | self.browser.urlChanged.connect(self.update_urlbar) 26 | 27 | # adding action when loading is finished 28 | self.browser.loadFinished.connect(self.update_title) 29 | 30 | # set this browser as central widget or main window 31 | self.setCentralWidget(self.browser) 32 | 33 | # creating a status bar object 34 | self.status = QStatusBar() 35 | 36 | # adding status bar to the main window 37 | self.setStatusBar(self.status) 38 | 39 | # creating QToolBar for navigation 40 | navtb = QToolBar("Navigation") 41 | 42 | # adding this tool bar tot he main window 43 | self.addToolBar(navtb) 44 | 45 | # adding actions to the tool bar 46 | # creating a action for back 47 | back_btn = QAction("🡨", self) 48 | 49 | # setting status tip 50 | back_btn.setStatusTip("Back to previous page") 51 | 52 | # adding action to the back button 53 | # making browser go back 54 | back_btn.triggered.connect(self.browser.back) 55 | 56 | # adding this action to tool bar 57 | navtb.addAction(back_btn) 58 | 59 | # similarly for forward action 60 | next_btn = QAction("🡪", self) 61 | next_btn.setStatusTip("Forward to next page") 62 | 63 | # adding action to the next button 64 | # making browser go forward 65 | next_btn.triggered.connect(self.browser.forward) 66 | navtb.addAction(next_btn) 67 | 68 | # similarly for reload action 69 | reload_btn = QAction("⭮", self) 70 | reload_btn.setStatusTip("Reload page") 71 | 72 | # adding action to the reload button 73 | # making browser to reload 74 | reload_btn.triggered.connect(self.browser.reload) 75 | navtb.addAction(reload_btn) 76 | 77 | # similarly for home action 78 | home_btn = QAction("⌂", self) 79 | home_btn.setStatusTip("Go home") 80 | home_btn.triggered.connect(self.navigate_home) 81 | navtb.addAction(home_btn) 82 | 83 | # adding a separator in the tool bar 84 | navtb.addSeparator() 85 | 86 | # creating a line edit for the url 87 | self.urlbar = QLineEdit() 88 | 89 | # adding action when return key is pressed 90 | self.urlbar.returnPressed.connect(self.navigate_to_url) 91 | 92 | # adding this to the tool bar 93 | navtb.addWidget(self.urlbar) 94 | 95 | # adding stop action to the tool bar 96 | stop_btn = QAction("🛑", self) 97 | stop_btn.setStatusTip("Stop loading current page") 98 | 99 | # adding action to the stop button 100 | # making browser to stop 101 | stop_btn.triggered.connect(self.browser.stop) 102 | navtb.addAction(stop_btn) 103 | 104 | # showing all the components 105 | self.show() 106 | 107 | 108 | # method for updating the title of the window 109 | def update_title(self): 110 | title = self.browser.page().title() 111 | self.setWindowTitle("% s - Geek Browser" % title) 112 | 113 | 114 | # method called by the home action 115 | def navigate_home(self): 116 | 117 | # open the google 118 | self.browser.setUrl(QUrl("http://www.google.com")) 119 | 120 | # method called by the line edit when return key is pressed 121 | def navigate_to_url(self): 122 | 123 | # getting url and converting it to QUrl objetc 124 | q = QUrl(self.urlbar.text()) 125 | 126 | # if url is scheme is blank 127 | if q.scheme() == "": 128 | # set url scheme to html 129 | q.setScheme("http") 130 | 131 | # set the url to the browser 132 | self.browser.setUrl(q) 133 | 134 | # method for updating url 135 | # this method is called by the QWebEngineView object 136 | def update_urlbar(self, q): 137 | 138 | # setting text to the url bar 139 | self.urlbar.setText(q.toString()) 140 | 141 | # setting cursor position of the url bar 142 | self.urlbar.setCursorPosition(0) 143 | 144 | 145 | # creating a pyQt5 application 146 | app = QApplication(sys.argv) 147 | 148 | # setting name to the application 149 | app.setApplicationName("Geek Browser") 150 | 151 | # creating a main window object 152 | window = MainWindow() 153 | 154 | # loop 155 | app.exec_() 156 | -------------------------------------------------------------------------------- /CPP/Infix to postfix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX_SIZE 10 4 | 5 | struct stack 6 | { 7 | char st[MAX_SIZE]; 8 | int top; 9 | }; 10 | struct stack2 11 | { 12 | int st[MAX_SIZE]; 13 | int top; 14 | }; 15 | bool IsOperator(char); 16 | bool IsOperand(char); 17 | bool eqlOrhigher(char, char); 18 | string convert(string); 19 | 20 | 21 | 22 | void pop(struct stack* s) 23 | { 24 | if (s->top==-1) 25 | { 26 | return; 27 | } 28 | else 29 | { 30 | 31 | s->top--; 32 | 33 | } 34 | } 35 | 36 | char pop2(struct stack* s) 37 | { 38 | if (s->top==-1) 39 | { 40 | return '$'; 41 | } 42 | else 43 | { 44 | char temp=s->st[s->top]; 45 | s->top--; 46 | return temp; 47 | } 48 | } 49 | int pop3(struct stack2* s) 50 | { 51 | if (s->top==-1) 52 | { 53 | return '$'; 54 | } 55 | else 56 | { 57 | int temp=s->st[s->top]; 58 | s->top--; 59 | return temp; 60 | } 61 | } 62 | void push(struct stack* s,char item) 63 | { 64 | if(s->top==MAX_SIZE-1) 65 | { 66 | return; 67 | } 68 | else 69 | { 70 | s->top++; 71 | s->st[s->top]=item; 72 | 73 | } 74 | } 75 | void push2(struct stack2* s,int item) 76 | { 77 | if(s->top==MAX_SIZE-1) 78 | { 79 | return ; 80 | } 81 | else 82 | { 83 | s->top++; 84 | s->st[s->top]=item; 85 | 86 | } 87 | } 88 | char top(struct stack* s) 89 | { 90 | return char(s->st[s->top]); 91 | } 92 | 93 | int top2(struct stack2* s) 94 | { 95 | return s->st[s->top]; 96 | } 97 | 98 | 99 | bool IsOperator(char c) 100 | { 101 | if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') 102 | return true; 103 | return false; 104 | } 105 | 106 | 107 | bool IsOperand(char c) 108 | { 109 | if (c >= 'A' && c <= 'Z') 110 | return true; 111 | if (c >= 'a' && c <= 'z') 112 | return true; 113 | return false; 114 | } 115 | 116 | int precedence(char op) 117 | { 118 | if (op == '+' || op == '-') 119 | return 1; 120 | if (op == '*' || op == '/') 121 | return 2; 122 | if (op == '^') 123 | return 3; 124 | return 0; 125 | } 126 | 127 | bool eqlOrhigher(char op1, char op2) 128 | { 129 | int p1 = precedence(op1); 130 | int p2 = precedence(op2); 131 | if (p1 == p2) 132 | { 133 | if (op1 == '^') 134 | return false; 135 | return true; 136 | } 137 | return (p1 > p2 ? true : false); 138 | } 139 | 140 | int Prec(char ch) 141 | { 142 | if (ch=='+' || ch=='-') 143 | { 144 | return 1; 145 | } 146 | else if (ch=='*' || ch=='/') 147 | { 148 | return 3; 149 | } 150 | else if (ch == '^') 151 | { 152 | return 6; 153 | } 154 | } 155 | 156 | int stackPrec(char ch) 157 | { 158 | if (ch=='+' || ch=='-') 159 | { 160 | return 2; 161 | } 162 | else if (ch=='*' || ch=='/') 163 | { 164 | return 4; 165 | } 166 | else if (ch == '^') 167 | { 168 | return 5; 169 | } 170 | return -1; 171 | } 172 | 173 | 174 | void convert(string infix,string* postfix) 175 | { 176 | struct stack s; 177 | s.top=-1; 178 | char ch; 179 | push(&s,'('); 180 | infix += ')'; 181 | 182 | for (int i = 0; i < infix.length(); i++) 183 | { 184 | 185 | ch = infix[i]; 186 | 187 | 188 | if (ch == '(') 189 | push(&s,ch); 190 | else if (IsOperand(ch)) 191 | *postfix += ch; 192 | else if (IsOperator(ch)) 193 | { 194 | while (s.top!=-1 && Prec(ch) < stackPrec(top(&s))) 195 | { 196 | *postfix += top(&s); 197 | pop(&s); 198 | } 199 | push(&s,ch); 200 | } 201 | else if (ch == ')') 202 | { 203 | while (s.top!=-1 && top(&s) != '(') 204 | { 205 | *postfix += top(&s); 206 | pop(&s); 207 | } 208 | pop(&s); 209 | } 210 | } 211 | 212 | } 213 | int calculate(int op1,char oper,int op2) 214 | { 215 | if(oper=='+') 216 | return op2+op1; 217 | else if(oper=='-') 218 | return op2-op1; 219 | else if(oper=='*') 220 | return op2*op1; 221 | else if(oper=='/') 222 | return op2/op1; 223 | else if(oper=='^') 224 | { 225 | int result=1; 226 | int i=op1; 227 | while(i!=0) 228 | { 229 | result = result*op2; 230 | i--; 231 | } 232 | return result; 233 | } 234 | } 235 | int evaluatePostfix(string postfix) 236 | { 237 | 238 | struct stack2 s1; 239 | s1.top=-1; 240 | int i=0; 241 | while(i>temp; 248 | push2(&s1,temp); 249 | 250 | } 251 | else 252 | { 253 | int op1=pop3(&s1); 254 | int op2=pop3(&s1); 255 | char op =postfix[i]; 256 | int value= calculate(op1,op,op2); 257 | push2(&s1,value); 258 | 259 | } 260 | i++; 261 | } 262 | int final = top2(&s1); 263 | return final; 264 | 265 | 266 | 267 | 268 | } 269 | int main() 270 | { 271 | string infix_expression, postfix_expression; 272 | int result; 273 | 274 | cout << " Enter an infix expression: "; 275 | cin >> infix_expression; 276 | convert(infix_expression,&postfix_expression); 277 | cout << "\n Your Infix expression is: " << infix_expression; 278 | cout << "\n Postfix expression is: " << postfix_expression; 279 | cout<<"\n Lets caluculate result"<a[j+1]):\n", 32 | " a[j],a[j+1]=a[j+1],a[j]\n", 33 | " \n", 34 | "a=array(\"i\",[])\n", 35 | "i=0\n", 36 | "while(i<10):\n", 37 | " a.insert(i,randint(0,20))\n", 38 | " i+=1\n", 39 | "bubble(a,10)\n", 40 | "print(\"Bubble Sort\")\n", 41 | "display(a,10)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "id": "confused-stocks", 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "7 5 19 11 20 13 4 16 14 12 \n", 55 | " Array after Insertion sort\n", 56 | "4 5 7 11 12 13 14 16 19 20 " 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "from array import *\n", 62 | "from random import randint\n", 63 | "\n", 64 | "def display(arr,n):\n", 65 | " for i in range(0,n):\n", 66 | " print(a[i], end =\" \")\n", 67 | " \n", 68 | "def insert(arr,n):\n", 69 | " for i in range(1,n):\n", 70 | " temp=a[i]\n", 71 | " j=i-1\n", 72 | " while j>=0 and a[j]>temp:\n", 73 | " a[j+1]=a[j]\n", 74 | " j-=1\n", 75 | " a[j+1]=temp\n", 76 | " \n", 77 | " \n", 78 | "a=array(\"i\",[])\n", 79 | "i=0\n", 80 | "while(i<10):\n", 81 | " a.insert(i,randint(0,20))\n", 82 | " i+=1\n", 83 | "display(a,10)\n", 84 | "insert(a,10)\n", 85 | "\n", 86 | "print(\"\\n Array after Insertion sort\")\n", 87 | "display(a,10)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 3, 93 | "id": "enhanced-castle", 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "Array before Sorting\n", 101 | "13 2 10 13 0 3 5 16 13 3 \n", 102 | "Array after Selection sorting\n", 103 | "0 2 3 3 5 10 13 13 13 16 " 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "from array import *\n", 109 | "from random import randint\n", 110 | "\n", 111 | "def display(arr,n):\n", 112 | " for i in range(0,n):\n", 113 | " print(a[i], end =\" \")\n", 114 | " \n", 115 | "def select(arr,n):\n", 116 | " for i in range(0, n-1):\n", 117 | " min=i\n", 118 | " for j in range(i+1,n):\n", 119 | " if a[j]=0 and key < customList[j]:\n", 266 | " customList[j+1] = customList[j]\n", 267 | " j -= 1\n", 268 | " customList[j+1] = key\n", 269 | " return customList\n", 270 | "\n", 271 | "\n", 272 | "def bucket(customList):\n", 273 | " numberofBuckets = round(math.sqrt(len(customList)))\n", 274 | " maxValue = max(customList)\n", 275 | " arr = []\n", 276 | "\n", 277 | " for i in range(numberofBuckets):\n", 278 | " arr.append([])\n", 279 | " for j in customList:\n", 280 | " index_b = math.ceil(j*numberofBuckets/maxValue)\n", 281 | " arr[index_b-1].append(j)\n", 282 | " \n", 283 | " for i in range(numberofBuckets):\n", 284 | " arr[i] = insertionSort(arr[i])\n", 285 | " \n", 286 | " k = 0\n", 287 | " for i in range(numberofBuckets):\n", 288 | " for j in range(len(arr[i])):\n", 289 | " customList[k] = arr[i][j]\n", 290 | " k += 1\n", 291 | " return customList \n", 292 | " \n", 293 | "a=array(\"i\",[])\n", 294 | "i=0\n", 295 | "while(i<10):\n", 296 | " a.insert(i,randint(0,20))\n", 297 | " i+=1\n", 298 | "display(a,10)\n", 299 | "print(\"\\n Array after Bucket sort\")\n", 300 | "display(bucket(a),len(a))\n" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 6, 306 | "id": "affecting-butterfly", 307 | "metadata": {}, 308 | "outputs": [ 309 | { 310 | "name": "stdout", 311 | "output_type": "stream", 312 | "text": [ 313 | "4 3 12 20 3 5 2 2 2 14 \n", 314 | "Array after Quick Sort \n", 315 | "2 2 2 3 3 4 5 12 14 20 " 316 | ] 317 | } 318 | ], 319 | "source": [ 320 | "from array import *\n", 321 | "from random import randint\n", 322 | "\n", 323 | "def display(a,n):\n", 324 | " for i in range(0,n):\n", 325 | " print(a[i], end =\" \")\n", 326 | " \n", 327 | "def partition(a,low,high):\n", 328 | " i=low-1\n", 329 | " pivot=a[high]\n", 330 | " for j in range(low, high):\n", 331 | " if (a[j]<=pivot):\n", 332 | " i+=1\n", 333 | " a[i],a[j]=a[j],a[i]\n", 334 | " a[i+1],a[high]=a[high],a[i+1]\n", 335 | " return (i+1)\n", 336 | " \n", 337 | "def QuickSort(a,low,high):\n", 338 | " if (low 1...> 3...> 4...> 2...> 5...> 6...> \n", 414 | "3--> 1--> 4--> 0--> 5--> 2--> 6--> \n", 415 | "3=> 4=> 1=> 5=> 6=> 2=> 0=> \n", 416 | "0 ->1 ->2 ->3 ->4 ->5 ->6 ->deleted\n", 417 | "6 ->1 ->2 ->3 ->4 ->5 ->Successfully deleted\n" 418 | ] 419 | } 420 | ], 421 | "source": [ 422 | "class BinTree:\n", 423 | " def __init__(self,size):\n", 424 | " self.customList=size *[None]\n", 425 | " self.lastUsedIndex=0\n", 426 | " self.maxSize=size\n", 427 | " \n", 428 | " def insertNode(self,value):\n", 429 | " if self.lastUsedIndex+1==self.maxSize:\n", 430 | " return (\"Binary tree is full\")\n", 431 | " self.customList[self.lastUsedIndex+1]=value\n", 432 | " self.lastUsedIndex+=1\n", 433 | " return \"Added\"\n", 434 | " \n", 435 | " def search(self,key):\n", 436 | " for i in range (len(self.customList)):\n", 437 | " if self.customList[i]==key:\n", 438 | " return True\n", 439 | " return False\n", 440 | " \n", 441 | " def preorder(self,index):\n", 442 | " if index >self.lastUsedIndex:\n", 443 | " return\n", 444 | " print(self.customList[index], end= \"...> \")\n", 445 | " self.preorder(index*2)\n", 446 | " self.preorder(index*2 +1) \n", 447 | " \n", 448 | " def inorder(self,index):\n", 449 | " if index >self.lastUsedIndex:\n", 450 | " return\n", 451 | " self.inorder(index*2)\n", 452 | " print(self.customList[index], end= \"--> \")\n", 453 | " self.inorder(index*2 +1) \n", 454 | " \n", 455 | " def postorder(self,index):\n", 456 | " if index >self.lastUsedIndex:\n", 457 | " return\n", 458 | " self.postorder(index*2)\n", 459 | " self.postorder(index*2 +1) \n", 460 | " print(self.customList[index], end= \"=> \")\n", 461 | " \n", 462 | " def levelorder(self,index):\n", 463 | " for i in range(index, self.lastUsedIndex+1):\n", 464 | " print(self.customList[i], end = \" ->\")\n", 465 | " \n", 466 | " \n", 467 | " #deepest node= index of last used list\n", 468 | " \n", 469 | " def del_elmt(self, key):\n", 470 | " if self.lastUsedIndex==0:\n", 471 | " return None\n", 472 | " \n", 473 | " deepestNode=self.customList[self.lastUsedIndex]\n", 474 | " for i in range(1, self.lastUsedIndex+1):\n", 475 | " if self.customList[i]==key:\n", 476 | " self.customList[i]=self.customList[self.lastUsedIndex]\n", 477 | " self.customList[self.lastUsedIndex]=None\n", 478 | " self.lastUsedIndex-=1\n", 479 | " return \"deleted\"\n", 480 | " \n", 481 | " def delBT(self):\n", 482 | " self.customList = None\n", 483 | " return 'Successfully deleted'\n", 484 | " \n", 485 | " \n", 486 | "newBt=BinTree(8)\n", 487 | "i=0\n", 488 | "while i<7:\n", 489 | " newBt.insertNode(i)\n", 490 | " i+=1\n", 491 | "\n", 492 | "print(newBt.search(2))\n", 493 | "newBt.preorder(1)\n", 494 | "print()\n", 495 | "newBt.inorder(1)\n", 496 | "print()\n", 497 | "newBt.postorder(1)\n", 498 | "print()\n", 499 | "newBt.levelorder(1)\n", 500 | "print(newBt.del_elmt(0))\n", 501 | "newBt.levelorder(1)\n", 502 | "print(newBt.delBT())" 503 | ] 504 | }, 505 | { 506 | "cell_type": "markdown", 507 | "id": "answering-activity", 508 | "metadata": {}, 509 | "source": [ 510 | "## Binary search tree\n" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 5, 516 | "id": "presidential-receipt", 517 | "metadata": { 518 | "scrolled": true 519 | }, 520 | "outputs": [ 521 | { 522 | "name": "stdout", 523 | "output_type": "stream", 524 | "text": [ 525 | "73\n", 526 | "40\n", 527 | "50\n", 528 | "\n", 529 | "40\n", 530 | "50\n", 531 | "73\n", 532 | "\n", 533 | "50 40 73 \n", 534 | "73\n", 535 | "50\n" 536 | ] 537 | } 538 | ], 539 | "source": [ 540 | "\n", 541 | "\n", 542 | "class Node:\n", 543 | " def __init__(self, value=None):\n", 544 | " self.value = value\n", 545 | " self.next = None\n", 546 | " \n", 547 | " def __str__(self):\n", 548 | " return str(self.value)\n", 549 | "\n", 550 | "class LinkedList:\n", 551 | " def __init__(self):\n", 552 | " self.head = None\n", 553 | " self.tail = None\n", 554 | " \n", 555 | " \n", 556 | "\n", 557 | "class Queue:\n", 558 | " def __init__(self):\n", 559 | " self.linkedList = LinkedList()\n", 560 | " \n", 561 | " def __str__(self):\n", 562 | " values = [str(x) for x in self.linkedList]\n", 563 | " return ' '.join(values)\n", 564 | " \n", 565 | " def enqueue(self, value):\n", 566 | " newNode = Node(value)\n", 567 | " if self.linkedList.head == None:\n", 568 | " self.linkedList.head = newNode\n", 569 | " self.linkedList.tail = newNode\n", 570 | " else:\n", 571 | " self.linkedList.tail.next = newNode\n", 572 | " self.linkedList.tail = newNode\n", 573 | " \n", 574 | " def isEmpty(self):\n", 575 | " if self.linkedList.head == None:\n", 576 | " return True\n", 577 | " else:\n", 578 | " return False\n", 579 | " \n", 580 | " def dequeue(self):\n", 581 | " if self.isEmpty():\n", 582 | " return \"There is not any node in the Queue\"\n", 583 | " else:\n", 584 | " tempNode = self.linkedList.head\n", 585 | " if self.linkedList.head == self.linkedList.tail:\n", 586 | " self.linkedList.head = None\n", 587 | " self.linkedList.tail = None\n", 588 | " else:\n", 589 | " self.linkedList.head = self.linkedList.head.next\n", 590 | " return tempNode\n", 591 | " \n", 592 | " def peek(self):\n", 593 | " if self.isEmpty():\n", 594 | " return \"There is not any node in the Queue\"\n", 595 | " else:\n", 596 | " return self.linkedList.head\n", 597 | " \n", 598 | " def delete(self):\n", 599 | " self.linkedList.head = None\n", 600 | " self.linkedList.tail = None\n", 601 | "\n", 602 | "\n", 603 | "\n", 604 | "class tree:\n", 605 | " def __init__(self,value):\n", 606 | " self.value=value\n", 607 | " self.lchild=None\n", 608 | " self.rchild=None\n", 609 | " \n", 610 | "def insert(root,data): \n", 611 | " if root is None:\n", 612 | " root.value=data\n", 613 | " return \n", 614 | " \n", 615 | " elif data <= root.value:\n", 616 | " if root.lchild is None:\n", 617 | " root.lchild=tree(data)\n", 618 | " else:\n", 619 | " insert(root.lchild,data)\n", 620 | " else:\n", 621 | " if root.rchild is None:\n", 622 | " root.rchild=tree(data)\n", 623 | " else:\n", 624 | " insert(root.rchild,data)\n", 625 | " \n", 626 | "def inserts(root, value):\n", 627 | " if root.value == None:\n", 628 | " root.value= value\n", 629 | " elif value <= root.value:\n", 630 | " if root.lchild is None:\n", 631 | " root.lchild =tree(value)\n", 632 | " else:\n", 633 | " inserts(root.lchild, value)\n", 634 | " else:\n", 635 | " if root.rchild is None:\n", 636 | " root.rchild = tree(value)\n", 637 | " else:\n", 638 | " inserts(root.rchild, value)\n", 639 | " return \"The node has been successfully inserted\"\n", 640 | "\n", 641 | "def preOrder(root):\n", 642 | " if not root:\n", 643 | " return \n", 644 | " print(root.value)\n", 645 | " preOrder(root.lchild)\n", 646 | " preOrder(root.rchild)\n", 647 | "\n", 648 | "def inOrder(root):\n", 649 | " if not root:\n", 650 | " return \n", 651 | " inOrder(root.lchild)\n", 652 | " print(root.value)\n", 653 | " inOrder(root.rchild)\n", 654 | "\n", 655 | "def postOrder(root):\n", 656 | " if not root:\n", 657 | " return \n", 658 | "\n", 659 | " postOrder(root.lchild)\n", 660 | " postOrder(root.rchild)\n", 661 | " print(root.value , end =\" \") \n", 662 | "\n", 663 | "def search(root,key):\n", 664 | " if root is None:\n", 665 | " return\n", 666 | " else:\n", 667 | " if root==key:\n", 668 | " return True\n", 669 | " elif root.value:\n", 670 | " if root.lchild.value == key:\n", 671 | " return True\n", 672 | " else:\n", 673 | " search(root.lchild,key)\n", 674 | " \n", 675 | " else:\n", 676 | " if root.rchild.value == key:\n", 677 | " return True\n", 678 | " else:\n", 679 | " search(root.rchild,key)\n", 680 | " return False\n", 681 | "\n", 682 | "def searchNode(rootNode, nodeValue):\n", 683 | " if rootNode.value == nodeValue:\n", 684 | " return True\n", 685 | " elif nodeValue < rootNode.value:\n", 686 | " if rootNode.lchild.value== nodeValue:\n", 687 | " return True\n", 688 | " else:\n", 689 | " searchNode(rootNode.lchild, nodeValue)\n", 690 | " else:\n", 691 | " if rootNode.rchild.value == nodeValue:\n", 692 | " return True\n", 693 | " else:\n", 694 | " searchNode(rootNode.rchild, nodeValue) \n", 695 | " \n", 696 | "def levelOrderTraversal(rootNode):\n", 697 | " if not rootNode:\n", 698 | " return\n", 699 | " else:\n", 700 | " customQueue = Queue()\n", 701 | " customQueue.enqueue(rootNode)\n", 702 | " while not(customQueue.isEmpty()):\n", 703 | " root = customQueue.dequeue()\n", 704 | " print(root.value.value)\n", 705 | " if root.value.lchild is not None:\n", 706 | " customQueue.enqueue(root.value.lchild)\n", 707 | " if root.value.rchild is not None:\n", 708 | " customQueue.enqueue(root.value.rchild)\n", 709 | "\n", 710 | " \n", 711 | "def minValueNode(bstNode):\n", 712 | " current=bstNode\n", 713 | " while current.lchild is not None:\n", 714 | " current=current.lchild\n", 715 | " return current\n", 716 | "\n", 717 | "def dele(root,value):\n", 718 | " if root is None:\n", 719 | " return\n", 720 | " else:\n", 721 | " if root.value> value:\n", 722 | " root.lchild=dele(root.lchild,value)\n", 723 | " elif value>root.value:\n", 724 | " root.rchild=dele(root.rchild,value)\n", 725 | " else:\n", 726 | " if root.lchild is None:\n", 727 | " temp=root.rchild\n", 728 | " root=None\n", 729 | " return temp\n", 730 | " if root.rchild is None:\n", 731 | " temp=root.lchild\n", 732 | " root=None\n", 733 | " return temp\n", 734 | " \n", 735 | " temp=minValueNode(root.rchild)\n", 736 | " root.value=temp.value\n", 737 | " root.rchild=dele(root.rchild,temp.data)\n", 738 | " return root\n", 739 | "def del_BST(root):\n", 740 | " root=None\n", 741 | " root.lchild=None\n", 742 | " root.rchild=None\n", 743 | " print(\"deleted\")\n", 744 | " \n", 745 | "BST=tree(None)\n", 746 | "inserts(BST,73)\n", 747 | "inserts(BST,40)\n", 748 | "inserts(BST,50)\n", 749 | "preOrder(BST)\n", 750 | "print()\n", 751 | "inOrder(BST)\n", 752 | "print()\n", 753 | "postOrder(BST)\n", 754 | "#print(searchNode(BST,73))\n", 755 | "dele(BST,40)\n", 756 | "print()\n", 757 | "levelOrderTraversal(BST)" 758 | ] 759 | }, 760 | { 761 | "cell_type": "markdown", 762 | "id": "biological-carry", 763 | "metadata": {}, 764 | "source": [ 765 | " ## AVL trees\n", 766 | " 1. Reduce the time complexity of searching (in skewed tree take O(n) in avl takes(O(log(n))" 767 | ] 768 | }, 769 | { 770 | "cell_type": "code", 771 | "execution_count": 6, 772 | "id": "nearby-table", 773 | "metadata": {}, 774 | "outputs": [ 775 | { 776 | "name": "stdout", 777 | "output_type": "stream", 778 | "text": [ 779 | "PREORDER\n", 780 | "5\n", 781 | "3\n", 782 | "7\n", 783 | "PREORDER\n", 784 | "5\n", 785 | "2\n", 786 | "1\n", 787 | "3\n", 788 | "7\n", 789 | "PREORDER\n", 790 | "3\n", 791 | "2\n", 792 | "1\n", 793 | "5\n", 794 | "4\n", 795 | "8\n", 796 | "6\n", 797 | "9\n", 798 | "PREORDER\n", 799 | "4\n", 800 | "2\n", 801 | "1\n", 802 | "8\n", 803 | "5\n", 804 | "6\n", 805 | "9\n" 806 | ] 807 | } 808 | ], 809 | "source": [ 810 | "class node:\n", 811 | " def __init__(self, num):\n", 812 | " self.value = num\n", 813 | " self.left = None\n", 814 | " self.right = None\n", 815 | " self.height = 1\n", 816 | "\n", 817 | "class AVL:\n", 818 | "\n", 819 | " def height(self, Node):\n", 820 | " if Node is None:\n", 821 | " return 0\n", 822 | " else:\n", 823 | " return Node.height\n", 824 | "\n", 825 | " def balance(self, Node):\n", 826 | " if Node is None:\n", 827 | " return 0\n", 828 | " else:\n", 829 | " return self.height(Node.left) - self.height(Node.right)\n", 830 | "\n", 831 | " def MinimumValueNode(self, Node):\n", 832 | " if Node is None or Node.left is None:\n", 833 | " return Node\n", 834 | " else:\n", 835 | " return self.MinimumValueNode(Node.left)\n", 836 | "\n", 837 | " def rotateR(self, Node):\n", 838 | " a = Node.left\n", 839 | " b = a.right\n", 840 | " a.right = Node\n", 841 | " Node.left = b\n", 842 | " Node.height = 1 + max(self.height(Node.left), self.height(Node.right))\n", 843 | " a.height = 1 + max(self.height(a.left), self.height(a.right))\n", 844 | " return a\n", 845 | "\n", 846 | " def rotateL(self, Node):\n", 847 | " a = Node.right\n", 848 | " b = a.left\n", 849 | " a.left = Node\n", 850 | " Node.right = b\n", 851 | " Node.height = 1 + max(self.height(Node.left), self.height(Node.right))\n", 852 | " a.height = 1 + max(self.height(a.left), self.height(a.right))\n", 853 | " return a\n", 854 | "\n", 855 | " def insert(self, val, root):\n", 856 | " if root is None:\n", 857 | " return node(val)\n", 858 | " elif val <= root.value:\n", 859 | " root.left = self.insert(val, root.left)\n", 860 | " elif val > root.value:\n", 861 | " root.right = self.insert(val, root.right)\n", 862 | " root.height = 1 + max(self.height(root.left), self.height(root.right))\n", 863 | " balance = self.balance(root)\n", 864 | " if balance > 1 and root.left.value > val:\n", 865 | " return self.rotateR(root)\n", 866 | " if balance < -1 and val > root.right.value:\n", 867 | " return self.rotateL(root)\n", 868 | " if balance > 1 and val > root.left.value:\n", 869 | " root.left = self.rotateL(root.left)\n", 870 | " return self.rotateR(root)\n", 871 | " if balance < -1 and val < root.right.value:\n", 872 | " root.right = self.rotateR(root.right)\n", 873 | " return self.rotateL(root)\n", 874 | " return root\n", 875 | "\n", 876 | " def preorder(self, root):\n", 877 | " if root is None:\n", 878 | " return\n", 879 | " print(root.value)\n", 880 | " self.preorder(root.left)\n", 881 | " self.preorder(root.right)\n", 882 | " \n", 883 | " def delete(self, val, Node):\n", 884 | " if Node is None:\n", 885 | " return Node\n", 886 | " elif val < Node.value:\n", 887 | " Node.left = self.delete(val, Node.left)\n", 888 | " elif val > Node.value:\n", 889 | " Node.right = self.delete(val, Node.right)\n", 890 | " else:\n", 891 | " if Node.left is None:\n", 892 | " lt = Node.right\n", 893 | " Node = None\n", 894 | " return lt\n", 895 | " elif Node.right is None:\n", 896 | " lt = Node.left\n", 897 | " Node = None\n", 898 | " return lt\n", 899 | " rgt = self.MinimumValueNode(Node.right)\n", 900 | " Node.value = rgt.value\n", 901 | " Node.right = self.delete(rgt.value, Node.right)\n", 902 | " if Node is None:\n", 903 | " return Node\n", 904 | " Node.height = 1 + max(self.height(Node.left), self.height(Node.right))\n", 905 | " balance = self.balance(Node)\n", 906 | " if balance > 1 and self.balance(Node.left) >= 0:\n", 907 | " return self.rotateR(Node)\n", 908 | " if balance < -1 and self.balance(Node.right) <= 0:\n", 909 | " return self.rotateL(Node)\n", 910 | " if balance > 1 and self.balance(Node.left) < 0:\n", 911 | " Node.left = self.rotateL(Node.left)\n", 912 | " return self.rotateR(Node)\n", 913 | " if balance < -1 and self.balance(Node.right) > 0:\n", 914 | " Node.right = self.rotateR(Node.right)\n", 915 | " return self.rotateL(Node)\n", 916 | " return Node\n", 917 | "\n", 918 | " \n", 919 | "Tree=AVL()\n", 920 | "rt=None\n", 921 | "rt=Tree.insert(3,rt)\n", 922 | "rt = Tree.insert(5, rt)\n", 923 | "rt = Tree.insert(7, rt)\n", 924 | "print(\"PREORDER\")\n", 925 | "Tree.preorder(rt)\n", 926 | "rt = Tree.insert(1, rt)\n", 927 | "rt = Tree.insert(2, rt)\n", 928 | "print(\"PREORDER\")\n", 929 | "Tree.preorder(rt)\n", 930 | "rt = Tree.insert(4, rt)\n", 931 | "rt = Tree.insert(6, rt)\n", 932 | "rt = Tree.delete(7, rt)\n", 933 | "rt = Tree.insert(8, rt)\n", 934 | "rt = Tree.insert(9, rt)\n", 935 | "print(\"PREORDER\")\n", 936 | "Tree.preorder(rt)\n", 937 | "rt = Tree.delete(3, rt)\n", 938 | "print(\"PREORDER\")\n", 939 | "Tree.preorder(rt)" 940 | ] 941 | }, 942 | { 943 | "cell_type": "code", 944 | "execution_count": null, 945 | "id": "restricted-china", 946 | "metadata": {}, 947 | "outputs": [], 948 | "source": [ 949 | "class Node:\n", 950 | " def __init__(self,data):\n", 951 | " self.data=data\n", 952 | " self.left=None\n", 953 | " self.right=None\n", 954 | " \n", 955 | "class Tree:\n", 956 | " def add(self,value,root):\n", 957 | " " 958 | ] 959 | } 960 | ], 961 | "metadata": { 962 | "kernelspec": { 963 | "display_name": "Python 3", 964 | "language": "python", 965 | "name": "python3" 966 | }, 967 | "language_info": { 968 | "codemirror_mode": { 969 | "name": "ipython", 970 | "version": 3 971 | }, 972 | "file_extension": ".py", 973 | "mimetype": "text/x-python", 974 | "name": "python", 975 | "nbconvert_exporter": "python", 976 | "pygments_lexer": "ipython3", 977 | "version": "3.8.5" 978 | } 979 | }, 980 | "nbformat": 4, 981 | "nbformat_minor": 5 982 | } 983 | --------------------------------------------------------------------------------