Skip to content

Commit c1b82ae

Browse files
committed
more programs
1 parent db5ea60 commit c1b82ae

20 files changed

+543
-15
lines changed

List_node.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Node:
2+
def __init__(self,init_data):
3+
self.data = init_data
4+
self.next = None
5+
6+
def get_data(self):
7+
return self.data
8+
9+
def get_next(self):
10+
return self.next
11+
12+
def set_data(self, new_data):
13+
self.data = new_data
14+
15+
def set_next(self, new_next):
16+
self.next = new_next

List_node.pyc

1.15 KB
Binary file not shown.

List_ordered.py

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from List_node import Node
2+
3+
class OrderedList:
4+
def __init__(self):
5+
self.head = None
6+
7+
def is_empty(self):
8+
return self.head == None
9+
10+
def size(self):
11+
count = 0
12+
current = self.head
13+
while current != None:
14+
count += 1
15+
current = current.get_next()
16+
17+
return count
18+
19+
def __str__(self):
20+
current = self.head
21+
list1 = []
22+
while current != None:
23+
list1 = list1+[current.get_data()]
24+
current = current.get_next()
25+
26+
return str(list1)
27+
28+
def remove(self, item):
29+
current = self.head
30+
prev = None
31+
found = False
32+
print self.head
33+
34+
while current != None and not found:
35+
print current
36+
print prev
37+
if item == current.get_data():
38+
found = True
39+
else:
40+
prev = current
41+
current = current.get_next()
42+
43+
if prev == None:
44+
self.head = current.get_next()
45+
else:
46+
prev.set_next(current.get_next())
47+
48+
def search(self, item):
49+
found = False
50+
current = self.head
51+
stop = False
52+
53+
while current != None and not found and not stop:
54+
if item < current.get_data():
55+
stop = True
56+
elif item == current.get_data():
57+
found = True
58+
else:
59+
current = current.get_next()
60+
61+
return found
62+
63+
def add(self, item):
64+
current = self.head
65+
prev = None
66+
stop = False
67+
68+
while current != None and not stop:
69+
if item < current.get_data():
70+
stop = True
71+
else:
72+
prev = current
73+
current = current.get_next()
74+
temp = Node(item)
75+
if prev == None:
76+
temp.set_next(self.head)
77+
self.head = temp
78+
else:
79+
temp.next = prev.get_next()
80+
prev.set_next(temp)
81+
82+
list1 = OrderedList()
83+
84+
print list1.is_empty()
85+
list1.add(31)
86+
list1.add(77)
87+
list1.add(17)
88+
list1.add(93)
89+
list1.add(99)
90+
print list1.size()
91+
print(list1)
92+
list1.remove(99)
93+
#print list1.size()
94+
#print list1.remove(54)
95+
print list1.size()
96+
print(list1)
97+
print list1.search(77)
98+
print list1.search(7)
99+
100+
101+

List_unorderedList.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from List_node import Node
2+
3+
class UnorderedList:
4+
def __init__(self):
5+
self.head = None
6+
7+
def __str__(self):
8+
current = self.head
9+
list1 = []
10+
while current != None:
11+
list1 = list1+[current.get_data()]
12+
current = current.get_next()
13+
14+
return str(list1)
15+
16+
def is_empty(self):
17+
return self.head == None
18+
19+
def add(self, item):
20+
temp = Node(item)
21+
temp.next = self.head
22+
self.head = temp
23+
24+
def size(self):
25+
count = 0
26+
current = self.head
27+
while current != None:
28+
count += 1
29+
current = current.get_next()
30+
31+
return count
32+
33+
def search(self, item):
34+
loc = None
35+
count = 0
36+
found = False
37+
current = self.head
38+
39+
while current != None and not found:
40+
if item == current.get_data():
41+
found = True
42+
loc = count
43+
current = current.get_next()
44+
count += 1
45+
46+
if found:
47+
return str("item found at position %r"%(loc))
48+
else:
49+
return "item not found"
50+
51+
def remove(self, item):
52+
prev = None
53+
found = False
54+
current = self.head
55+
56+
while current != None and not found:
57+
if item == current.get_data():
58+
found = True
59+
else:
60+
prev = current
61+
current = current.get_next()
62+
63+
if prev == None:
64+
self.head = current.get_next()
65+
else:
66+
prev.set_next(current.get_next())
67+
68+
def append(self, item):
69+
prev = None
70+
found = False
71+
current = self.head
72+
73+
while current != None and not found:
74+
75+
prev = current
76+
current = current.get_next()
77+
78+
temp = Node(item)
79+
prev.set_next(temp)
80+
81+
82+
list1 = UnorderedList()
83+
84+
print list1.is_empty()
85+
list1.add(31)
86+
list1.add(77)
87+
list1.add(17)
88+
print list1.size()
89+
list1.add(93)
90+
list1.add(54)
91+
print list1.size()
92+
#print list1.remove(31)
93+
print list1.size()
94+
#print list1.remove(54)
95+
print list1.size()
96+
list1.append(73)
97+
print list1.size()
98+
list1.append(799)
99+
print list1.size()
100+
print(list1)

Queue.py

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ class Queue:
22
def __init__(self):
33
self.items = []
44

5+
def __str__(self):
6+
return str(self.items)
7+
58
def is_empty(self):
69
return self.items == []
710

Queue.pyc

162 Bytes
Binary file not shown.

Queue_radixSearch.py

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from Queue import Queue
2+
3+
def radixSort(x):
4+
main_bin = Queue()
5+
for num in x:
6+
main_bin.enqueue(int(num))
7+
8+
print(main_bin)
9+
10+
#zero,one,two,three,four,five,six,seven,eight,nine = Queue()
11+
dicts = {1:'one', 2:'two', 3:'three',
12+
4:'four', 5:'five', 6:'six',
13+
7:'seven', 8:'eight', 9:'nine',
14+
0:'zero'}
15+
"""for value in dicts.values():
16+
print value
17+
value = Queue()
18+
print value
19+
"""
20+
zero = Queue()
21+
one = Queue()
22+
two = Queue()
23+
three = Queue()
24+
four = Queue()
25+
five = Queue()
26+
six = Queue()
27+
seven = Queue()
28+
eight = Queue()
29+
nine = Queue()
30+
31+
def main_enqueue():
32+
while not zero.is_empty():
33+
main_bin.enqueue(zero.dequeue())
34+
while not one.is_empty():
35+
main_bin.enqueue(one.dequeue())
36+
while not two.is_empty():
37+
main_bin.enqueue(two.dequeue())
38+
while not three.is_empty():
39+
main_bin.enqueue(three.dequeue())
40+
while not four.is_empty():
41+
main_bin.enqueue(four.dequeue())
42+
while not five.is_empty():
43+
main_bin.enqueue(five.dequeue())
44+
while not six.is_empty():
45+
main_bin.enqueue(six.dequeue())
46+
while not seven.is_empty():
47+
main_bin.enqueue(seven.dequeue())
48+
while not eight.is_empty():
49+
main_bin.enqueue(eight.dequeue())
50+
while not nine.is_empty():
51+
main_bin.enqueue(nine.dequeue())
52+
53+
def queue_sort(x,y):
54+
while not main_bin.is_empty():
55+
temp = main_bin.dequeue()
56+
if (temp%x)/y == 0:
57+
zero.enqueue(temp)
58+
elif (temp%x)/y == 1:
59+
one.enqueue(temp)
60+
elif (temp%x)/y == 2:
61+
two.enqueue(temp)
62+
elif (temp%x)/y == 3:
63+
three.enqueue(temp)
64+
elif (temp%x)/y == 4:
65+
four.enqueue(temp)
66+
elif (temp%x)/y == 5:
67+
five.enqueue(temp)
68+
elif (temp%x)/y == 6:
69+
six.enqueue(temp)
70+
elif (temp%x)/y == 7:
71+
seven.enqueue(temp)
72+
elif (temp%x)/y == 8:
73+
eight.enqueue(temp)
74+
elif (temp%x)/y == 9:
75+
nine.enqueue(temp)
76+
77+
queue_sort(10,1)
78+
main_enqueue()
79+
80+
print(main_bin)
81+
82+
queue_sort(100,10)
83+
main_enqueue()
84+
85+
print(main_bin)
86+
87+
queue_sort(1000,100)
88+
main_enqueue()
89+
90+
91+
92+
print(main_bin)
93+
94+
95+
main_enqueue()
96+
97+
print(main_bin)
98+
99+
100+
101+
radixSort([12,43,464,234,65,23,25,33,43,333])

Recursion_integerToBaseString.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def to_str(no, base):
2+
char_str = '0123456789ABCDEF'
3+
if no<base:
4+
return char_str[no]
5+
else:
6+
return to_str(no//base, base) + char_str[no%base]
7+
8+
print to_str(1453,16)
9+
print to_str(769,10)
10+
print to_str(11,2)
11+
12+
from Stack import Stack
13+
14+
def to_str(no, base):
15+
rec_stk = Stack()
16+
char_str = '0123456789ABCDEF'
17+
while no > 0:
18+
if no < base:
19+
rec_stk.push(char_str[no])
20+
else:
21+
rec_stk.push(char_str[no%base])
22+
23+
no = no // base
24+
25+
string = ''
26+
while not rec_stk.is_empty():
27+
string = string + rec_stk.pop()
28+
return string
29+
30+
print to_str(1453,16)
31+
print to_str(11,2)

Recursion_listSum.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def list_sum(ls):
2+
lsum = 0
3+
if len(ls) == 1:
4+
return ls[0]
5+
else:
6+
lsum = ls[0] + list_sum(ls[1:])
7+
return lsum
8+
9+
print list_sum([1,2,3,4,5,6,7,8,9])

Recursion_selfCheck.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def str_rev(s):
2+
if len(s) == 1:
3+
return s
4+
else:
5+
return str_rev(s[1:]) + s[0]
6+
#string reverse
7+
print str_rev('avs')
8+
9+
# palindrome check
10+
x = str(raw_input("enter to check palindrome "))
11+
print x == str_rev(x)
12+
x = raw_input("enter to check palindrome ")
13+
print x == str_rev(x)

Recursion_towerofhanoi.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def move_tower(height, from_pole, to_pole, with_pole):
2+
if height >= 1:
3+
move_tower(height-1, from_pole, with_pole, to_pole)
4+
move_disk(from_pole, to_pole)
5+
move_tower(height-1, with_pole, to_pole, from_pole)
6+
7+
def move_disk(fp,tp):
8+
print("moving from ",fp, " to ", tp)
9+
10+
move_tower(3, "A","B","C")

0 commit comments

Comments
 (0)