diff --git a/List_node.py b/List_node.py new file mode 100644 index 0000000..39bfc5c --- /dev/null +++ b/List_node.py @@ -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 diff --git a/List_node.pyc b/List_node.pyc new file mode 100644 index 0000000..99841ac Binary files /dev/null and b/List_node.pyc differ diff --git a/List_ordered.py b/List_ordered.py new file mode 100644 index 0000000..3262762 --- /dev/null +++ b/List_ordered.py @@ -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) + + + diff --git a/List_unorderedList.py b/List_unorderedList.py new file mode 100644 index 0000000..b8a9d7e --- /dev/null +++ b/List_unorderedList.py @@ -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) diff --git a/Queue.py b/Queue.py index b5075cb..5ac0b89 100644 --- a/Queue.py +++ b/Queue.py @@ -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 == [] diff --git a/Queue.pyc b/Queue.pyc index 1b9e860..cd81eea 100644 Binary files a/Queue.pyc and b/Queue.pyc differ diff --git a/Queue_radixSearch.py b/Queue_radixSearch.py new file mode 100644 index 0000000..b4b72c3 --- /dev/null +++ b/Queue_radixSearch.py @@ -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]) diff --git a/Recursion_integerToBaseString.py b/Recursion_integerToBaseString.py new file mode 100644 index 0000000..a138873 --- /dev/null +++ b/Recursion_integerToBaseString.py @@ -0,0 +1,31 @@ +def to_str(no, base): + char_str = '0123456789ABCDEF' + if 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) diff --git a/Recursion_listSum.py b/Recursion_listSum.py new file mode 100644 index 0000000..a6566b8 --- /dev/null +++ b/Recursion_listSum.py @@ -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]) diff --git a/Recursion_selfCheck.py b/Recursion_selfCheck.py new file mode 100644 index 0000000..3364a74 --- /dev/null +++ b/Recursion_selfCheck.py @@ -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) diff --git a/Recursion_towerofhanoi.py b/Recursion_towerofhanoi.py new file mode 100644 index 0000000..91a03fa --- /dev/null +++ b/Recursion_towerofhanoi.py @@ -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") diff --git a/Recursion_turtleDraw.py b/Recursion_turtleDraw.py new file mode 100644 index 0000000..c0ac4f8 --- /dev/null +++ b/Recursion_turtleDraw.py @@ -0,0 +1,15 @@ +import turtle +#import time +my_turtle = turtle.Turtle() +my_win = turtle.Screen() + +def draw_spiral(my_turtle, line_len): + if line_len > 0: + my_turtle.backward(line_len) + my_turtle.right(90) + #time.sleep(2) + draw_spiral(my_turtle, line_len - 3) + +draw_spiral(my_turtle, 300) +my_win.exitonclick() + diff --git a/Recursion_turtleDrawSierpinski.py b/Recursion_turtleDrawSierpinski.py new file mode 100644 index 0000000..a431442 --- /dev/null +++ b/Recursion_turtleDrawSierpinski.py @@ -0,0 +1,44 @@ +import turtle + +def draw_triangle(points, color, my_turtle): + my_turtle.fillcolor(color) + my_turtle.up() + my_turtle.goto(points[0][0],points[0][1]) + my_turtle.down() + my_turtle.begin_fill() + my_turtle.goto(points[1][0],points[1][1]) + my_turtle.goto(points[2][0],points[2][1]) + my_turtle.goto(points[0][0],points[0][1]) + my_turtle.end_fill() + +def mid_points(p1,p2): + return( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) /2 ) + +def sierpinski(points, degree, my_turtle): + color_map = ['blue', 'red', 'green', 'white', 'yellow', 'violet', 'orange'] + + draw_triangle(points, color_map[degree], my_turtle) + if degree > 0: + sierpinski([points[0], + mid_points(points[0],points[1]), + mid_points(points[0],points[2]), + degree - 1 ,my_turtle) + + sierpinski([points[1], + mid_points(points[1],points[0]), + mid_points(points[1],points[2]), + degree - 1 ,my_turtle) + + sierpinski([points[2], + mid_points(points[2],points[1]), + mid_points(points[0],points[2]), + degree - 1 ,my_turtle) + +def main(): + my_turtle = turtle.Turtle() + my_win = turtle.Screen() + my_points = [[-100, -50],[0, 50],[100, -50]] + sierpinski(my_points,3,my_turtle) + my_win.exitonclick() + +main() diff --git a/Recursion_turtleTreeDraw.py b/Recursion_turtleTreeDraw.py new file mode 100644 index 0000000..a5e5779 --- /dev/null +++ b/Recursion_turtleTreeDraw.py @@ -0,0 +1,34 @@ +import turtle +import random + +def tree(branch_len, t): + branch_len = random.randint(branch_len-4, branch_len+4) + if branch_len > 5: + degree1 = random.randint(5,65) + t.pensize(branch_len/5) + t.forward(branch_len) + t.right(degree1) + tree(branch_len-5, t) + t.left(2*degree1) + tree(branch_len - 5, t) + t.right(degree1) + if 5 < branch_len < 20: + t.color("green") + else: + t.color("brown") + t.pensize(branch_len/5) + t.backward(branch_len) + +def main(): + t = turtle.Turtle() + my_win = turtle.Screen() + t.left(90) + t.up() + t.backward(100) + t.down() + t.color("green") + tree(35,t) + my_win.exitonclick() + +main() + diff --git a/Recursion_turutleDrawSirpienski.py b/Recursion_turutleDrawSirpienski.py new file mode 100644 index 0000000..17d1b6d --- /dev/null +++ b/Recursion_turutleDrawSirpienski.py @@ -0,0 +1,47 @@ +import turtle + +def draw_triangle(points, color, my_turtle): + my_turtle.fillcolor(color) + my_turtle.speed(0) + my_turtle.up() + my_turtle.goto(points[0][0],points[0][1]) + my_turtle.down() + my_turtle.begin_fill() + my_turtle.goto(points[1][0],points[1][1]) + my_turtle.goto(points[2][0],points[2][1]) + my_turtle.goto(points[0][0],points[0][1]) + my_turtle.end_fill() + + +def mid_points(p1,p2): + return( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) /2 ) + +def sierpinski(points, degree, my_turtle): + color_map = ['blue', 'red', 'green', 'white', 'yellow', 'violet', 'orange'] + + draw_triangle(points, color_map[degree], my_turtle) + + if degree > 0: + sierpinski([points[0], \ + mid_points(points[0],points[1]), \ + mid_points(points[0],points[2])], \ + degree-1 ,my_turtle) + + sierpinski([points[1], \ + mid_points(points[1],points[0]), \ + mid_points(points[1],points[2])], \ + degree-1 ,my_turtle) + + sierpinski([points[2], \ + mid_points(points[2],points[1]), \ + mid_points(points[2],points[0])], \ + degree-1 ,my_turtle) + +def main(): + my_turtle = turtle.Turtle() + my_win = turtle.Screen() + my_points = [[-400, -350],[0, 350],[400, -350]] + sierpinski(my_points,6,my_turtle) + my_win.exitonclick() + +main() diff --git a/Stack.pyc b/Stack.pyc index 7509077..afa0d3b 100644 Binary files a/Stack.pyc and b/Stack.pyc differ diff --git a/Stack_decimalToBinary.py b/Stack_decimalToBinary.py index 0a9aec9..19d4940 100644 --- a/Stack_decimalToBinary.py +++ b/Stack_decimalToBinary.py @@ -33,8 +33,8 @@ def deciToBase(deci,base): return newString -print(deciToBase(25,8)) -print(deciToBase(256,16)) -print(deciToBase(16,16)) +print(deciToBase(17,2)) +print(deciToBase(45,2)) +print(deciToBase(96,2)) print(deciToBase(8,8)) print(deciToBase(2,2)) diff --git a/Stack_infixToPostfix.py b/Stack_infixToPostfix.py index 61af90a..ffecc7c 100644 --- a/Stack_infixToPostfix.py +++ b/Stack_infixToPostfix.py @@ -9,9 +9,11 @@ def type_ch(x): def infixToPostfix(infix_expr): postfix_str = [] -# token_list = infix_expr.split() #for input string with spaces-separated-characters - token_list =list(infix_expr) #for input string without spaces-separated-characters **all or some chars** - + token_list = infix_expr.split() #for input string with spaces-separated-characters +# token_list =list(infix_expr) #for input string without spaces-separated-characters **all or some chars** + #doesn't work with numbers over 9 since + #they have more than one character + #print(token_list) while ' ' in token_list: token_list.remove(' ') @@ -44,9 +46,9 @@ def infixToPostfix(infix_expr): return str(' '.join(postfix_str)) #return postfix with spaces # return str(''.join(postfix_str)) #return postfix without spaces - -"""print(infixToPostfix("(A+B)^X*(C+D)")) -print(infixToPostfix("5 * 3^(4 - 2)")) #string without spaces +#print(infixToPostfix("15 * 3 ^ ( 4 - 2 )")) +"""print(infixToPostfix("(A + B ) ^ X * ( C + D )")) +print(infixToPostfix("5 * 3 ^ ( 4 - 2 )")) #string without spaces print(infixToPostfix("A * B + C * D")) #string with spaces print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )")) print infixToPostfix("( A + B ) * ( C + D )") diff --git a/Stack_infixToPostfix.pyc b/Stack_infixToPostfix.pyc index e5981aa..abe830e 100644 Binary files a/Stack_infixToPostfix.pyc and b/Stack_infixToPostfix.pyc differ diff --git a/Stack_postfixEvaluation.py b/Stack_postfixEvaluation.py index b06db52..446ff13 100644 --- a/Stack_postfixEvaluation.py +++ b/Stack_postfixEvaluation.py @@ -26,11 +26,11 @@ def postfixEval(postfix_str): for token in postfix_list: if type_ch(token): - op_stack.push(token) + op_stack.push(int(token)) else: - op2 = int(op_stack.pop()) - op1 = int(op_stack.pop()) + op2 = op_stack.pop() + op1 = op_stack.pop() op_stack.push(evaluateFunc(token,op1,op2)) return op_stack.pop() @@ -41,10 +41,12 @@ def type_ch(x): return True except: return False - -#x = infixToPostfix("10 + 10 * 5 / ( 26 - 1 )") -x = infixToPostfix("5 * 3^(4 - 2)") +s = str(input("Enter your expression with spaces in between characters")) +#x = infixToPostfix("10 + 10 * 5 / ( 26 - 1 )") +#x = infixToPostfix("15 * 3 ^ ( 14 - 12 )") +#print x +x = infixToPostfix(s) print(postfixEval(x))