Wednesday 23 September 2015

Check the Existence of a File Using Perl

A simple Perl script to check the existence of the file in the destination place.

Perl Code: 
#check the existence of the file if yes say file exists else terminate the program
$filename = 'G:\\GPB\\protoc-3.0.0-alpha-3-win32\\protoc.exe';
 if (-e $filename)
 {
    print "File Exists!";
 }
 else
 {
    die ("$!\n");   
 }



OutPut:

 > perl CheckExsistenceOfFile.pl
 File Exists!

Tuesday 8 September 2015

Chef and operators

Following is the program form  https://www.codechef.com/problems/CHOPRT

Chef has just started Programming, he is in first year of Engineering. Chef is reading about Relational Operators.

Relational Operators are operators which check relatioship between two values. Given two numerical values A and B you need to help chef in finding the relationship between them that is, 

  • First one is greater than second or,
  • First one is less than second or,
  • First and second one are equal.


  • Input

    First line contains an integer T, which denotes the number of testcases. Each of the T lines contain two integers A and B.

    Output

    For each line of input produce one line of output. This line contains any one of the relational operators

    '≺' , '≻' , '='.

    Program:

    #include <iostream>
    using namespace std;
    int ProcessNumbers (int InCount);

    int main() {
        // testcase control parameter
        int TestCaseNo;
        int FirstNo, SecondNo;
        cin>>TestCaseNo;
        ProcessNumbers(TestCaseNo);
    return 0;
    }

    int ProcessNumbers(int InCount)
    {
        int FirstNo, SecondNo;
        char output_symbol[3];
        for (int i = 0; i < InCount ; i++ )
        {
            cin>>FirstNo>>SecondNo;
            
            if (FirstNo < SecondNo)
                output_symbol[i] = {'<'};
            else if (FirstNo > SecondNo)
                output_symbol[1] = {'>'} ;
            else 
                output_symbol[i] = {'='} ;
                
        }
        
        for (int j = 0; j < InCount ; j++ )
        {
            cout << "\n" <<output_symbol [j];
        }
       }

    InPut:
    3
    2 3
    4 6
    4 4 
    OutPut:

    <
    >
    =

    Saturday 3 January 2015

    FIZZ BUZZ

    Write a program that prints the numbers in the given range. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Print a new line after each string or number.

    Input Format First line will be the number of testcases, T. Next line will have T integers, denoted by N.

    Output Format For each testcase, print the number from 1 to N. But follow the rules given in the problem statement.

    Constraints

    1 <= T <= 10

    N is an integer.

    Input File

    Expected Output

    You can also look at the solution that passes all test cases for this problem.

    View C Solution

    View Java Solution
    Sample Input (Plaintext Link)

    2
    3 15

    Sample Output (Plaintext Link)

    1
    2
    Fizz
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz



    /* Program */

    #include <iostream>
    using namespace std;

    int main()
    {
        int n;
        int a[10];
        // as per constraints given
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
       
        for(int j=0;j<n;j++)
        {
            for(int c=1;c<=a[j];c++)
            {
                if(c%3==0 && c%5==0)
                 cout<<"FizzBuzz"<<endl;
                else if(c%3==0)
                 cout<<"Fizz"<<endl;
                else if(c%5==0)
                 cout<<"Buzz"<<endl;
                else
                 cout<<c<<endl;
            }
        }

    return 0;
    }