diff --git a/PythonTesting/ch5_basic.py b/PythonTesting/ch5_basic.py index e7b7e5f..5866d3f 100644 --- a/PythonTesting/ch5_basic.py +++ b/PythonTesting/ch5_basic.py @@ -50,6 +50,7 @@ def test_with_when(self): self.assertAlmostEqual(controller.previous_error, -11.0) self.assertAlmostEqual(controller.integrated_error, 0) +class time class TestCalculateResponse(unittest.TestCase): def ignore_test_without_when(self): diff --git a/mongodb_1.py b/mongodb_1.py new file mode 100644 index 0000000..9223665 --- /dev/null +++ b/mongodb_1.py @@ -0,0 +1,13 @@ +import bottle +import pymongo + +@bottle.route('/') +def index(): + connection = pymongo.MongoClient('localhost', 27017) + db = connection.test + name = db.names + item = name.find_one() + + return 'Hello {}!'.format(item['name']) + +bottle.run(host='localhost', port=8080) diff --git a/various/pythonTree.py b/various/pythonTree.py index e6affe5..5c437c0 100644 --- a/various/pythonTree.py +++ b/various/pythonTree.py @@ -1,47 +1,65 @@ - import random -class Node: +class Node: data = None - left = None + left = None right = None - def __init__(self, initialValue): - self.data = initialValue + def __init__(self, initial_value): + self.data = initial_value - def addNode(self, newNode): + def add_node(self, new_node): if not self: - self = newNode - elif newNode.data >= self.data: + self = new_node + elif new_node.data >= self.data: if not self.right: - self.right = newNode + self.right = new_node else: - self.right.addNode(newNode) + self.right.add_node(new_node) else: if not self.left: - self.left = newNode + self.left = new_node else: - self.left.addNode(newNode) + self.left.add_node(new_node) - def printInorder(self): + def print_in_order(self): if self.left: - self.left.printInorder() + self.left.print_in_order() if self: print str(self.data), if self.right: - self.right.printInorder() + self.right.print_in_order() + + def check_in_order(self, mini, maxi): + if self.left: + self.left.check_in_order(mini, maxi) + if self: + print str("data= {} min={} max={}=> {}/{}".format(self.data)) + if self.right: + self.right.check_in_order(mini, maxi) + if __name__ == "__main__": root = Node(0) - for i in xrange(1,100): - value = random.randint(1,1000) - root.addNode(Node(value)) + for i in xrange(1, 10): + value = random.randint(1, 1000) + root.add_node(Node(value)) print value, + root.print_in_order() + root.check_in_order(0, 0) + print "\n---" - root.printInorder() + broken = Node(3) + broken.left = Node(2) + broken.left.left = Node(1) + broken.left.right = Node(4) + broken.right = Node(5) + broken.right.left = Node(6) + broken.print_in_order() + broken.check_in_order(3, 3) diff --git a/various/test_new.py b/various/test_new.py new file mode 100644 index 0000000..bcba6e3 --- /dev/null +++ b/various/test_new.py @@ -0,0 +1,58 @@ +def permutate(astring): + permute("", astring) + +def permute(prefix, rest_of_string): + if len(rest_of_string)==0: + print prefix + else: + for idx in range(0, len(rest_of_string)): + permute(prefix + rest_of_string[idx], rest_of_string[:idx] + rest_of_string[idx+1:]) + +def smallest_common(arr1, arr2): + arr2.sort() + found = -1 + for el in arr2: + if el in arr1: + found = el + break + return found + +def reversel(sir): + lo =0 + hi = len(sir)-1 + sir = [c for c in sir] + while lo s: + binary_search(array[0:len(array)/2], s) + else: + binary_search(array[len(array)/2+1:], s) + if len(array) == 1 and array[0] == s: + print "bingo" + return True + else: + return False + + + +if __name__ == "__main__": + permutate("ABCD") + arr1=[9,8,7,6,5,4] + arr2=[54,22,19,6,2,0,33] + print smallest_common(arr1, arr2) + print "" + # _123 / 1 23 + print reversel("ABCDEF") + + print binary_search([1,2,3,4,5,6,7,8,9], 4) \ No newline at end of file diff --git a/various/tests.py b/various/tests.py new file mode 100644 index 0000000..b87e291 --- /dev/null +++ b/various/tests.py @@ -0,0 +1,115 @@ +# exercise 1 +def check_string(input_string): + tmp_array = [x for x in input_string] + tmp_array.sort() + my_str = "" + for el in tmp_array: + my_str += el + return my_str +def anagram_finder(input_string, compare_string): + start_index = 0 + len_base_str = len(compare_string) + while start_index <= len(input_string) - len_base_str: + if check_string(input_string[start_index:start_index + len_base_str]) == compare_string: + print start_index, + start_index += 1 + print "\n" + +#exercise 2 +def display_matrix(matrix): + for i in matrix: + for j in i: + print j, + print +def check_neighbors(i, j, m, n, matrix, chain): + # print i, j, m, n + if i < 0 or j < 0 or i >= m or j >= n or matrix[i][j] == 0: + return + + if matrix[i][j] == 1: + matrix[i][j] = 0 + check_neighbors(i-1, j, m, n, matrix, chain) + check_neighbors(i, j-1, m, n, matrix, chain) + check_neighbors(i-1, j-1, m, n, matrix, chain) + check_neighbors(i+1, j-1, m, n, matrix, chain) + + check_neighbors(i+1, j, m, n, matrix, chain) + check_neighbors(i, j+1, m, n, matrix, chain) + check_neighbors(i+1, j+1, m, n, matrix, chain) + check_neighbors(i-1, j+1, m, n, matrix, chain) + chain.append((i, j)) + return +def build_paths(i, j, m, n, matrix): + chain = [] + if matrix[i][j] == 1: + # matrix[i][j] = 0 + check_neighbors(i, j, m, n, matrix, chain) + if len(chain): + chain.reverse() + print chain +def check_chains(matrix, m, n): + display_matrix(matrix) + print + + for i in range(0, len(matrix)): + for j in range(0, len(matrix[i])): + build_paths(i, j, m, n, matrix) + +#exercise 3 +def number_finder(array_in): + #1. Given an array of integers. Segregate all the non-zero numbers at the beginning. + # Print the number of non-zero integers and the minimum number of swaps required for these operations. + #Eg. : I/p : 1, 0, 0, -6, 2, 0 + #o/p : Number of non-zero integers : 3 + #Minimum number of swaps : 2 + zero_elem = [e for e in array_in if e == 0] + no_zeros = len(zero_elem) + full_length = len(array_in) - no_zeros + # initial phase + for el in array_in[::-1]: + if el == 0: + no_zeros -= 1 + else: + break + # if zeros in last + updated_zeros = len([e for e in array_in[::-1][:no_zeros] if e == 0]) + print "Initial - Number of non-zero {} minimum swaps {}".format(full_length, no_zeros - updated_zeros) + + +def swap(i, j, a_array): + tmp = a_array[i] + a_array[i] = a_array[j] + a_array[j] = tmp + +#exercise 4 +def all_permutations(a_string): + a_array = [e for e in a_string[:]] + permute("", a_string) + +def permute(pref, stri): + if not len(stri): + print pref, + else: + for i in range(0, len(stri)): + permute(pref + stri[i], stri[0:i] + stri[i+1:]) + +def fibo(n): + if n == 2 or n==1: + return 1 + else: + return fibo(n-1) + fibo(n-2) + + +if __name__ == "__main__": + # anagram_finder("BACDGABCDA", "ABCD") + + #matrix = [[1,1,0,0,0], [0,1,0,0,1], [1,0,0,1,1], [0,0,0,0,0], [1,0,1,0,1]] + #m=5 + #n=5 + #check_chains(matrix, m, n) + + # number_finder([1, 0, 0, -6, 2, 0, 6, 7]) + + all_permutations('123') + + pass