UIU: Competitive Programming: Class – 03


Palindrome check


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>
#import <algorithm>

using namespace std;

bool isPalindrome(char str [])
{
    int length = strlen (str);

    char copy [length];

    // making a copy of original string
    strcpy(copy, str);

    reverse(copy, copy + length);

    return strcmp(copy, str) == 0;
}

int main(int argc, const char * argv[])
{
    char input [100];

    scanf ("%s", input);

    if (isPalindrome(input)) {
        printf ("Palindrome\n");

    } else {
        printf ("Not Palindrome\n");
    }

    return 0;
}

// @END_OF_SOURCE_CODE

Prime factor


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#import <cmath>

using namespace std;

bool isPrime(int n)
{
    if ( n < 2 ) return false;

    if ( n == 2 ) return true;

    if ( n % 2 == 0 ) return false;

    int squareRoot = (int) sqrt(n);

    for ( int i = 3; i <= squareRoot; i += 2 ) {
        if ( n % i == 0 ) return false;
    }

    return true;
}

int nextPrime(int n)
{
    while(!isPrime(++n));

    return n;
}

int main(int argc, const char * argv[])
{
    // 36 = 2 * 2 * 3 * 3;
    int input;

    scanf ("%d", &input);

    int primeNumber = nextPrime(0);

    while (input != 1) {
        while (input % primeNumber == 0) {
            printf ("%d\n", primeNumber);
            input /= primeNumber;
        }

        primeNumber = nextPrime(primeNumber);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

GCD and LCM


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int gcd(int a, int b)
{
    if ( a == 0 ) return b;

    return gcd(b % a, a);
}

int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}

int main(int argc, const char * argv[])
{
    printf ("%d\n", gcd(42, 36));

    printf ("%d\n", gcd(36, 42));

    printf ("%d\n", lcm(36, 42));

    return 0;
}

// @END_OF_SOURCE_CODE

Input from a file and Output in a file

1. Create a file named “in.txt” in same location of your source cpp file

2. Write two integer number in “in.txt” file

for example,
2 5

// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int main(int argc, const char * argv[])
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    int a, b;

    scanf ("%d %d", &a, &b);

    printf ("%d\n", a + b);

    return 0;
}

// @END_OF_SOURCE_CODE

UIU: Learn C by Examples : Reverse


Reverse string naive approach


#include <cstdio>
#include <cstring>

using namespace std;

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    char reverseString [50];

    int reverseIndex = lengthMainString - 1;

    for ( int i = 0; i < lengthMainString; i++ ) {
        reverseString [reverseIndex] = mainString [i];
        reverseIndex--;
    }

    reverseString [lengthMainString] = '\0';

    printf ("Reverse String: %s\n", reverseString);

    return 0;
}


Main String: Hello
Reverse String: olleH

Reverse string (length / 2) approach


#include <cstdio>
#include <cstring>

using namespace std;

void swap (char *a, char *b)
{
    char temp = *b;
    *b = *a;
    *a = temp;
}

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    for ( int i = 0; i < lengthMainString / 2; i++ ) {
        swap(&mainString [i], &mainString [lengthMainString - 1 - i]);
    }

    printf ("Reverse String: %s\n", mainString);

    return 0;
}


Main String: Hello
Reverse String: olleH

Reverse string STL approach


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    reverse(mainString, mainString + lengthMainString);

    printf ("Reverse String: %s\n", mainString);

    return 0;
}



Main String: Hello
Reverse String: olleH

Reverse number sprintf approach


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main(int argc, const char * argv[])
{
    int n = 12345;

    char number [50];

    sprintf(number, "%d", n);

    reverse(number, number + strlen (number));

    printf ("Reverse Number: %s\n", number);

    // convert a string to integer
    int reverseNumber = atoi(number);

    printf ("Reverse Number: %d\n", reverseNumber);

    return 0;
}


Reverse Number: 54321
Reverse Number: 54321

Reverse number modulus approach


#include <cstdio>

using namespace std;

int reverseNumber(int n)
{
    int ret = 0;

    while ( n ) {
        ret = 10 * ret + n % 10;
        n /= 10;
    }

    return ret;
}

int main(int argc, const char * argv[])
{
    int n = 12345;

    printf ("Reverse Number: %d\n", reverseNumber(n));

    return 0;
}


Reverse Number: 54321

Trip To Thailand With Kaz Software


Launch from Bangladesh

Launch from BD

Suvarnabhumi Airport

Thailand Airport

Snacks at KFC

Snacks

Toilet for Men

Male Toilet

Toilet for Women

Female Toilet

Krabi Aonang Resort

Hotel room

Krabi Beach

IMG_0148

IMG_0153

IMG_0252

Railey Beach, Krabi

IMG_0284

IMG_0293

IMG_0298

IMG_0311

Out of Nowhere

IMG_0353

IMG_0369

Phi Phi Island

IMG_0375

IMG_0472

Out of Nowhere again

IMG_0474

IMG_0476

IMG_0511

Another Beach (forgot the name!)

IMG_0521

IMG_0525

IMG_0526

Again Another Beach (forgot the name! again!!)

IMG_0554

IMG_0560

IMG_0561

IMG_0562

IMG_0583

IMG_0589

IMG_0603

Bangladeshi Restaurant

IMG_0606

Street Food, Bangkok

IMG_0610

IMG_0617

Unicorn Express Hotel

IMG_0620

IMG_0621

IMG_0630

IMG_0631

IMG_0635

Way to Siam Paragon

IMG_0640

Siam Paragon (Shopping Mall)

IMG_0648

IMG_0655

IMG_0662

IMG_0663

IMG_0682

Buffet Breakfast Unicorn Express Hotel

IMG_0692

IMG_0697

IMG_0701

IMG_0704

Dragon Fruit

IMG_0707

Prayer place, Siam Center

IMG_0721

MDK Center, Bankok (Shopping mall)

IMG_0727

Bangkok Road

IMG_0728

Important consideration before accepting a Development job offer


You should consider the following acceptance criteria before making a decision to take a Software development job offer.

If the company uses Source Control Management.

If the company follows Software Development cycle

If the company uses Continuous integration system

If the company has Senior/Principal Software Engineer/Architect

If you need to work in a team or alone

If the company has QA (Quality Assurance) team

If you need to overwork.

Courtesy: Shah Ali Newaz (Topu) vai

Top and Most commonly used Git commands you should know


Currently, I am using MacOS-X 10.9; though I believe the following commands will work irrespective of your Hardware and OS. I grouped the commands by their relevancy and their necessity base (basic commands first). The target audience are developers, who know git theoretically and also use git in day to day work but having hard time remembering the commands.

After successful Installation:

check to see where is the git located

~$ which git
/usr/bin/git

the version of the git installed

~$ git --version
git version 1.8.3.4 (Apple Git-47)

Configuration:

System wide git configuration file path: /etc/gitconfig
This file is not used or modified commonly.

file path for Windows: Program files\Git\etc\gitconfig

User git configuration file path: ~/.gitconfig
file path for Windows: $HOME\.gitconfig

Project git configuration file path: PROJECT/.git/config

set user name (User configuration)

~$ git config --global user.name "YOUR USER NAME" 

--global [indicates User configuration, not System configuration]
--system [indicates System wide configuration]

set user email id (User configuration)

~$ git config --global user.email "YOUR EMAIL ID"

set default text editor for git

~$ git config --global core.editor "textedit -wl1"

-w [indicates, git should wait for next task until editor is closed]
l1 [indicates, the text editor will put the cursor at line 1 when starts]

use colors with git command line

~$ git config --global color.ui true

Current status of configuration

~$ git config --list
user.name=shahab.uddin
user.email=tausiq19@gmail.com
editor="textedit -wl1"
color.ui=true

Auto Completion for Git:

Windows already have Auto completion feature already. So the instruction are for Mac and Unix user only

Download the completion file form Github

~$ cd ~
~$ curl -0L https://github.com/git/git/raw/master/contrib/completion/git-completion.bash

Rename file

~$ mv ~/git-completion.bash ~/.git-completion.bash

Open bash profile file

~$ nano .bash_profile

Edit bash profile file
add this code to the file

if [ -f ~/.git-completion.bash ]; then
    source ~/.git-completion.bash
fi

now hit: ctrl + x to close the file
now hit: y to say, yes I want to save the file
now hit: enter to go back
restart terminal (quit and open again)

Git Help:

Open git help

~$ git help

Open git help for specific command

~$ git help log

hit f to move forward
hit b to move backward
hit q to quit

Starting Git

Initialize a project
Select a folder where you would like to create a project (that folder will be tracked by Git).
On terminal go to that folder (write cd and drag drop the folder)

~$ git init
Initialized empty Git repository in /Users/Documents/Sourcecode/Git/first_git_project/.git/

Look inside what Git Initialized

~$ ls -la
total 0
drwxr-xr-x   3 Shahab  staff  102 Nov 24 22:17 .
drwxr-xr-x   3 Shahab  staff  102 Jul 14 05:12 ..
drwxr-xr-x  10 Shahab  staff  340 Nov 24 22:15 .git
~$ ls -la .git/
total 24
drwxr-xr-x  10 Shahab  staff  340 Nov 24 22:15 .
drwxr-xr-x   4 Shahab  staff  136 Nov 24 22:17 ..
-rw-r--r--   1 Shahab  staff   23 Nov 24 22:15 HEAD
drwxr-xr-x   2 Shahab  staff   68 Nov 24 22:15 branches
-rw-r--r--   1 Shahab  staff  138 Nov 24 22:15 config
-rw-r--r--   1 Shahab  staff   73 Nov 24 22:15 description
drwxr-xr-x  11 Shahab  staff  374 Nov 24 22:15 hooks
drwxr-xr-x   3 Shahab  staff  102 Nov 24 22:15 info
drwxr-xr-x   4 Shahab  staff  136 Nov 24 22:15 objects
drwxr-xr-x   4 Shahab  staff  136 Nov 24 22:15 refs

Add files to Git

~$ git add .

. [indicates that all files inside the directory will be added to Git]

~$ git add firstFile.txt

First commit with Git

~$ git commit -m "Initial commit"
[master (root-commit) 468be11] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 firstFile.txt

-m [indicates that we are going to add a message next]

Few tips on commit messages

  • write commit messages is present tense, not past tense.
  • write “fix bug” or “fixes bug”, not “fixed bug”
  • you can add bullet points using asterisks or hyphen
  • Its a good idea to add ticket tracking numbers/ bug report number with commit
  • Be clear and descriptive
  • for example,
    Bad: “Fix typo”
    Good: “Add missing > in project section of HTML”
  • another example,
    Bad: “Update login info”
    Good: “Change user authentication to Blowfish”

A good commit message example

t23094 – Fixes bug in admin logout

When an admin logged out of the admin area, they could not log in to the members area because their session[:user_id] was still set to the admin ID. This patch fixes the bug by setting session[:user_id] to nil when any user logs out of any area.

Git Log

View the last commits

~$ git log
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

View the last commits upto limited number

~$ git log -n 1
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

-n 1 [it will show the last 1 commit]

View the last commits since up to a date

~$ git log --since=2013-11-23
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

View the last commits until a date

~$ git log --until=2013-11-25
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

View the commits of a particular author

~$ git log --author="shahab.uddin"
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

View the commits with a regular expression

~$ git log --grep="Init"
commit d68b938b21642db657d23b8b6bcf4da310635ee5
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sun Nov 24 22:22:01 2013 +0600

    Initial commit

Find out where is the HEAD pointing now

~$ git log HEAD
commit 468be1105dc2190f76335fcdb3f708a58ecaafaf
Author: shahab.uddin <tausiq19@gmail.com>
Date:   Sat Dec 14 00:22:21 2013 +0600

    Initial commit

Git Diff

View the difference of the working directory

~$ git diff
diff --git a/firstFile.txt b/firstFile.txt
index 55d1db5..8ee1b5a 100644
--- a/firstFile.txt
+++ b/firstFile.txt
@@ -1 +1,3 @@
 This is my First File.
+
+I have changed this portion of text.

Scenario
1. You have created a file (firstFile.txt)
2. made some changes to that file
3. add it to the staging index ( using, git add . or, git add firstFile.txt )
4. make some more changes to firstFile.txt
5. Now you would like to view the difference between the staged index and working directory
6. the above command means, show me the difference between the staging index and present working copy

[to see the difference of a specific file, use git diff firstFile.txt ]

View the difference of the staging index

~$ git diff --staged
diff --git a/firstFile.txt b/firstFile.txt
index 55d1db5..8ee1b5a 100644
--- a/firstFile.txt
+++ b/firstFile.txt
@@ -1 +1,3 @@
 This is my First File.
+
+I have changed this portion of text.

Scenario:
1. you have added a file to the staging index, for the 2nd time
2. now you would like to view the changes you made on the 2nd time
3. the above command means, what are the changes in my repository and staging index

Remove file from Git

Remove file form Git repository

Removing a file from git repository is a two step process.
1. remove the file from git repository
2. make a commit that you have removed the file

~$ git rm firstFile.txt
rm 'firstFile.txt'
~$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    firstFile.txt
#
~$ git commit -m "first file removed"
[master c80278c] first file removed
 1 file changed, 3 deletions(-)
 delete mode 100644 firstFile.txt

To be continued

Acknowledgment: Kevin Skoglund from lynda.com