Saturday, 16 June 2018
Tuesday, 1 May 2018
Sunday, 1 October 2017
How to check Palindrome
Check if a string is palindrome or not is one of most common question we encountered during our basic days.
Let's Solve this problem with a Naive but efficient approach in this case.
Pseudocode :
- i=0, j=n
- while i < j:
- check if i'th element is not equal to j'th
- then return False
- else i++,j--
- loop end
- return True
Monday, 27 June 2016
When to use our own copy constructor while compiler already provides it
- #include <iostream>
- #include<cstring>
- #include<cstdio>
- using namespace std;
- class XYZ
- {
- public:
- char *p;
- XYZ(char *name,int l){
- p=new char[l+1];
- strcpy(p,name);
- }
- };
- int main() {
- XYZ obj1("str",3);
- printf("content of obj1.p is : %u\n",obj1.p);
- XYZ obj2=obj1;
- printf("content of obj2.p is : %u",obj2.p);
- }
So here content of member p of obj1 and obj2 is same which is base address of string str
Note that memory is allocated once and pointer to that memory is set for both objects. So default copy constructor only copying members value that is value of obj1.p ( which is address of string "str" ) is copied to obj2.p
Saturday, 25 June 2016
Private Constructor Function and Singleton Pattern
In C++ a constructor can be made private. But what is the use of private constructor ?
Think that you want to create a class in such a way that only one object can be created for this class something called Singleton pattern in C++.
Below is the program to do this.
- #include<bits/stdc++.h>
- using namespace std;
- class ABC
- {
- public:
- int t;
- static ABC creator()
- {
- static ABC x;
- return x;
- }
- private :
- ABC(){ t=10;}
- };
- int main()
- {
- ABC obj1=ABC::creator();
- cout<<" Value of t= "<<obj1.t;
- //ABC obj2=ABC::creator() returns same object
- }
How it works?
Static function are class variable not object's so static function 'creator()' called using class_name::function_name.
Static object is created inside static function. So private constructor function is called which is a valid call since it is from inside of a member function. So obj1 is created and returned.
Next time when we call static function it does not create new object since object is declare as static so same will be returned.
So only one object is created
Wednesday, 16 March 2016
Best way to download file in Mint/Ubuntu ..
Linux (Debian/Mint/Ubuntu) provide really very powerful tools for lots of things. Download is a common thing that we use in daily life.
Linux provide a very powerful command line tool that really enhance our download experience.
It is very powerful and very convenient. Introducing to you "wget" commond.
Linux provide a very powerful command line tool that really enhance our download experience.
It is very powerful and very convenient. Introducing to you "wget" commond.
Friday, 11 March 2016
Quick Dive to data handeling and machine learning in Python
This model show a quick way to apply different classification machine learning algorithm on data strored in CSV formate.
Python provide very powerful tools like pandas, numpy, and sklearn. we exploit them a lot.
Source Identification using 2 Class of image¶
In [72]:
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import normalize
import numpy as np
import pandas as pd
# matplotlib.pyplot as plt
Understanding Data¶
In [2]:
sony=pd.read_csv("Sony_NONIQM.csv")
sony.head()
73
In [3]:
nikon=pd.read_csv("Nikons_NONIQM.csv")
nikon.head()
Out[3]:
Saturday, 30 January 2016
Quick Python Pandas Basics
Lets Learn Pandas
In [1]:
import pandas as pd
Pandas series
pandas series is similar to numpy array, But it support lots of extra functionality like Pandaseries.describe()
Basic access is similar to numpy array, it support access by index( s[5] ) or slicing ( s[5:10] ).
It also support vectorise operation and looping like numpy array.
Implemented in C so it works very fast.
Note : Get Code for offline testing Github_Link or nbviewer.jupyter link
Learn Basic of Numpy
Benfits of Pandas series¶
In [8]:
s=pd.Series([2,3,4,5,6])
print s.describe()
count 5.000000 mean 4.000000 std 1.581139 min 2.000000 25% 3.000000 50% 4.000000 75% 5.000000 max 6.000000 dtype: float64
Friday, 29 January 2016
Basics of Numpy & Pandas
Numpy and pandas are very popular packages used for data analysis. this post provide you quick guide to learn the basics of these. We assume you are familiar with vector operation .
Numpy is mostly written in C & pandas are written over numpy & also uses C programming language as core implementation So these two are fast as compare to general data structure provided with python.
Numpy array is similar to python list but it contain all data of same data-type & it is much faster.
we can also treat numpy array as a vector.
Pandas uses scalar instead of list or numpy array but we can also visualize it as numpy array with some advance functionalities.
Example
Numpy uses array whereas pandas used scaler
In [2]:
import numpy as np
Array are similar to python list , but it all element must be of same data type, and it faster than list¶
In [13]:
num = np.array([3,4,2,5,7,23,56,23,7,23,89,43,676,43])
num
Out[13]:
array([ 3, 4, 2, 5, 7, 23, 56, 23, 7, 23, 89, 43, 676, 43])
Lets see some of functionality¶
Importing CSV File in Python
CSV file is one of most common used format we encounter. Let see few technique to efficiently import these data to python for better & efficient use.
CSV is also known as comma separated format. you can easily write a code to import CSV data .
But today we will see some of very efficient way to do so.
Python provide few package like CSV, unicodecsv, pandas Thease packages help you to do your job.
CSV is also known as comma separated format. you can easily write a code to import CSV data .
But today we will see some of very efficient way to do so.
Python provide few package like CSV, unicodecsv, pandas Thease packages help you to do your job.
- CSV : package follow basic approach & slow , it reads data line by line & split.
- unicodecsv : Import data & create a list of dictionaries each data is associated with its attribute as key. So it coud be very usefull in many cases but this process is also slow
- pandas : This technique is very fast as compare to above two methods.
You can download & test in your computer using Ipython notebook [Github_link]
Example
In [2]:
import unicodecsv
import pprint
import csv
In [3]:
csv_file_name="enrollments.csv"
Json Import of csv file¶
Import data in json formate, All data are imported as string¶
In [16]:
enrolment = []
f=open(csv_file_name,'rb')
reader =unicodecsv.DictReader(f)
#reader is a itterater so loop is possible only once
print "type(reader) =",reader
#for each_row in reader:
# enrolment.append(each_row)
enrolment=list(reader) #shorthand for above two line
#close file
f.close()
print "Total no of row : ",len(enrolment),"\n\n"
#print demo data
pprint.pprint(enrolment[1])
type(reader) = <unicodecsv.py2.DictReader instance at 0x04A10A08>
Total no of row : 1640
{u'account_key': u'448',
u'cancel_date': u'2014-11-10',
u'days_to_cancel': u'5',
u'is_canceled': u'True',
u'is_udacity': u'True',
u'join_date': u'2014-11-05',
u'status': u'canceled'}
## Simple import
Wednesday, 20 January 2016
Pattern Search using Regular Expression in Python
In our daily life we found lots of problem of pattern search. like find all mobile numbers, or emails etc from given web page or from any file.
Writing manual code for that is not efficient and also very messy. Regular Expression is very popular technique used for pattern search (All compiler & interpreter use it ) & it is very easy to implement & it is very efficient .
I suggest you to write a code for extracting all emails from a webpage without using regular expression & test it .
Emails found on a webpage may follow some of these pattern like
Writing manual code for that is not efficient and also very messy. Regular Expression is very popular technique used for pattern search (All compiler & interpreter use it ) & it is very easy to implement & it is very efficient .
I suggest you to write a code for extracting all emails from a webpage without using regular expression & test it .
Emails found on a webpage may follow some of these pattern like
Tuesday, 10 November 2015
Pong Game In python
This is very easy if you are intermediate in python and good in basic physics and basic maths.
You need to know basics of reflection, velocity, acceleration, and basics mathematics in order to calculate collision .When boundary of pong just touches wall or paddle are situation of collision. rest of thing is just basic python.
We need to increase velocity of pong with time so we add some constant small acceleration that is being triggered after some small time interval . why small because if we use large acceleration or time then change in velocity will be sudden, so speed of pong increase suddenly.
You need to know basics of reflection, velocity, acceleration, and basics mathematics in order to calculate collision .When boundary of pong just touches wall or paddle are situation of collision. rest of thing is just basic python.
We need to increase velocity of pong with time so we add some constant small acceleration that is being triggered after some small time interval . why small because if we use large acceleration or time then change in velocity will be sudden, so speed of pong increase suddenly.
Saturday, 7 November 2015
Using SVM Classifier
Support vector Machine (SVM) is one of most famous machine learning tool for classification problem. This is supervised learning technique .Read More
we are going to see how to use SVM classifier in python.
Our Demonstration uses digit dataset . This dataset uses 64 feature vector to identify handwritten digit [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] . It means this 64 feature extracted from Handwritten digit used to classify handwritten digit in 9 classes .
So Again we gonna use skilearn Python Package .
| SVM Margin |
Our Demonstration uses digit dataset . This dataset uses 64 feature vector to identify handwritten digit [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] . It means this 64 feature extracted from Handwritten digit used to classify handwritten digit in 9 classes .
So Again we gonna use skilearn Python Package .
Friday, 6 November 2015
Using Naive Bayes classifier
This is most basic classifier, It uses concept of probability in order to predict the class of input feature data . Read More
We gonna see how to use Naive Bayes Classifier very easily using python.
We gonna see how to use Naive Bayes Classifier very easily using python.
![]() |
| Bayes rule |
Prerequisite:
- Python 2
- sklearn [Python Module]
- Numpy [Python Module]
We are going to apply this algorithm on Iris dataset .
Thursday, 24 September 2015
Python In Sort
Python is one of most famous Programming Language. This is summary of python programming language for Quick Start to Python Programming .
{ Zoom or download picture}
{ Zoom or download picture}
Saturday, 19 September 2015
Easieast Way to create Access Point Hotspot in Ubuntu 15.04
Creating Hotspot in Ubuntu is easier task . But many people (beginners) find hard to cerate hotspot access point to connect other devices like Android phone,tablets etc.
So , I gonna so you A graphical way To do it very easily.
So , I gonna so you A graphical way To do it very easily.
- Download kde-nm-connection-editor (Plasma-nm) from Ubuntu Software center. kde-nm-connection-editor. (you may not directly find it in software search so follow link given, or you may search and install plasma-nm)

kde-nm-connection-editor link
Friday, 18 September 2015
Sunday, 13 September 2015
Array Aptitude
Divide array in two part such that sum of array left to partition index i is equal to sum of array right to partition index i.
That is A[0]+A[1]...+A[i-1]=A[i+1]+....+A[n]
This is slightly tricky question but very easy to solve and code ,if you find the trick.
That is A[0]+A[1]...+A[i-1]=A[i+1]+....+A[n]
This is slightly tricky question but very easy to solve and code ,if you find the trick.
Algorithm:
- Find the cumulative sum at each index of array
- loop through array and check A[i-1]==A[n]-A[i] , If condition Satisfy for any index i, means array can be partition in two group with this condition,So print "YES" else "NO"
- Use special case if array has only 1 element then print YES
- if array has only two element then print "NO"
Saturday, 5 September 2015
Finding First and Last occurrence in sorted list
Finding first and last occurrence of given number in sorted list is a very easy problem and can easily solved by leaner search with worse case Time complexity O(n) .
But We can Achieve faster search of first and last occurrence of a number by exploiting sorted feature of list.
We can search occurrence of any number in sorted list in O(log(n)) Time Complexity by using Binary search Algorithm.
But We can Achieve faster search of first and last occurrence of a number by exploiting sorted feature of list.
We can search occurrence of any number in sorted list in O(log(n)) Time Complexity by using Binary search Algorithm.
Subscribe to:
Comments (Atom)







