Skip to content

Commit 1377382

Browse files
committed
new programs
1 parent 5ae4d4b commit 1377382

9 files changed

+189
-27
lines changed

Basic/functions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def add(x, y):
2+
return x + y
3+
#Evaluates x + y and returns the INTEGER result back to the point where this function was called from.
4+
5+
def sub(a, b):
6+
return a - b
7+
8+
def mult(p, r):
9+
return p * r
10+
11+
def div(a, b):
12+
return a / b
13+
14+
def remainder(m,n):
15+
return m % n
16+
17+
print(" ** CALCULATOR ** ")
18+
print("Enter two numbers")
19+
a = input()
20+
b = input()
21+
22+
#Suppose user enters 3 and 5, Then a = "3", b = "5", "3" and "5" are strings
23+
#But we need a = 3, and not "3" and b = 5, and not "5", so that they become integers and then we can perform arithmetic operations.
24+
25+
a = int(a) # This line converts whatever is stored in a to INTEGER Type and assigns that value to a.
26+
b = int(b) # This line converts whatever is stored in b to INTEGER Type and assigns that value to b again.
27+
28+
#SO simply put, int function converts variables to INTEGER data type
29+
# And similiarly, there is an str() function which converts variables to STRING data type
30+
31+
print("\n\nResults!")
32+
33+
# Suppose I entered a=1, b=2 then this happens,
34+
35+
print(add(a,b)) #Addition, add(a,b) becomes --> 3, 3 gets printed on screen.
36+
print(sub(a,b)) #Subtraction, sub(a,b) --> -1
37+
print(div(a,b)) #Division, div(a,b) --> 0
38+
print(mult(a,b)) #Multiplication, mult(a,b) -> 2
39+
print(remainder(a,b)) #Remainder, remainder(a,b) -> 1

Data Structures/queue/hotpotato.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Queue:
2+
def __init__(self):
3+
self.queue = []
4+
def enqueue(self, item):
5+
self.queue.insert(0, item)
6+
def dequeue(self):
7+
return self.queue.pop()
8+
def size(self):
9+
return len(self.queue)
10+
def isEmpty(self):
11+
return self.queue == []
12+
13+
def hotPotato(people, iteration):
14+
length = len(people)
15+
q = Queue()
16+
17+
for x in people:
18+
q.enqueue(x)
19+
20+
while(q.size() > 1):
21+
for i in range(0, iteration):
22+
item = q.dequeue()
23+
q.enqueue(item)
24+
25+
q.dequeue()
26+
27+
return q.dequeue()
28+
29+
print hotPotato(["Hemang", "Rahul", "Totem", "Hero"], 7)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import random
2+
3+
class Queue:
4+
def __init__(self):
5+
self.queue = []
6+
def enqueue(self, item):
7+
self.queue.insert(0, item)
8+
def dequeue(self):
9+
return self.queue.pop()
10+
def size(self):
11+
return len(self.queue)
12+
def isEmpty(self):
13+
return self.queue == []
14+
15+
class Printer:
16+
def __init__(self, ppm):
17+
self.pagerate = ppm
18+
self.currentTask = None
19+
self.timeRemaining = 0
20+
21+
def tick(self):
22+
if self.currentTask != None:
23+
self.timeRemaining = self.timeRemaining - 1
24+
if self.timeRemaining <= 0:
25+
self.currentTask = None
26+
27+
def busy(self):
28+
return self.currentTask != None
29+
30+
def startNext(self, newtask):
31+
self.currentTask = newtask
32+
self.timeRemaining = newtask.getPages() * 60/self.pagerate
33+
34+
35+
class Task:
36+
def __init__(self, time):
37+
self.timestamp = time
38+
self.pages = random.randrange(1,21)
39+
40+
def getStamp(self):
41+
return self.timestamp
42+
43+
def getPages(self):
44+
return self.pages
45+
46+
def waitTime(self, currenttime):
47+
return currenttime - self.timestamp
48+
49+
50+
def simulation(numSeconds, pagesPerMinute):
51+
52+
labprinter = Printer(pagesPerMinute)
53+
printQueue = Queue()
54+
waitingtimes = []
55+
56+
for currentSecond in range(numSeconds):
57+
58+
if newPrintTask():
59+
task = Task(currentSecond)
60+
printQueue.enqueue(task)
61+
62+
if (not labprinter.busy()) and (not printQueue.isEmpty()):
63+
nexttask = printQueue.dequeue()
64+
waitingtimes.append(nexttask.waitTime(currentSecond))
65+
labprinter.startNext(nexttask)
66+
67+
labprinter.tick()
68+
69+
averageWait=sum(waitingtimes)/len(waitingtimes)
70+
print("Average Wait %6.2f secs %3d tasks remaining."%(averageWait,printQueue.size()))
71+
72+
def newPrintTask():
73+
num = random.randrange(1,181)
74+
if num == 180:
75+
return True
76+
else:
77+
return False
78+
79+
for i in range(10):
80+
simulation(3600,5)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Stack:
2+
3+
def __init__(self):
4+
self.stack = []
5+
def push(self, item):
6+
self.stack.append(item)
7+
def pop(self):
8+
return self.stack.pop()
9+
def isEmpty(self):
10+
return self.stack == []
11+
def size(self):
12+
return len(self.stack)
13+
def __str__(self):
14+
stack_str = ""
15+
for x in self.stack:
16+
stack_str += x
17+
return stack_str
18+
19+
20+
list1 = Stack()
21+
list2 = Stack()
22+
def stringToStack(string):
23+
for x in string:
24+
list1.push(x)
25+
26+
def makeListTwo(list):
27+
length = list.size()/2
28+
while(length > 0):
29+
list2.push(list1.pop())
30+
length = length - 1
31+
def compareLists(list1, list2):
32+
print(list1)
33+
print(list2)
34+
return
35+
36+
def doTask():
37+
stringToStack("abba")
38+
makeListTwo(list1)
39+
print(compareLists(list1, list2))
40+
41+
doTask()

basics/24HourClock.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

basics/daysOfWeek.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

basics/degreeAndFahreinheit.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

basics/simpleInterest.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)