diff --git a/Kadanes_Algorithm.py b/Kadanes_Algorithm.py new file mode 100644 index 0000000..06b8f9e --- /dev/null +++ b/Kadanes_Algorithm.py @@ -0,0 +1,18 @@ +def maxSubArraySum(arr,size): + + max_till_now = arr[0] + max_ending = 0 + + for i in range(0, size): + max_ending = max_ending + arr[i] + if max_ending < 0: + max_ending = 0 + + + elif (max_till_now < max_ending): + max_till_now = max_ending + + return max_till_now + +arr = [-2, -3, 4, -1, -2, 5, -3] +print("Maximum Sub Array Sum Is" , maxSubArraySum(arr,len(arr))) diff --git a/Merge Sort b/Merge Sort new file mode 100644 index 0000000..c48ec79 --- /dev/null +++ b/Merge Sort @@ -0,0 +1,80 @@ +# Python program for implementation of MergeSort + +# Merges two subarrays of arr[]. +# First subarray is arr[l..m] +# Second subarray is arr[m+1..r] + + +def merge(arr, l, m, r): + n1 = m - l + 1 + n2 = r - m + + # create temp arrays + L = [0] * (n1) + R = [0] * (n2) + + # Copy data to temp arrays L[] and R[] + for i in range(0, n1): + L[i] = arr[l + i] + + for j in range(0, n2): + R[j] = arr[m + 1 + j] + + # Merge the temp arrays back into arr[l..r] + i = 0 # Initial index of first subarray + j = 0 # Initial index of second subarray + k = l # Initial index of merged subarray + + while i < n1 and j < n2: + if L[i] <= R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + + # Copy the remaining elements of L[], if there + # are any + while i < n1: + arr[k] = L[i] + i += 1 + k += 1 + + # Copy the remaining elements of R[], if there + # are any + while j < n2: + arr[k] = R[j] + j += 1 + k += 1 + +# l is for left index and r is right index of the +# sub-array of arr to be sorted + + +def mergeSort(arr, l, r): + if l < r: + + # Same as (l+r)//2, but avoids overflow for + # large l and h + m = l+(r-l)//2 + + # Sort first and second halves + mergeSort(arr, l, m) + mergeSort(arr, m+1, r) + merge(arr, l, m, r) + + +# Driver code to test above +arr = [12, 11, 13, 5, 6, 7] +n = len(arr) +print("Given array is") +for i in range(n): + print("%d" % arr[i]), + +mergeSort(arr, 0, n-1) +print("\n\nSorted array is") +for i in range(n): + print("%d" % arr[i]), + +# This code is contributed by Mohit Kumra diff --git a/MergeSort.py b/MergeSort.py new file mode 100644 index 0000000..615c286 --- /dev/null +++ b/MergeSort.py @@ -0,0 +1,71 @@ +def merge(arr, l, m, r): + n1 = m - l + 1 + n2 = r - m + + # create temp arrays + L = [0] * (n1) + R = [0] * (n2) + + # Copy data to temp arrays L[] and R[] + for i in range(0, n1): + L[i] = arr[l + i] + + for j in range(0, n2): + R[j] = arr[m + 1 + j] + + # Merge the temp arrays back into arr[l..r] + i = 0 # Initial index of first subarray + j = 0 # Initial index of second subarray + k = l # Initial index of merged subarray + + while i < n1 and j < n2: + if L[i] <= R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + + # Copy the remaining elements of L[], if there + # are any + while i < n1: + arr[k] = L[i] + i += 1 + k += 1 + + # Copy the remaining elements of R[], if there + # are any + while j < n2: + arr[k] = R[j] + j += 1 + k += 1 + +# l is for left index and r is right index of the +# sub-array of arr to be sorted + + +def mergeSort(arr, l, r): + if l < r: + + # Same as (l+r)//2, but avoids overflow for + # large l and h + m = l+(r-l)//2 + + # Sort first and second halves + mergeSort(arr, l, m) + mergeSort(arr, m+1, r) + merge(arr, l, m, r) + + +# Driver code to test above +arr = [12, 11, 13, 5, 6, 7] +n = len(arr) +print("Given array is") +for i in range(n): + print("%d" % arr[i]), + +mergeSort(arr, 0, n-1) +print("\n\nSorted array is") +for i in range(n): + print("%d" % arr[i]) diff --git a/README.md b/README.md index c0b6a11..56568dc 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ ### This repo contains sorting program in Python. #### List of programs available here : - - Bubble Sort + - Bubble Sort:is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. - Insertion Sort - Heap Sort - Maximum of two numbers in Python - Python Program for simple interest - Python Program for factorial of a number + - Django and Flask are most Popular Python Framework + - Python use for ML Studies Also + - python widely use programming Language diff --git a/Selectionsort.py b/Selectionsort.py new file mode 100644 index 0000000..d2462ff --- /dev/null +++ b/Selectionsort.py @@ -0,0 +1,21 @@ +import sys +A = [64, 25, 12, 22, 11] + +# Traverse through all array elements +for i in range(len(A)): + + # Find the minimum element in remaining + # unsorted array + min_idx = i + for j in range(i+1, len(A)): + if A[min_idx] > A[j]: + min_idx = j + + # Swap the found minimum element with + # the first element + A[i], A[min_idx] = A[min_idx], A[i] + +# Driver code to test above +print ("Sorted array") +for i in range(len(A)): + print("%d" %A[i]), diff --git a/Sort numeric strings in a list in Python by Naive Methodby b/Sort numeric strings in a list in Python by Naive Methodby new file mode 100644 index 0000000..1496c3f --- /dev/null +++ b/Sort numeric strings in a list in Python by Naive Methodby @@ -0,0 +1,6 @@ +Naive Method + numeric string sorting +for i in range(0, len(test_list)) : + test_list[i] = int(test_list[i]) +test_list.sort() +print ("The resultant sorted list : " + str(test_list)) diff --git a/add two number b/add two number new file mode 100644 index 0000000..8fbba12 --- /dev/null +++ b/add two number @@ -0,0 +1,10 @@ +# This program adds two numbers + +num1 = 1.5 +num2 = 6.3 + +# Add two numbers +sum = num1 + num2 + +# Display the sum +print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) diff --git a/area.py b/area.py new file mode 100644 index 0000000..17b6b8e --- /dev/null +++ b/area.py @@ -0,0 +1,10 @@ +#program to find area of circle using functions +import math + +#Function that calculates area of circle +def area_of_circle(r): + a = r**2 * math.pi + return a + +r = float(input("Enter the radius of the circle: ")) +print("%.2f" %area_of_circle(r)) \ No newline at end of file diff --git a/bullcowgame.py b/bullcowgame.py new file mode 100644 index 0000000..99add60 --- /dev/null +++ b/bullcowgame.py @@ -0,0 +1,60 @@ +import random as rand +import emoji as emo + +emo_cow = emo.emojize(emo.demojize('🐄')) +emo_bull = emo.emojize(emo.demojize('🐂')) +print('\n\033[1m' + ' --: Welcome to Cows (', emo_cow, ') and Bulls (', emo_bull, ') game :--' + '\033[0m') + +rand_num = rand.randrange(1000, 9999, 1) +rand_num_list = list(str(rand_num).replace("", "")) + +trial = 0 +error_dig = 0 +error_let = 0 + +while True: + user_num = input('Guess the 4 digit number : ') + user_num_list = list(str(user_num).replace("", "")) + + def if_contain_letter(x): + for ele in x: + if ele.isalpha(): + con = True + else: + con = False + return con + + if len(user_num_list) != 4: + print(' DIGIT no. Error : \n Your entered number does not contain 4 digits') + error_dig += 1 + if error_dig > 3: + print('*) WARNING !! What are you doing ? Too much errors ! Be careful !') + + if if_contain_letter(user_num_list): + print('DIGIT type Error :\n inputted number contains letter ') + error_let += 1 + + if not if_contain_letter(user_num_list): + if len(user_num_list) == 4: + a = [] + b = [] + + for i in range(len(user_num_list)): + if rand_num_list[i] == user_num_list[i]: + a.append(user_num_list[i]) + + if rand_num_list[i] != user_num_list[i]: + if user_num_list[i] in rand_num_list: + b.append(user_num_list[i]) + + num_of_cow = len(a) + num_of_bull = len(b) + print(num_of_cow, ' cow (', emo_cow, ')') + print(num_of_bull, ' bull (', emo_bull, ')') + trial += 1 + + if str(user_num) == str(rand_num): + print('\n\033[1m' + '### Congratulations! , Well played ###' + '\033[0m') + print('Trials = ', trial) + print('Errors = ', error_dig + error_let) + break diff --git a/c.py b/c.py new file mode 100644 index 0000000..85a3502 --- /dev/null +++ b/c.py @@ -0,0 +1,29 @@ +def isVowel(c): + return (c == 'A' or c == 'E' or c == 'I' or + c == 'O' or c == 'U' or c == 'a' or + c == 'e' or c == 'i' or c == 'o' or + c == 'u'); + + +def pigLatin(s): + + + length = len(s); + index = -1; + for i in range(length): + if (isVowel(s[i])): + index = i; + break; + + + if (index == -1): + return "-1"; + + + return s[index:] + s[0:index] + "ay"; + +str = pigLatin("graphic"); +if (str == "-1"): + print("No vowels found. Pig Latin not possible"); +else: + print(str); \ No newline at end of file diff --git a/calc.py b/calc.py new file mode 100644 index 0000000..d335840 --- /dev/null +++ b/calc.py @@ -0,0 +1,32 @@ +opt = int(input("enter the option you want to perform : 1.addition 2.multiplication")) +if opt==1: + s=0 + n=int(input("enter how many times you want to sum up :")) + print("enter",n,"numbers : ") + for i in range(0,n): + a=int(input("enter the number")) + s=s+a + print("the result is : ",s) + +elif opt==2: + m=1 + n1=int(input("how many times you want to multiply : ")) + print("enter",n1,"numbers :") + for i in range(0,n1): + + b=int(input("enter the number :")) + m=m*b + print("the result is :",m) +elif opt==3: + n2=int(input("how many times you want to substract :")) + print("enter", n2 ,"numbers: ") + a=int(input("enter the 1st number")) + b=int(input("enter the 2nd number")) + #c=(a-b) + d=(n2-2) + print("you have to enter ", d, "numbers more") + for i in range(0,d): + c=(a-b) + e=int(input("enter the number : ")) + f=c-e + print("the result is :", f) \ No newline at end of file diff --git a/checkconnection.py b/checkconnection.py new file mode 100644 index 0000000..88b9b16 --- /dev/null +++ b/checkconnection.py @@ -0,0 +1,11 @@ +from urllib.request import urlopen ##importing necessary library this library contains all the modules for url status checking + +name=input("enter your name:") ## entering the user name + +def status(): ## defining the function to check the status + try: + urlopen("https://www.youtube.com/",timeout=1) ## if the url is openable then user is connected + print(name,"you are connected to internet") + except: + print(name,"you are not connected to internet") ## otherwise not connected +status() ## calling the status function \ No newline at end of file diff --git a/client-chat.py b/client-chat.py new file mode 100644 index 0000000..bdfe862 --- /dev/null +++ b/client-chat.py @@ -0,0 +1,38 @@ +import socket +import select +import sys + +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +if len(sys.argv) != 3: + print ("Correct usage: script, IP address, port number") + exit() +IP_address = str(sys.argv[1]) +Port = int(sys.argv[2]) +server.connect((IP_address, Port)) + +while True: + + # maintains a list of possible input streams + sockets_list = [sys.stdin, server] + + """ There are two possible input situations. Either the + user wants to give manual input to send to other people, + or the server is sending a message to be printed on the + screen. Select returns from sockets_list, the stream that + is reader for input. So for example, if the server wants + to send a message, then the if condition will hold true + below.If the user wants to send a message, the else + condition will evaluate as true""" + read_sockets,write_socket, error_socket = select.select(sockets_list,[],[]) + + for socks in read_sockets: + if socks == server: + message = socks.recv(2048) + print (message) + else: + message = sys.stdin.readline() + server.send(message) + sys.stdout.write("") + sys.stdout.write(message) + sys.stdout.flush() +server.close() \ No newline at end of file diff --git a/multiplication.py b/multiplication.py new file mode 100644 index 0000000..0b507a5 --- /dev/null +++ b/multiplication.py @@ -0,0 +1,3 @@ +n=int(input("Multiplication Table of:")) +for i in range(1,10+1): + print("{} X {} = {}".format(i,n,i*n)) \ No newline at end of file diff --git a/solve quadratic b/solve quadratic new file mode 100644 index 0000000..9304927 --- /dev/null +++ b/solve quadratic @@ -0,0 +1,17 @@ +# Solve the quadratic equation ax**2 + bx + c = 0 + +# import complex math module +import cmath + +a = 1 +b = 5 +c = 6 + +# calculate the discriminant +d = (b**2) - (4*a*c) + +# find two solutions +sol1 = (-b-cmath.sqrt(d))/(2*a) +sol2 = (-b+cmath.sqrt(d))/(2*a) + +print('The solution are {0} and {1}'.format(sol1,sol2)) diff --git a/sum of even number b/sum of even number new file mode 100644 index 0000000..cfe9bd8 --- /dev/null +++ b/sum of even number @@ -0,0 +1,9 @@ +maximum = int(input(" Please Enter the Maximum Value : ")) +total = 0 + +for number in range(1, maximum+1): + if(number % 2 == 0): + print("{0}".format(number)) + total = total + number + +print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, total)) diff --git a/sum of oddno.py b/sum of oddno.py new file mode 100644 index 0000000..0d9163c --- /dev/null +++ b/sum of oddno.py @@ -0,0 +1,5 @@ +n=int(input("Enter n value:")) +sum=0 +for i in range(1,n+1,2): + sum+=i +print(sum) \ No newline at end of file diff --git a/swap in python b/swap in python new file mode 100644 index 0000000..22a78b5 --- /dev/null +++ b/swap in python @@ -0,0 +1,16 @@ +# Python program to swap two variables + +x = 5 +y = 10 + +# To take inputs from the user +#x = input('Enter value of x: ') +#y = input('Enter value of y: ') + +# create a temporary variable and swap the values +temp = x +x = y +y = temp + +print('The value of x after swapping: {}'.format(x)) +print('The value of y after swapping: {}'.format(y))