Graph in Python can be created with:
Monday, April 9, 2018
Sunday, April 8, 2018
Merge sort and quick sort in python
Merge Sort:
def merge_sort(sor): mid = len(sor)//2
if(len(sor)<=1):
return sor left =merge_sort(sor[0:mid]) right = merge_sort(sor[mid:]) i=j=0
tar =0
while(i<len(left) and j<len(right)): if(left[i]<right[j]): sor[tar]=left[i] i = i+1
else: sor[tar] =right[j] j=j+1
tar =tar+1
while(i<len(left)): sor[tar] =left[i] i =i +1
tar=tar+1
while(j<len(right)): sor[tar]= right[j] j=j+1
tar =tar+1 return sor if __name__ =='__main__': arr = [150,4,9,26,1,5,65,2,12,1,1] final =merge_sort(arr) print (final)
Quick Sort:
def partition(arr,l,h): # Note for finding the pivot element and placing the pivot element in middle# start from initial index and then increase one pointer j to teh last greatest element# if small element is found swap small element with bigger that is pointed by the j pointer#pivot = arr[h] j=l for i in range(l,h): if(arr[i]<pivot): arr[i],arr[j]=arr[j],arr[i] j=j+1 arr[j],arr[h]=arr[h],arr[j] return j def quick_sort(arr,l,h): if(l<h): pivot = partition(arr,l,h) quick_sort(arr,l,pivot-1) quick_sort(arr,pivot+1,h) if __name__ =='__main__': arr = [150,4,9,26,8,100,80,12] n= len(arr) quick_sort(arr,0,n-1) print (arr)
Binary tree creation and inorder,preorder traversal both iterative and recurrence in python
class node: def __init__(self,key): self.data=key self.left= None
self.right =None def insert(root,nd): if(nd.data <= root.data): if(root.left is None): root.left = nd else: insert(root.left,nd) else: if(root.right is None): root.right = nd else: insert(root.right,nd) def bin_search(root,key): if(root is None or root.data == key): return root else: if(key<root.data): return bin_search(root.left,key) else: return bin_search(root.right,key) def tree_inorder_traverse(node): if(node == None): return tree_inorder_traverse(node.left) print(node.data) tree_inorder_traverse(node.right) def inorder_iterative(node): stack = [] current = node while(1): if(current is not None): stack.append(current) current = current.left else: if(len(stack)>0): current = stack.pop() print(current.data) current = current.right else: break def preorder_traversal(node): if(node == None): return print(node.data) preorder_traversal(node.left) preorder_traversal(node.right) def preorder_iterative(root): stack_pre=[] current = root while(1): if(current is not None): print(current.data) stack_pre.append(current) current =current.left else: if(len(stack_pre)>0): current = stack_pre.pop() current = current.right else: break def preorder_iterative2(root): stack_pre=[] current = root stack_pre.append(current) while(len(stack_pre)>0): current = stack_pre.pop() print (current.data) if(current.right is not None): stack_pre.append(current.right) if(current.left is not None): stack_pre.append(current.left) if __name__ == "__main__": root = node(50) insert(root,node(20)) insert(root,node(70)) insert(root,node(10)) insert(root,node(30)) insert(root,node(60)) preorder_traversal(root) value = bin_search(root,20) print (value.data) print ("iterative") preorder_iterative(root)
Thursday, February 15, 2018
Compare and Swap(CAS) implementation for counter shared data in C++
implementation in simple increase counter:
#include <iostream>
#include <thread>
#include <unistd.h>
#include <atomic>
std::atomic<int> count(0); std::mutex n_mutux; void increase_counter() { int old_value = count.load(); ; while (!count.compare_exchange_weak(old_value, old_value +1)) { } }
int main() { unsigned int n = std::thread::hardware_concurrency(); std::cout << n << " concurrent threads are supported.\n"; int thread_num =10; std::thread t[thread_num]; for(int i=0;i<thread_num;i++) { t[i]=std::thread((increase_counter)); } for(int i=0;i<thread_num;i++) { t[i].join(); } std::cout<<count; }
implementation with vector and structure:
//// Created by Laxmi Kadariya on 2/14/18.//#include <iostream>#include <vector>#include <thread>#include <atomic>std::atomic<int> count(0); struct Counter { int value; Counter() : value(0){} void increment(){ int old_value = count.load(); ; while (!count.compare_exchange_weak(old_value, old_value +1)) { } // ++value; } }; int main(){ Counter counter; std::vector<std::thread> threads; for(int i = 0; i < 10; ++i){ threads.push_back(std::thread([&counter](){ for(int i = 0; i < 1000; ++i){ counter.increment(); } })); } for(auto& thread : threads){ thread.join(); } std::cout << count << std::endl; // std::cout << counter.value << std::endl; return 0; }
Wednesday, February 14, 2018
Reading chunk of data from a file calling a function using C++
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
using namespace std; bool generate_chunks(ifstream &fp,int chunk_size,string &buffer) { vector<char>data(chunk_size); fp.read(&data[0],chunk_size); std::streamsize datasize = fp.gcount(); if(datasize == 0) { cout<<"here"; return false; } buffer =string(data.begin(),data.end()); return true; } int main() { std::cout << "Hello, World!" << std::endl; ifstream myfile; bool test; string data; myfile.open("../test.dat",ifstream::binary); if(myfile.is_open()) { if(generate_chunks(myfile, 1024, data)) { cout<<data; } } myfile.close(); return 0; }
Tuesday, February 13, 2018
reading of chunk of data from the binary file and unpacking the data using struct construct in python
import struct class parser(): def __init__(self): self.data = "" self.offset = 0 def add_data(self,data): self.data = self.data[self.offset:] # print (self.data)
self.data += data self.offset = 0 def parse_header(self): l,n = struct.unpack('>hh',self.data[self.offset: self.offset+4]) if len(self.data) - self.offset >l:
status = True
else: status = False return l,n,status def parse_QuoteORTrade_data(self): val,type = struct.unpack('>hs',self.data[self.offset:self.offset+3]) # print(val,type)
self.update_offset(3) if(type == b'Q'): # sym,price,size = struct.unpack('>5shh',self.data[self.offset:self.offset+9]) self.update_offset(17) # print (sym,price,size)
self.update_offset(val-20) if(type == b'T'): sym,price,size = struct.unpack('>5shq',self.data[self.offset:self.offset+15]) self.update_offset(15) print (sym,price,size) self.update_offset(val-18) def update_offset(self,offset): self.offset = self.offset+ offset def ReadInChunk(fileobj,ChunkSize= 50): while True: data = fileobj.read(ChunkSize) if not data: break yield data def read_file(): f = open("input.dat","rb") return f if __name__ == "__main__" : offset_chunk = 0 obj=parser() fp = read_file() for chunk in ReadInChunk(fp): obj.add_data(chunk) # print (obj.data)
while True: try: length,num,status = obj.parse_header() except Exception,e: break
if(status == False): break obj.update_offset(4) for data in range(num): obj.parse_QuoteORTrade_data() fp.close()
Tuesday, February 6, 2018
Binary Tree creation with c++ using class and inorder traversal without recursion
//// Created by Laxmi Kadariya on 2/5/18. //This is the source code to create binary tree with class in c++//#include "iostream"#include <stack>using namespace std; struct node{ int item; struct node *left; struct node *right; }; class bst{ private: struct node *root; public: struct node* insert_node(node *root,int item) { if(root == NULL) { root = new node; root->item = item; root->left = NULL; root->right = NULL; } else{ if(root->item< item) { root->left = insert_node(root->left,item); } else if(root->item >item) { root->right = insert_node(root->right,item); } } return root; } /*Method to print inorder traversal of a BST*/
void inorder(node *root) { stack<node*> stack; node *curr=root; while(!stack.empty() || curr!=NULL) { /*If current node is not NULL push the node in stack*/
if(curr!=NULL) { stack.push(curr); curr=curr->left; } /*If current node is empty or NULL pop it from the stack */
else
{ curr=stack.top(); stack.pop(); cout<<curr->item<<" "; curr=curr->right; } } } }; int main() { bst tree; struct node *root = NULL; root = tree.insert_node(root,1); tree.insert_node(root,2); tree.insert_node(root,13); tree.insert_node(root,4); tree.inorder(root); }
Subscribe to:
Posts (Atom)