Skip to main content

Posts

Showing posts with the label Programming

Python : if else in python

The below example show how to use if else in python.A variable a is initialized to 1. Then in the if - else conditon the variable is searched and if matched then that branch is taken. in this example the else if condition is matching. #!/usr/bin/python a = 1 if a == 10 :     print "The variable is not matching" elif a == 1:     print  "The variable ", a ," is matching" else:     print "No condition matched"

Python : for loop in python

Example 1 :The following example shows how to use for loop in python. a list is created with elements in it. The variable i runs for each item in the list increment it by 1 implicitly. Inside the loop each item in printed. #!/usr/bin/python list = [1,2,3,4,5] for i in list:     print i output: 1 2 3 4 5 Example 2 : If we have integer value and need to loop that many number of time the below can be used. a = 5 for i in range(0,5):     print i

Python : Read a file in python

  Here I will show you how to read a file and display the contents in python. The following code in python will read the file and display it on console.  Assume a file with name "file.txt" in present in the current directory.   #!/usr/bin/python #python code to read and display a file    file = open("file.txt","r") print file.read() file.close() After running this code it will display the content of the file immediately to the consol. Lets see the various mode of operating a file in python r -  This will open the file in read mode w - This will open the file in write mode. If any other file with same name is present in the location it will erase it ans create a new one. a - This will open the file in append mode. The new data is added to the end of the file  r+ - This will open the file in both read and write mode

R programing language

R is a programing language for statistical computing. Unlike other programing languages used by programers to create high end application, R is mainly used to compute or solve problems in data analysis or statistical computing. The following link is a good resource to learn R language in a interactive way. http://tryr.codeschool.com

UNIX : grep command examples

GREP (Global Regular Expression and Print) is a utility used to search for lines containing the string in a file. Syntax of GREP is :  grep [option] [pattern] [ files] 1. Search for lines in a file that contains a word . > grep "word" <filename> 2. How to search for more than one matching string in a file? >egrep "a|b|c" <filename>

UNIX interview questions

1. Display 5th line of a file. cat <file> | head -5 | tail -1 2. How to reverse a string in unix? echo "reverse this string!" | rev 3. How to select last word of a line? echo "i need the last word" | rev | cut -f1 -d' ' | rev 4. Select nth value from a comma seperated line. echo "a,b,c d,e,f,g ,h"| cut -f<n> -d',' 5. How to insert a header record to a file. sed -i "1i HEADERTEXT" <outputfile>

UNIX SCRIPT : Read text file and write calculate sum

The following KSH script read the contents of a text file in CSV format , calculate its sum line by line and write the result to another file.   #!/usr/bin/ksh #PROGRAM BEGINS HERE #DEFINING OUTPUT FILE OUTPUTFILE=result.txt; #CHECK FOR INPUT FILE if [[ $# -eq 0 ]];then         print "No Files";         exit fi; #ACCEPTING INPUT FILE IF PRESENT FILE=$1; #READ FILE CONTENT LINE BY LINE while read FILE do         VAR1=`echo $FILE| cut -d "," -f1`;         VAR2=`echo $FILE|cut -d "," -f2`;         SUM=`expr $VAR1 + $VAR2`;         echo $SUM >> $OUTPUTFILE; done < "$FILE" #PROGRAM ENDS HERE

Simple calculator program in android

Hi everyone.Here I am going to put a simple calculator program in Android. Please find the required files 1. MainActivity.java 2. activity_main.xml (Layoutfile) 3. String.xml 1. MainActivity.java /* MainActivity starts here */ import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { /* Variable declaration starts here */ Button plusButton ;      Button minusButton; Button mulButton ; Button divideButton; EditText number1EditText ; EditText number2EditText; TextView result;    /* End of variable declaration*/ /*Please don't give any code inside these two function */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); variable_defenition(); call_to

Open CV : Program to display an image

Now I will show a program to display an image with C++ in OpenCV. Open a new project in Visual C++ and open a Win32 console program file. Add the required library file as I have shown in my previous post. Here is a program to read a picture file from a location in your system and display the picture. Add the required header files as shown below and execute the program by pressing F5 . Here I am showing the output of the above program. Now you have understood the basics of running a OpenCV program with Visual C++. I will explain more in coming posts.Stay with me....

Mount an iso image in ubuntu using no GUI

We can easily mount an iso image file in our system to a directory in linux.We need not require an GUI application to do this.It is easy to do from the terminal itself. We use the mount command for that. The steps are as follows  1. Open the terminal in ubuntu (Shortcut press ctrl+Alt+t) 2. Create a directory to mount the iso image              $sudo mkdir /media/myimage 3. Then type the following command     $sudo modprobe loop 4.After that go to the location of the iso image file and type     $sudo mount /media/myimage -t iso9660 -0 loop     The iso file is mounted to the specified directory To unmount it give the command   sudo unmount /media/myimage That all.Cool isn't! sudo modprobe loop will loads the module for loopback file system iso9660 is the file system used by CD-ROM -t specify the file system type -o loop additional option used by a loopback filesystem

How to make category in blogger

You can categorise similar posts in blogger with the 'LABEL' option. For that login to blogger.com.Then select 'design tab' and select  the 'page element' tab.Then add 'LABEL' by selecting 'add a gadget'.Save it. When we create a post ,below the content area there is label textbox.We can either give a new label or select an existing label from options. The content always appears as a new post in mixed fashion,but on selecting a label option from the sidebar in the blog post under a label are grouped.  

C programing : File

/* Program to take lines 1,4,7,10.... from a text file and to write into a new file. It is written in c with platform devcpp*/ /* eg:- input file -> ab.txt contain 1 lady gaga sdrgrg ergerg 2 oraph winfrey dfgdr dfgdf . output file neww.txt 1 lady gaga 2 oraph winfrey . . */ #include #include #include using namespace std; main() { char c; FILE *fp1,*fp2; if(fp1=fopen("e:\\ab.txt","r")) { cout<<"\n opened fp1"; } else { cout<<"\n failed fp1"; } if( fp2=fopen("e:\\neww.txt","w") ) { cout<<"\n opened fp2"; } else { cout<<"\n failed fp2"; } while(!feof(fp1)) { c=getc(fp1); while(c!='\n') { putc(c,fp2); c=getc(fp1); } putc('\n',fp2); c=getc(fp1); while(c!='\n') { c=getc(fp1); } c=getc(fp1); while(c!='\n') { c=getc(fp1); } } cout<<"\n End"; getch(); return 0; }

First program with opencv in ubuntu

#include "cv.h" #include "highgui.h" int main( int argc, char** argv )  { IplImage* img = cvLoadImage(argv[1]); cvNamedWindow("Example1",CV_WINDOW_AUTOSIZE); cvShowImage("Example1", img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("Example1"); } The above example is taken from OReilly Learning OpenCV.  Save the above program as test.c file . Execute the following command in terminal. g++ -ggdb `pkg-config opencv --cflags --libs` test .c -o opencv_example if no error occurs run   ./opencv_example image1.jpg This will display the image.