Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions python/BuySellStock.py
Original file line number Diff line number Diff line change
@@ -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)
68 changes: 68 additions & 0 deletions python/CircularQueue.py
Original file line number Diff line number Diff line change
@@ -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()
65 changes: 65 additions & 0 deletions python/Clock.py
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 10 additions & 0 deletions python/Convert a list of Tuples into Dictionary.py
Original file line number Diff line number Diff line change
@@ -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))
30 changes: 30 additions & 0 deletions python/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -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]),
140 changes: 140 additions & 0 deletions python/calculator_gui.py
Original file line number Diff line number Diff line change
@@ -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()
30 changes: 30 additions & 0 deletions python/calendar.py
Original file line number Diff line number Diff line change
@@ -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.")
Loading