Skip to content

Commit

Permalink
more programs
Browse files Browse the repository at this point in the history
  • Loading branch information
arbitrarymahi committed Feb 18, 2018
1 parent db5ea60 commit c1b82ae
Show file tree
Hide file tree
Showing 20 changed files with 543 additions and 15 deletions.
16 changes: 16 additions & 0 deletions List_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Node:
def __init__(self,init_data):
self.data = init_data
self.next = None

def get_data(self):
return self.data

def get_next(self):
return self.next

def set_data(self, new_data):
self.data = new_data

def set_next(self, new_next):
self.next = new_next
Binary file added List_node.pyc
Binary file not shown.
101 changes: 101 additions & 0 deletions List_ordered.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from List_node import Node

class OrderedList:
def __init__(self):
self.head = None

def is_empty(self):
return self.head == None

def size(self):
count = 0
current = self.head
while current != None:
count += 1
current = current.get_next()

return count

def __str__(self):
current = self.head
list1 = []
while current != None:
list1 = list1+[current.get_data()]
current = current.get_next()

return str(list1)

def remove(self, item):
current = self.head
prev = None
found = False
print self.head

while current != None and not found:
print current
print prev
if item == current.get_data():
found = True
else:
prev = current
current = current.get_next()

if prev == None:
self.head = current.get_next()
else:
prev.set_next(current.get_next())

def search(self, item):
found = False
current = self.head
stop = False

while current != None and not found and not stop:
if item < current.get_data():
stop = True
elif item == current.get_data():
found = True
else:
current = current.get_next()

return found

def add(self, item):
current = self.head
prev = None
stop = False

while current != None and not stop:
if item < current.get_data():
stop = True
else:
prev = current
current = current.get_next()
temp = Node(item)
if prev == None:
temp.set_next(self.head)
self.head = temp
else:
temp.next = prev.get_next()
prev.set_next(temp)

list1 = OrderedList()

print list1.is_empty()
list1.add(31)
list1.add(77)
list1.add(17)
list1.add(93)
list1.add(99)
print list1.size()
print(list1)
list1.remove(99)
#print list1.size()
#print list1.remove(54)
print list1.size()
print(list1)
print list1.search(77)
print list1.search(7)



100 changes: 100 additions & 0 deletions List_unorderedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from List_node import Node

class UnorderedList:
def __init__(self):
self.head = None

def __str__(self):
current = self.head
list1 = []
while current != None:
list1 = list1+[current.get_data()]
current = current.get_next()

return str(list1)

def is_empty(self):
return self.head == None

def add(self, item):
temp = Node(item)
temp.next = self.head
self.head = temp

def size(self):
count = 0
current = self.head
while current != None:
count += 1
current = current.get_next()

return count

def search(self, item):
loc = None
count = 0
found = False
current = self.head

while current != None and not found:
if item == current.get_data():
found = True
loc = count
current = current.get_next()
count += 1

if found:
return str("item found at position %r"%(loc))
else:
return "item not found"

def remove(self, item):
prev = None
found = False
current = self.head

while current != None and not found:
if item == current.get_data():
found = True
else:
prev = current
current = current.get_next()

if prev == None:
self.head = current.get_next()
else:
prev.set_next(current.get_next())

def append(self, item):
prev = None
found = False
current = self.head

while current != None and not found:

prev = current
current = current.get_next()

temp = Node(item)
prev.set_next(temp)


list1 = UnorderedList()

print list1.is_empty()
list1.add(31)
list1.add(77)
list1.add(17)
print list1.size()
list1.add(93)
list1.add(54)
print list1.size()
#print list1.remove(31)
print list1.size()
#print list1.remove(54)
print list1.size()
list1.append(73)
print list1.size()
list1.append(799)
print list1.size()
print(list1)
3 changes: 3 additions & 0 deletions Queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class Queue:
def __init__(self):
self.items = []

def __str__(self):
return str(self.items)

def is_empty(self):
return self.items == []

Expand Down
Binary file modified Queue.pyc
Binary file not shown.
101 changes: 101 additions & 0 deletions Queue_radixSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from Queue import Queue

def radixSort(x):
main_bin = Queue()
for num in x:
main_bin.enqueue(int(num))

print(main_bin)

#zero,one,two,three,four,five,six,seven,eight,nine = Queue()
dicts = {1:'one', 2:'two', 3:'three',
4:'four', 5:'five', 6:'six',
7:'seven', 8:'eight', 9:'nine',
0:'zero'}
"""for value in dicts.values():
print value
value = Queue()
print value
"""
zero = Queue()
one = Queue()
two = Queue()
three = Queue()
four = Queue()
five = Queue()
six = Queue()
seven = Queue()
eight = Queue()
nine = Queue()

def main_enqueue():
while not zero.is_empty():
main_bin.enqueue(zero.dequeue())
while not one.is_empty():
main_bin.enqueue(one.dequeue())
while not two.is_empty():
main_bin.enqueue(two.dequeue())
while not three.is_empty():
main_bin.enqueue(three.dequeue())
while not four.is_empty():
main_bin.enqueue(four.dequeue())
while not five.is_empty():
main_bin.enqueue(five.dequeue())
while not six.is_empty():
main_bin.enqueue(six.dequeue())
while not seven.is_empty():
main_bin.enqueue(seven.dequeue())
while not eight.is_empty():
main_bin.enqueue(eight.dequeue())
while not nine.is_empty():
main_bin.enqueue(nine.dequeue())

def queue_sort(x,y):
while not main_bin.is_empty():
temp = main_bin.dequeue()
if (temp%x)/y == 0:
zero.enqueue(temp)
elif (temp%x)/y == 1:
one.enqueue(temp)
elif (temp%x)/y == 2:
two.enqueue(temp)
elif (temp%x)/y == 3:
three.enqueue(temp)
elif (temp%x)/y == 4:
four.enqueue(temp)
elif (temp%x)/y == 5:
five.enqueue(temp)
elif (temp%x)/y == 6:
six.enqueue(temp)
elif (temp%x)/y == 7:
seven.enqueue(temp)
elif (temp%x)/y == 8:
eight.enqueue(temp)
elif (temp%x)/y == 9:
nine.enqueue(temp)

queue_sort(10,1)
main_enqueue()

print(main_bin)

queue_sort(100,10)
main_enqueue()

print(main_bin)

queue_sort(1000,100)
main_enqueue()



print(main_bin)


main_enqueue()

print(main_bin)



radixSort([12,43,464,234,65,23,25,33,43,333])
31 changes: 31 additions & 0 deletions Recursion_integerToBaseString.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def to_str(no, base):
char_str = '0123456789ABCDEF'
if no<base:
return char_str[no]
else:
return to_str(no//base, base) + char_str[no%base]

print to_str(1453,16)
print to_str(769,10)
print to_str(11,2)

from Stack import Stack

def to_str(no, base):
rec_stk = Stack()
char_str = '0123456789ABCDEF'
while no > 0:
if no < base:
rec_stk.push(char_str[no])
else:
rec_stk.push(char_str[no%base])

no = no // base

string = ''
while not rec_stk.is_empty():
string = string + rec_stk.pop()
return string

print to_str(1453,16)
print to_str(11,2)
9 changes: 9 additions & 0 deletions Recursion_listSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def list_sum(ls):
lsum = 0
if len(ls) == 1:
return ls[0]
else:
lsum = ls[0] + list_sum(ls[1:])
return lsum

print list_sum([1,2,3,4,5,6,7,8,9])
13 changes: 13 additions & 0 deletions Recursion_selfCheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def str_rev(s):
if len(s) == 1:
return s
else:
return str_rev(s[1:]) + s[0]
#string reverse
print str_rev('avs')

# palindrome check
x = str(raw_input("enter to check palindrome "))
print x == str_rev(x)
x = raw_input("enter to check palindrome ")
print x == str_rev(x)
10 changes: 10 additions & 0 deletions Recursion_towerofhanoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def move_tower(height, from_pole, to_pole, with_pole):
if height >= 1:
move_tower(height-1, from_pole, with_pole, to_pole)
move_disk(from_pole, to_pole)
move_tower(height-1, with_pole, to_pole, from_pole)

def move_disk(fp,tp):
print("moving from ",fp, " to ", tp)

move_tower(3, "A","B","C")
Loading

0 comments on commit c1b82ae

Please sign in to comment.