Posts

Existence: Absolute & Relative

Image
 Who am I? Why am I? Where am I? How did I happen exactly? So many unanswered questions. Continuing the premise from my previous post about life and death . I believe I am real. I am real because I perceive the world through my senses. I witness my physical existence. Taking a closer look at my existence, I feel I should revise my statement to I believe I physically exist . I am associated with a medium (my physical body) that enables me to perceive the physical universe. So probably I'm just a consciousness that is aware of this physical world as long as its attached to a functioning medium.  May be this feeling of awareness within me, till my consciousness is aware of this perceivable physical universe, can be referred to as the feeling of being alive.  The association of consciousness with a functioning body is a function of time and is commonly referred as life.   This association deteriorates with time due to limitations of physical body. Of course, it would ch...

Life and Death: An NP Hard Problem

Image
Finally, I thought of documenting my thoughts, perspective and my incomplete yet ongoing and probably incorrect or partially correct understanding on my beliefs in the concept of life and death, the two widely accepted states of living beings. My apologies to the state(s) that may be there but I missed to include them here due to my limited awareness. Let's start with the physical and tangible world we believe we exist in and cohabitate. I believe this physical world is real to me (we will come to real later) as I can perceive it through my accessible sense organs and my brain interprets the signals of following known categories: sight, sound, smell, taste and touch. Observable Universe These sense organs receive raw stimuli from the physical world and translate them into signals the nervous system can use. Nerves relay the signals to the brain, which interprets them as sight (vision), sound (hearing), smell (olfaction), taste (gustation), and touch (tactile perception). Detailed e...

Hyperlocal Startups Creating a Better Living

Image
Recently, I read about this article on yourstory: IIT alumni startup creating a better living experience and it made me go down the memory lane when I used to work in a small real estate based startup in Bengaluru, India. Startups are growing like mushrooms in India and most of them claim to be solving a real world problem. I am specifically intrigued, surprised and sometimes disappointed by some of these startups. These startups, specifically the hyperlocal ones, when claim to create a better living experience, a better lifestyle or quality of life makes me rethink about the life I'm living. Am I doing something wrong? Am I wasting my time in buying groceries or washing my clothes or cooking lunch? Further, I would like to emphasize that its not just the services in the hyperlocal sector that makes me rethink. Its also what they claim to achieve by doing so is astonishing. This umbrella euphoria of every startup claiming to create a better living experience is making me...

Facebook Messenger Chat Bot Tutorial - AI powered by wit.ai

Image
Facebook has provided interesting way to allow you to build a chat bot for your FB page. I will guide you step by step to build this chat bot. This chat bot essentially is something that will receive the fb messages send to your page, process them and send back response. To start with, we need an app that would receive fb messages. To know how to step up a free app: click here Once we have setup your app and its up and running, we need to goto FB Developers  . Signin using the same FB account that is an admin to the required FB page we are setting up our BOT. After signin, create a new appId. Screen looks like: Fill up and create the app. Post app creation, we need to goto the app and click "Add Product" at the bottom left corner and select "Messenger" from the options. After adding messenger, we need to setup Webhook. Click "Add Product" again and select Webhook, it should come under your products list now. Click on webhook, foll...

Facebook Messenger Chat Bot Tutorial - AI powered by wit.ai

Facebook has provided interesting way to allow you to build a chat bot for your FB page. I will guide you step by step to build this chat bot. This chat bot essentially is something that will receive the fb messages send to your page, process them and send back response. To start with, we need an app that would receive fb messages. To know how to step up a free app: click here Once we have setup your app and its up and running, we need to goto FB Developers and create an FB APP.

Facebook Integration with your website

Trie Tree Implementation in Java

Trie is supposedly the best suited DS for auto-suggest feature. package challenges; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class TrieTreeImpl { public static void main(String[] args) throws IOException, InterruptedException { File file = new File("/home/sundaramtiwari/Documents/CodeEval/trieTree.txt"); BufferedReader buffer = new BufferedReader(new FileReader(file)); String line = buffer.readLine().trim(); List<String> wordList = Arrays.asList(line.split(",")); Collections.sort(wordList); Map<Character, Node> children = new HashMap<Character, Node>(); TrieTree trie = new TrieTree(children); for (String word : wordList) { addW...

Java Program for a Deadlock

public class DeadLock{ public static void main(String[] args) throws InterruptedException { final Object obj = new Object(); final Object obj1 = new Object(); Thread t = new Thread() { public void run() { while (true) { synchronized (obj) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (obj1) { } } } } }; t.start(); while (true) { synchronized (obj1) { Thread.sleep(1000); synchronized (obj) { } } } } } Login with Facebook

Java Program for Langford Sequence of a given number

public class Langford {     private static boolean print(int array[], int k, int n) {         if (k == n + 1) {             return true;         }         for (int i = 0; i < array.length; ++i) {             if (array[i] == 0 && i + k + 1 < array.length && array[i + k + 1] == 0) {                 array[i] = array[i + k + 1] = k;                 if (print(array, k + 1, n)) {                     return true;                 }                 array[i] = array[i + k + 1] = 0;             }         }         return false;     }    ...

Multi Thread Synchronization by locks in java

Following java code prints natural numbers in sequence by multiple threads. Number of threads can be changed by the field "numOfThreads". If numOfThreads is 3, output will be like: Thread 1 -1, Thread 2- 2, Thread 3- 3, Thread 1- 4, Thread 2- 5 and so on till a max value defined by field "maxValue". package multi.thread.abc; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class MultiThreadSync { private static final int numOfThreads = 4; private static final int maxValue = 20; private final CyclicBarrier barrier; private final Counter counter; private final Thread[] threads; public MultiThreadSync () { counter = new Counter(numOfThreads, maxValue); barrier = new CyclicBarrier(numOfThreads + 1); threads = new Thread[numOfThreads]; for (int ...

Odd Even and Alphabet program in Java by 3 or more threads

Following program can print the below series: 1,2,a,3,4,b,5,6,d..... (By 3 threads) 1,2,3,a,4,5,6,b,7,8,9,d... (By 4 threads) 1,2,3,4,5,6,a,7,8,9,10,11,12,b.... (By 7 threads) Number of threads and max value can be set by their respective fields. package multi.thread.abc; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /**  *  * @author sundaramtiwari  *  */ public class MultiThreadAbc { /** Configure number of threads counting number series */ private static final int NUM_OF_THREADS = 3; private static final int MAX_VALUE = 54; private final int noOfThreads; private final MyCount counter; private final CyclicBarrier barrier; private final Thread[] threads; public MultiThreadAbc(int noOfThreads, int maxCounterValue) { this.noOfThreads = noOfThread...

Spiral Traversal of a 2 D matrix in java

Spiral traversal of matrix is a commonly asked interview question at preliminary levels now. Here is my attempt to solve it: package com.rivigo.main; /**  * @author Sundaram  *  */ public class SpiralTraversal { /** *  1    2   3   4 5    6   7   8 9   10  11  12 13  14  15  16 17  18  19  20 */ public static void main(String[] args) { int[][] input = new int[][] {{1,2,3,4,}, {5,6,7,8}, {9, 10, 11, 12}, {13, 14, 15, 16}, {17, 18, 19, 20}}; spiralTraversal(input); } public static void spiralTraversal (int[][] input) { if (input != null) { // Temp matrix to hold data and track traversal int[][] temp = new int[input.length][input[0].length]; travelRight(input, temp, 0, 0); } else { System.out.println("Input matrix is null!"); } } public static void travelRight(int[][] input, int[][] temp, int row, int col...

Decimal Representation of a number (comma separated) in java

package challenges; import java.util.Stack; public class DecimalRepresentation { public static void main(String[] args) { System.out.println(decimalRepresent(123456789)); } private static String decimalRepresent(int no) { String noStr = Integer.toString(no); char[] charArr = noStr.toCharArray(); StringBuffer sbr = new StringBuffer(); Stack<Character> stack = new Stack<Character>(); int pos = 0; for (int i=charArr.length-1; i>=0; i--) { if (pos == 3) { stack.push(','); pos = 0; } else { pos++; stack.push(charArr[i]); } } while(!stack.isEmpty()) { sbr.append(stack.pop()); } return sbr.toString(); } }

Print Even and Odd Numbers using two threads in java

Frequently asked multi-threading interview question in java is printing odd numbers by one thread and even numbers by another thread in ascending order. Interviewer wants to check if you are able to implement thread communication properly. package thread; public class EvenOddByThreads { public static void main(String[] args) { Object obj = new Object(); Thread odd = new Thread(new EvenOddThread(obj, 1)); Thread even = new Thread(new EvenOddThread(obj, 2)); odd.start(); even.start(); } } class EvenOddThread implements Runnable { private Object lock; private int start; public EvenOddThread(Object lock, int start){ this.lock = lock; this.start = start; } @Override public void run() { synchronized (lock) { for (int i=start; i<=20; i+=2) { try { System.out.println(i); lock.notify(); lock.wait(); } catch (Exception e) { } } } } }

Regex example in Java: Response Time Extractor

package client; import java.awt.Container; import java.awt.Desktop; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; class ResponseTimeExtractor extends JFrame { private static final long serialVersionUID = 1L; String emailableLocation, textLocation; JButton startConversion, openFile; JTextField emailableTextField, textTextField; JLabel emailableLabel, textLabel, result; ResponseTimeExtractor() { JFrame frame = new JFrame("Response Time"); frame.setDefau...