diff --git a/python/BuySellStock.py b/python/BuySellStock.py new file mode 100644 index 0000000..fb9621f --- /dev/null +++ b/python/BuySellStock.py @@ -0,0 +1,20 @@ +# Buy and Sell Stock I + +def buyAndSellStock(stocks): + max_value = 0 + minSoFar = stocks[0] + n = len(stocks) + for i in range(1, n): + if minSoFar > stocks[i]: + minSoFar = stocks[i] + if stocks[i] - minSoFar > max_value: + max_value = stocks - minSoFar + return max_value + +# Test 1: +arr = [7,1,5,3,6,4] +print(buyAndSellStock(arr) + +# Test 2: +arr = [7,6,4,3,1] +print(buyAndSellStock(arr) diff --git a/python/CircularQueue.py b/python/CircularQueue.py new file mode 100644 index 0000000..6515b15 --- /dev/null +++ b/python/CircularQueue.py @@ -0,0 +1,68 @@ +# Circular Queue implementation in Python + + +class MyCircularQueue(): + + def __init__(self, k): + self.k = k + self.queue = [None] * k + self.head = self.tail = -1 + + # Insert an element into the circular queue + def enqueue(self, data): + + if ((self.tail + 1) % self.k == self.head): + print("The circular queue is full\n") + + elif (self.head == -1): + self.head = 0 + self.tail = 0 + self.queue[self.tail] = data + else: + self.tail = (self.tail + 1) % self.k + self.queue[self.tail] = data + + # Delete an element from the circular queue + def dequeue(self): + if (self.head == -1): + print("The circular queue is empty\n") + + elif (self.head == self.tail): + temp = self.queue[self.head] + self.head = -1 + self.tail = -1 + return temp + else: + temp = self.queue[self.head] + self.head = (self.head + 1) % self.k + return temp + + def printCQueue(self): + if(self.head == -1): + print("No element in the circular queue") + + elif (self.tail >= self.head): + for i in range(self.head, self.tail + 1): + print(self.queue[i], end=" ") + print() + else: + for i in range(self.head, self.k): + print(self.queue[i], end=" ") + for i in range(0, self.tail + 1): + print(self.queue[i], end=" ") + print() + + +# Your MyCircularQueue object will be instantiated and called as such: +obj = MyCircularQueue(5) +obj.enqueue(1) +obj.enqueue(2) +obj.enqueue(3) +obj.enqueue(4) +obj.enqueue(5) +print("Initial queue") +obj.printCQueue() + +obj.dequeue() +print("After removing an element from the queue") +obj.printCQueue() diff --git a/python/Clock.py b/python/Clock.py new file mode 100644 index 0000000..639a064 --- /dev/null +++ b/python/Clock.py @@ -0,0 +1,65 @@ +#import all the required libraries first + +import sys + +from tkinter import * + +#import time library to obtain current time + +import time + + + +#create a function timing and variable current_time + +def timing(): + + #display current hour,minute,seconds + + current_time = time.strftime("%H : %M : %S") + + #configure the clock + + clock.config(text=current_time) + + #clock will change after every 200 microseconds + + clock.after(200,timing) + + + +#Create a variable that will store our tkinter window + +root=Tk() + +#define size of the window + +root.geometry("600x300") + +#create a variable clock and store label + +#First label will show time, second label will show hour:minute:second, third label will show the top digital clock + +clock=Label(root,font=("times",60,"bold"),bg="blue") + +clock.grid(row=2,column=2,pady=25,padx=100) + +timing() + + + +#create a variable for digital clock + +digital=Label(root,text="Digital Clock",font="times 24 bold") + +digital.grid(row=0,column=2) + + + +nota=Label(root,text="hours minutes seconds",font="times 15 bold") + +nota.grid(row=3,column=2) + + + +root.mainloop() diff --git a/python/Convert a list of Tuples into Dictionary.py b/python/Convert a list of Tuples into Dictionary.py new file mode 100644 index 0000000..a5d97b1 --- /dev/null +++ b/python/Convert a list of Tuples into Dictionary.py @@ -0,0 +1,10 @@ +def Convert(tup, di): + for a, b in tup: + di.setdefault(a, []).append(b) + return di + +# Driver Code +tups = [("akash", 10), ("gaurav", 12), ("anand", 14), + ("suraj", 20), ("akhil", 25), ("ashish", 30)] +dictionary = {} +print (Convert(tups, dictionary)) diff --git a/python/bubble_sort.py b/python/bubble_sort.py new file mode 100644 index 0000000..20ff873 --- /dev/null +++ b/python/bubble_sort.py @@ -0,0 +1,30 @@ +# Python program for implementation of Bubble Sort + +def bubbleSort(arr): + n = len(arr) + + # Traverse through all array elements + for i in range(n): + + # Last i elements are already in place + for j in range(0, n-i-1): + + # traverse the array from 0 to n-i-1 + # Swap if the element found is greater + # than the next element + if arr[j] > arr[j+1] : + arr[j], arr[j+1] = arr[j+1], arr[j] + + +arr = [] +size = int(input()) +while(size): + value = int(input()) + arr.append(value) + size = size - 1 + +bubbleSort(arr) + +print ("Sorted array is:") +for i in range(len(arr)): + print ("%d" %arr[i]), diff --git a/python/calculator_gui.py b/python/calculator_gui.py new file mode 100644 index 0000000..c28ca3f --- /dev/null +++ b/python/calculator_gui.py @@ -0,0 +1,140 @@ +from tkinter import * +import parser +import math + +# Parser help us to solve mathematical operation + +root = Tk() +root.title('CALCULATOR') +root.geometry('670x450') +root.configure(bg='Blue') + +# get the user input and place it in the text field +i = 0 + + +def get_variables(num): + global i + display.insert(i, num) + i += 1 + + +def calculate(): + entire_string = display.get() + try: + a = parser.expr(entire_string).compile() + result = eval(a) + clear_all() + display.insert(0, result) + + except EXCEPTION: + clear_all() + display.insert(0, "ERROR") + + +def factorial(): + n = int(display.get()) + fact = math.factorial(n) + clear_all() + display.insert(0, fact) + + +# adding functionality +def get_operation(operator): + global i + length = len(operator) + display.insert(i, operator) + i += length + + +# deleting entire elements on the screen +def clear_all(): + display.delete(0, END) + + +# deleting single element on the screen +def undo(): + entire_string = display.get() + if len(entire_string): + new_string = entire_string[:-1] + clear_all() + display.insert(0, new_string) + else: + clear_all() + display.insert(0, "ERROR") + + +# adding the input field + +display = Entry(root, font=('Helvetica', '40')) +display.grid(row=1, columnspan=6, padx=45, pady=35, sticky=W + E) + +# adding buttons to calculator +Button(root, text="1", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(1)).grid(row=2, column=0) +Button(root, text="2", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(2)).grid(row=2, column=1) +Button(root, text="3", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(3)).grid(row=2, column=2) + +Button(root, text="4", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(4)).grid(row=3, column=0) +Button(root, text="5", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(5)).grid(row=3, column=1) +Button(root, text="6", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(6)).grid(row=3, column=2) + +Button(root, text="7", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(7)).grid(row=4, column=0) +Button(root, text="8", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(8)).grid(row=4, column=1) +Button(root, text="9", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(9)).grid(row=4, column=2) + +# adding other buttons to the calculator +Button(root, text="AC", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: clear_all()).grid(row=5, column=0) +Button(root, text="0", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_variables(0)).grid(row=5, column=1) +Button(root, text="=", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: calculate()).grid(row=5, column=2) + +Button(root, text="+", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("+")).grid(row=2, + column=3) +Button(root, text="-", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("-")).grid(row=3, + column=3) +Button(root, text="*", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("*")).grid(row=4, + column=3) +Button(root, text="/", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("/")).grid(row=5, + column=3) + +# adding new operations +Button(root, text="pi", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("*3.14")).grid(row=2, + column=4) +Button(root, text="%", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("%")).grid(row=3, + column=4) +Button(root, text="(", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("(")).grid(row=4, + column=4) +Button(root, text="exp", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("**")).grid(row=5, + column=4) + +Button(root, text=">-", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: undo()).grid(row=2, column=5) +Button(root, text="!", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: factorial()).grid(row=3, column=5) +Button(root, text=")", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation(")")).grid(row=4, + column=5) +Button(root, text="^2", bg="Black", fg="White", padx=25, pady=13, font=('Helvetica', '20'), + command=lambda: get_operation("**2")).grid(row=5, + column=5) + +root.mainloop() \ No newline at end of file diff --git a/python/calendar.py b/python/calendar.py new file mode 100644 index 0000000..da52b09 --- /dev/null +++ b/python/calendar.py @@ -0,0 +1,30 @@ +import calendar +from datetime import date +from datetime import datetime + +# handling user exceptions +try: + year = int( input("Enter the year of the required calendar\n")) + month = int( input("Enter the month of the required calendar\n")) + + # getting current date and time with their proper formatting + today = date.today() + today_formatted = today.strftime("%d/%m/%Y") + current_time = datetime.now().strftime("%H:%M:%S") + + # splitting all parts of the time in a list because from there we can get the idea of PM OR AM. + current_time_list = current_time.split(":") + + # checking for the conditions of AM OR PM + if(int(current_time_list[0])>=24): + current_time += " AM" + elif(int(current_time_list[0])>=12): + current_time += " PM" + + + print(f"Current Time : {current_time}\nToday is {today_formatted}\n-------------------") + print(calendar.month(year,month)) + + +except Exception as e: + print("Please make sure your input is in correct format.") diff --git a/python/calwithgui.py b/python/calwithgui.py new file mode 100644 index 0000000..43219d9 --- /dev/null +++ b/python/calwithgui.py @@ -0,0 +1,162 @@ +# Python program to create a simple GUI +# calculator using Tkinter + +# import everything from tkinter module +from tkinter import * + +# globally declare the expression variable +expression = "" + + +# Function to update expression +# in the text entry box +def press(num): + # point out the global expression variable + global expression + + # concatenation of string + expression = expression + str(num) + + # update the expression by using set method + equation.set(expression) + + +# Function to evaluate the final expression +def equalpress(): + # Try and except statement is used + # for handling the errors like zero + # division error etc. + + # Put that code inside the try block + # which may generate the error + try: + + global expression + + # eval function evaluate the expression + # and str function convert the result + # into string + total = str(eval(expression)) + + equation.set(total) + + # initialize the expression variable + # by empty string + expression = "" + + # if error is generate then handle + # by the except block + except: + + equation.set(" error ") + expression = "" + + +# Function to clear the contents +# of text entry box +def clear(): + global expression + expression = "" + equation.set("") + + +# Driver code +if __name__ == "__main__": + # create a GUI window + gui = Tk() + + # set the background colour of GUI window + gui.configure(background="light green") + + # set the title of GUI window + gui.title("Simple Calculator") + + # set the configuration of GUI window + gui.geometry("270x150") + + # StringVar() is the variable class + # we create an instance of this class + equation = StringVar() + + # create the text entry box for + # showing the expression . + expression_field = Entry(gui, textvariable=equation) + + # grid method is used for placing + # the widgets at respective positions + # in table like structure . + expression_field.grid(columnspan=4, ipadx=70) + + # create a Buttons and place at a particular + # location inside the root window . + # when user press the button, the command or + # function affiliated to that button is executed . + button1 = Button(gui, text=' 1 ', fg='black', bg='red', + command=lambda: press(1), height=1, width=7) + button1.grid(row=2, column=0) + + button2 = Button(gui, text=' 2 ', fg='black', bg='red', + command=lambda: press(2), height=1, width=7) + button2.grid(row=2, column=1) + + button3 = Button(gui, text=' 3 ', fg='black', bg='red', + command=lambda: press(3), height=1, width=7) + button3.grid(row=2, column=2) + + button4 = Button(gui, text=' 4 ', fg='black', bg='red', + command=lambda: press(4), height=1, width=7) + button4.grid(row=3, column=0) + + button5 = Button(gui, text=' 5 ', fg='black', bg='red', + command=lambda: press(5), height=1, width=7) + button5.grid(row=3, column=1) + + button6 = Button(gui, text=' 6 ', fg='black', bg='red', + command=lambda: press(6), height=1, width=7) + button6.grid(row=3, column=2) + + button7 = Button(gui, text=' 7 ', fg='black', bg='red', + command=lambda: press(7), height=1, width=7) + button7.grid(row=4, column=0) + + button8 = Button(gui, text=' 8 ', fg='black', bg='red', + command=lambda: press(8), height=1, width=7) + button8.grid(row=4, column=1) + + button9 = Button(gui, text=' 9 ', fg='black', bg='red', + command=lambda: press(9), height=1, width=7) + button9.grid(row=4, column=2) + + button0 = Button(gui, text=' 0 ', fg='black', bg='red', + command=lambda: press(0), height=1, width=7) + button0.grid(row=5, column=0) + + plus = Button(gui, text=' + ', fg='black', bg='red', + command=lambda: press("+"), height=1, width=7) + plus.grid(row=2, column=3) + + minus = Button(gui, text=' - ', fg='black', bg='red', + command=lambda: press("-"), height=1, width=7) + minus.grid(row=3, column=3) + + multiply = Button(gui, text=' * ', fg='black', bg='red', + command=lambda: press("*"), height=1, width=7) + multiply.grid(row=4, column=3) + + divide = Button(gui, text=' / ', fg='black', bg='red', + command=lambda: press("/"), height=1, width=7) + divide.grid(row=5, column=3) + + equal = Button(gui, text=' = ', fg='black', bg='red', + command=equalpress, height=1, width=7) + equal.grid(row=5, column=2) + + clear = Button(gui, text='Clear', fg='black', bg='red', + command=clear, height=1, width=7) + clear.grid(row=5, column='1') + + Decimal= Button(gui, text='.', fg='black', bg='red', + command=lambda: press('.'), height=1, width=7) + Decimal.grid(row=6, column=0) + # start the GUI + gui.mainloop() diff --git a/python/cryptomachine.py b/python/cryptomachine.py new file mode 100644 index 0000000..e2393a1 --- /dev/null +++ b/python/cryptomachine.py @@ -0,0 +1,24 @@ +def machine(): + keys = 'abcdefghijklmnopqrstuvwxyz !' + value = keys[-1] + keys[0:-1] + + encryptDict = dict(zip(keys, value)) + decryptDict = dict(zip(value, keys)) + + message = input("Please enter your secret message : ") + mode = input("Please enter the mode : Encode(E) OR Decode(D) :") + + if mode.upper() == 'E': + newMessage = ''.join([encryptDict[letter] + for letter in message.lower()]) + + elif mode.upper() == 'D': + newMessage = ''.join([decryptDict[letter] + for letter in message.lower()]) + + else: + print("Please enter a correct choice") + + return newMessage.capitalize() + +print(machine()) \ No newline at end of file