Sunday, 23 December 2018

Hanging Posters Hacker Rank solution in python3

Problem:
https://www.hackerrank.com/contests/hourrank-31/challenges/hanging-posters/problem

Solution:
#!/bin/python3

from math import *

n,h=map(int, input().split())
wp = list(map(int, input().split()))
pl = list(map(int, input().split()))
ans=0
for i in range(n):
    if wp[i] <= h:
        temp=0
    else:
        temp = wp[i]+pl[i]*1/4  #height of top of the poster from ground level
        temp=temp-pl[i]*1/2-h   #reduce the height of arthur and half of height of poster
   
    if ans<temp:
        ans=temp
print(ceil(ans))       


Thursday, 8 November 2018

Chef and Easy Problem CodeChef March Challenge solution in C++ using bitset

 Problem : https://www.codechef.com/MARCH18B/problems/XXOR/


 Solution:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <bitset>
using namespace std;

int main() {

    long long int n,q;
    cin>>n>>q;
    long long int temp,bc[n];
    bitset<31> bin[n];
    for(long long int i=0;i<n;i++)
    {
        cin>>temp;
        bin[i]=temp;
        bc[i]=floor(log2(temp))+1;
    }
    //cout<<bin[0];
     while(q--)
    {
        long long int a1,b1;
        bitset<31> b;
        b.set();
        cin>>a1>>b1;
        a1--;
        b1--;
        long long int max=bc[a1];
        for(long long int i=a1+1;i<=b1;i++)
        {
            if(max<bc[i])
                max=bc[i];
        }
      
        for(long long int j=max-1;j>=0;j--)
        {
            long long int c0=0,c1=0;
            for(long long int i=a1;i<=b1;i++)
            {
                if(bin[i][j]==1)
                    c1++;
                else
                    c0++;
            }
            if(c1>=c0)
            b.reset(j);
        }
        cout<<b.to_ulong() << '\n';
    }
    return 0;
}

Tuesday, 23 October 2018

Comparator function in C

int compare (const void * a, const void * b)
{
 if(*(int*)a < *(int*)b )
    return 1; //if a<b do swapping
 return -1;   //else donot do swapping
}

int main ()
{
int arr[] = {10, 5, 15, 12, 90, 80};
int n = sizeof(arr)/sizeof(arr[0]), i;

qsort (arr, n, sizeof(int), compare);

for (i=0; i<n; i++)
    printf ("%d ", arr[i]);
return 0;
}

Thursday, 6 September 2018

Strong number, Spy number C program

Check whether a given number is strong or not C program
#include<stdio.h>
main()
{
    int n,f,sum=0,temp,r,i;
    scanf("%d",&n);
    temp=n;
    while(n>0)
    {
        f=1;
        r=n%10;
        for(i=1;i<=r;i++)
            f=f*i;
        sum=sum+f;
        n=n/10;
    }
    if(sum == temp)
        printf("strong");
    else
        printf("not strong");
}
 Check whether a given number is spy number or not C program
#include<stdio.h>
main()
{
    int n,f=1,sum=0,r,i;
    scanf("%d",&n);
   
    while(n>0)
    {
        r=n%10;
        f=f*r;
        sum=sum+r;
        n=n/10;
    }
    if(sum == f)
        printf("spy");
    else
        printf("not spy");
}

Monday, 21 May 2018

How to send Whats App Messages Automatically Using the data in Excel

To Send What's App messages automatically to contact numbers provided in excel.

1. First export the contacts in excel to your gmail account.
you can watch the video in the below link or follow the instructions below

https://www.youtube.com/watch?v=rDbXq-EemdM

You can go to google contacts and download the contacts.csv file and copy the contacts in excel
and paste it in contacts.csv file.

2. Once all the contacts are exported to gmail. Sync those contacts in your mobile

3. You will get those contacts added automatically and will show as contacts in your mobile.

4. You can now broad cast messages to your contacts by selecting new Broadcast.
In Whatsapp account, click on 3 dots and from drop down menu select New Broadcast. When you select New Broadcast in your mobile, you can same message to 256 contacts with out forming a group.


5. Or you can also download the vb script in the below link
https://drive.google.com/file/d/1qx9nPX9r9GcukAj3Nb7AkmhrnBIDu49x/view?usp=sharing

and follow the instructions in the below video
https://www.youtube.com/watch?v=vhcgZDKVQT8

Sunday, 20 May 2018

Fibonacci 2048 game : Program in C

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include<time.h>
#include<ctype.h>
#include <time.h>
#include <windows.h>
#include <process.h>

#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77

#define SIZE 4
#define BORDER 219
#define ESC 57
#define PR 2
int a[20];
int Matrix[SIZE+1][SIZE+1]={0};
int score=0,last=0,count = 2584;
void starting()
{
 
    printf("\n\n\n\t\t\tINSTRUCTION\n\n\n");
    printf(" -> Use the arrow keys to move\n");
    printf(" -> Combine numbers that are next to each other in the Fibonacci sequence\n to make bigger numbers\n\n");
    printf(" -> For wining this game any one box must have value 2584. \nAlso You have 2584 tries to win\n\n\n\n");
    printf("\t\t\t\t\t\tPRESS ANY KEY TO PLAY\n");
    while(!kbhit());
}
void print_ever()
{
    printf("\n\n\n\t\t\tFIBONACCI TWO-ZERO-FOUR-EIGHT\n\n");
    printf("\t\t\t\t\t\t SCORE  : %d\n\n\n\n",score);
    printf("\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER);

}
void Display()
{
    int i,j;
    system("cls");
    print_ever();
    for(i=0;i<SIZE;i++)
    {
        printf("\t\t\t %c",BORDER);
        for(j=0;j<SIZE;j++)
        {
            if(Matrix[i][j]==0)
            {
                printf("      ");
            }
            else
                printf(" %4d ",Matrix[i][j]);
        }
        printf("%c\n\t\t\t %c                        %c\n",BORDER,BORDER,BORDER);
    }
    printf("\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n\n",BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER,BORDER);
}
void Random_creater()
{
    int temp1,temp2,i,j;
    srand(time(NULL));
    temp1=rand()%SIZE;  // i

    srand(time(NULL));
    temp2=rand()%SIZE;  // j



    for(i=0;i<temp1;i++)
    {
        for(j=temp2;j<SIZE;j++)
        {
            if(Matrix[i][j]==0)
            {
                Matrix[i][j]=1;
                return;
            }
        }
    }

}
void Starting_Random()
{
    int temp1,temp2,add,i,j;
    srand(time(NULL));
    temp1=rand()%SIZE;  // i

    srand(time(NULL));
    temp2=rand()%SIZE;  // j
    Matrix[temp1][temp2]=1;
    Random_creater();
    Display();
}


printer(char s[])
{
    int i=0,k;
    while(s[i]!='\0')
    {
        printf("%c",s[i]);
        for(k=0;k<65535;k++);
        for(k=0;k<65535;k++);
        i++;
    }
}
void record()
{
    char plname[20],cha,c;
    int i,j;
    FILE *info;
    info=fopen("record.txt","a+");
    getch();
    system("cls");
    printf("Enter your name\n");
    scanf("%s",plname);
 
    fprintf(info,"Player Name :%s\n",plname);
    //for date and time

time_t mytime;
  mytime = time(NULL);
  fprintf(info,"Played Date:%s",ctime(&mytime));
  fprintf(info,"Score:%d\n",score);//call score to display score
   
    for(i=0;i<=50;i++)
    fprintf(info,"%c",'_');
    fprintf(info,"\n");
    fclose(info);
    printf("want to see past records press 'y'\n");
    cha=getch();
    system("cls");
    if(cha=='y'){
    info=fopen("record.txt","r");
    do{
        putchar(c=getc(info));
        }while(c!=EOF);}
      fclose(info);
      printf("\n\n\n\t\t\t\tPRESS ANY KEY TO EXIT\n");
      while(!kbhit());
}
void ending()
{
    /*here print prev high score and current score from file */
    printf("\n\n\t\t\t\tYOUR SCORE is  : %d",score);
    record();
}


int Temp_counter()
{
    /* IT SHOULD FIND MAX VALUE FROM WHOLE MATRIX  */
    int temp=0,i,j;

    for(i=0;i<SIZE;i++)
    {
        for(j=0;j<SIZE;j++)
        {
            if(Matrix[i][j]==2584)
                return(1);
            if(Matrix[i][j]==0)
                temp=1;
        }
    }
    if(temp==1)
    {
        last=0;
        return(-99);
    }
    else
    {
        if(last==1)
        {
            return(0);
        }
        last=1;
        return(-99);
    }
}

void Down()
{
    int i,j;
    for(j=0;j<SIZE;j++)
    {
        i=2;
        while(1)
        {
            while(Matrix[i][j]==0)
            {
                if(i==-1)
                    break;
                i--;
            }
            if(i==-1)
                break;
            while(i<SIZE-1 && (Matrix[i+1][j]==0 || (Matrix[i][j] == 1 && Matrix[i+1][j]==1) ||((Matrix[i][j] != Matrix[i+1][j]) && !(abs(Matrix[i][j]-Matrix[i+1][j]) > min(Matrix[i][j],Matrix[i+1][j])))))
            {
                if(Matrix[i+1][j] != 0)
score = score +Matrix[i+1][j]+Matrix[i][j];
Matrix[i+1][j]+=Matrix[i][j];
             
                Matrix[i][j]=0;
                i++;
            }
            i--;
        }
    }
}
void Up()
{
    int i,j;
    for(j=0;j<SIZE;j++)
    {
        i=1;
        while(1)
        {
            while(Matrix[i][j]==0)
            {
                if(i==SIZE)
                    break;
                i++;
            }
            if(i==SIZE)
                break;
            while(i>0 && (Matrix[i-1][j]==0 || (Matrix[i][j] == 1 && Matrix[i-1][j]==1) ||((Matrix[i][j] != Matrix[i-1][j]) && !(abs(Matrix[i][j]-Matrix[i-1][j]) > min(Matrix[i][j],Matrix[i-1][j])))))
            {
                if(Matrix[i-1][j] != 0)
score = score +Matrix[i-1][j]+Matrix[i][j];
Matrix[i-1][j]+=Matrix[i][j];
             
                Matrix[i][j]=0;
                i--;
            }
            i++;
        }
    }
}
void Right()
{
    int i,j;
    for(i=0;i<SIZE;i++)
    {
        j=2;
        while(1)
        {
            while(Matrix[i][j]==0)
            {
                if(j==-1)
                    break;
                j--;
            }
            if(j==-1)
                break;
            while(j<SIZE-1 && (Matrix[i][j+1]==0 || (Matrix[i][j] == 1 && Matrix[i][j+1]==1) ||((Matrix[i][j] != Matrix[i][j+1]) && !(abs(Matrix[i][j]-Matrix[i][j+1]) > min(Matrix[i][j],Matrix[i][j+1])))))
            {
                if(Matrix[i][j+1] != 0)
score = score +Matrix[i][j+1]+Matrix[i][j];
Matrix[i][j+1]+=Matrix[i][j];
             
                Matrix[i][j]=0;
                j++;
            }
            j--;
        }
    }
}
void Left()
{
    int i,j;
    for(i=0;i<SIZE;i++)
    {
        j=1;
        while(1)
        {
            while(Matrix[i][j]==0)
            {
                if(j==SIZE)
                    break;
                j++;
            }
            if(j==SIZE)
                break;
            while(j>0 && (Matrix[i][j-1]==0 || (Matrix[i][j] == 1 && Matrix[i][j-1]==1) ||((Matrix[i][j] != Matrix[i][j-1]) && !(abs(Matrix[i][j]-Matrix[i][j-1]) > min(Matrix[i][j],Matrix[i][j-1])))))
            {
                if(Matrix[i][j-1] != 0)
score = score+Matrix[i][j-1]+Matrix[i][j];
Matrix[i][j-1]+=Matrix[i][j];
             
                Matrix[i][j]=0;
                j--;
            }
            j++;
        }
    }
}

void Action(int Arrow)
{

    switch(Arrow)
    {
        case UP:
            {
                Up();
                break;
            }
        case DOWN:
            {
                /* logic here */
                Down();
                break;
            }
        case LEFT:
            {
                Left();
                break;
            }
        case RIGHT:
            {
                Right();
                break;
            }
        default:
            {
                /*DO NOTHING */
                return;
            }
    }
    count--;
    Random_creater();
    Display();
}




main()
{
    int aro,i,k;
    char Arrow;
    char s[]="THANKS FOR PLAYING, GOOD LUCK FOR NEXT TIME ";
    int temp;
    starting();
    Starting_Random();
    Arrow=DOWN;
    while(Arrow!='\e')  //  HERE IF USER WANT TO EXIT THEN PRESS ESC KEY
    {
        Arrow=getch();
        aro=Arrow;
        Action(Arrow);
        temp=Temp_counter();
        if(temp==1)
        {
            printf("\n\t\t\tYOU WON");
            break;
        }
        if(temp==0 || count < 0)
        {
            printf("\n\t\t\tSORRY !  GAME OVER\n");
            break;
        }
    }
    printf("\n\n\n\t");
    printer(s);
ending();
    system("cls");
 
   for(k=0;k<65535;k++);
   for(k=0;k<65535;k++);
}

Thursday, 17 May 2018

One and Two Dimensional array using Dynamic Memory Allocation



Application's memory is divided into 4 segments - code, static/global, stack and heap

Stack memory for a program running is fixed at compile time by the compiler, os etc..
and some part of stack memory is allocated to main()  called stack frame.
the lifetime of local variables cannot be changed. 
If your program is using more memory than the stack memory then stack overflow occurs as stack will not grow.

If u want more memory at run time and u want to control the lifetime of a variable then use heap memory
Using heap memory is called dynamic memory allocation.

Dynamic memory allocation in C is done using 3 inbuilt functions in stdlib.h
  1. malloc() :  number of elements * size of one element    int *p = malloc(10​*sizeof(int)); does not initialize
  2. calloc() : int *p = calloc(10​,sizeof(int)); sets all values to zeros
  3. realloc() : pointer to existing block , size of new block ..int q=realloc(p,20*​sizeof(int));
Deallocation:
free(p);

Q)Declaration 1D array of size 3 using DMA

Solution :
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n=3,i, j;
    int *arr = (int *)malloc(n * sizeof(int *));
    for (i=0; i<n; i++)
         arr[i] = i;
    for (i=0; i<n; i++)
        printf("%d\t",arr[i]);
    return 0;
}

Q) Declaration 2D array of size 5x6 using DMA 

Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int r=5,c=6,i, j, count=1;
    int *arr[r];
    for (i=0; i<r; i++)
        arr[i]= (int *)malloc(c * sizeof(int *));
    for (i=0; i<r; i++)
        for (j=0; j<c; j++)
            arr[i][j] = count++;
    for (i=0; i<r; i++)
    {
        for (j=0; j<c; j++)
            printf("%d\t",arr[i][j]);
        printf("\n");
    }
        
    return 0;
}


Q)Declaration of 2D array by taking row and column number using DMA

Solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int r,c,i, j, count=1;
    scanf("%d%d",&r,&c);
    int *arr[r];
    for (i=0; i<r; i++)
        arr[i]= (int *)malloc(c * sizeof(int *));
    for (i=0; i<r; i++)
        for (j=0; j<c; j++)
            arr[i][j] = count++;
    for (i=0; i<r; i++)
    {
        for (j=0; j<c; j++)
            printf("%d\t",arr[i][j]);
        printf("\n");
    }
        
    return 0;
}

Q) Create an array of size 10 initially and read k value from user increase the array size by k

Solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n=3,i, j,k;
    int *arr = (int *)malloc(n * sizeof(int *));
    for (i=0; i<n; i++)
         arr[i] = i;
    for (i=0; i<n; i++)
        printf("%d\t",arr[i]);
    
    scanf("%d",&k);
    printf("\n allocate extra %d memory\n",k);
    arr = (int *)realloc(arr, sizeof(int)*k);
    
    for(i=n;i<n+k;i++)
        arr[i]=i;
    for(i=0;i<n+k;i++)
        printf("%d\t",arr[i]);
    return 0;
}

Q) Declare an array of size k and read the elements into array and print those elements and free the memory occupied by array

Solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n=3,i, j;
    int *arr = (int *)malloc(n * sizeof(int *));
    for (i=0; i<n; i++)
         arr[i] = i;
    for (i=0; i<n; i++)
        printf("%d\t",arr[i]);
    free(arr);
    
    return 0;
}

Saturday, 14 April 2018

Python code to read words in first column of excel sheet and convert all the words into individual audio files

import tts.sapi
import os
import openpyxl
import time

filename=str(raw_input('enter path to excel file: ')) # enter the complete path of excel file
wb = openpyxl.load_workbook(filename)
ws = wb.get_sheet_by_name('Sheet1') # gets Sheet1 in excel
rowcount=ws.max_row

def say(s):
    voice = tts.sapi.Sapi()
    voice.set_voice("Zira")#u can use the voice of David also
    #you set the rate to say slowly as shown below
    #voice.set_rate(-5)
    filename=str(ws.cell(row=i+1,column=1).value)+'.wav'
    voice.create_recording(filename, s)




if __name__== "__main__":

       for i in range(rowcount):              # loop through entire rows in excel

               mytext = str(ws.cell(row=i+1,column=1).value) # gets the first column data

               say(mytext)

Friday, 30 March 2018

Printing all subsets of given string in C/C++

In C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    vector<string>subst;
    string s;
 
    cin>>s;
    int l=s.length();
    for(int i=0;i<l;i++)
    {
        for(int j=1;j<=l-i;j++) // we take substring of length j from index i
            subst.push_back(s.substr(i,j));
    }
 
    sort(subst.begin(),subst.end());  // to sort substrings lexicographically
 
    for(auto t:subst)   //for displaying subsets
        cout<<t<<endl;
 
    return 0;

}
Input:
dab

Output:
a
ab
b
d
da
dab

In C
#include<stdio.h>
#include<string.h>
int main()
{
  char st[100],t[100];
  int i,l,j=0,k;
  scanf("%s",st);
  l=strlen(st);
  for(k=0;k<l;k++)
  {
        for(i=k;i<l;i++)
        {
    t[j++]=st[i];
    t[j]='\0';
    printf("%s\n",t);
        }
        j=0;
    }

    return 0;
}

Input:
abcd

Output:
a
ab
abc
abcd
b
bc
bcd
c
cd
d

Sunday, 25 March 2018

Segment trees range maximum query: Finding maximum element in given range

Segment Tree: 


Program:


#include <iostream>
#include <vector>
using namespace std;
const int M = 100000; // maximum size of input array
vector<long> v(M), tree(M * 4); // allocating size for segment tree and array
void build(long node, long a, long b) // to construct segment tree
{
 if (a > b) 
     return;
 if (a == b) 
 {
  tree[node] = v[a];
  return;
 }
 build(node * 2 + 1, a, (a + b) / 2);
 build(node * 2 + 2, (a + b) / 2 + 1, b);
 if (tree[node * 2 + 1] > tree[node * 2 + 2])
  tree[node] = tree[node * 2 + 1];
 
 else
  tree[node] = tree[node * 2 + 2];
}
void update(long node, long a, long b, long x, long y) // to update segment tree
{
 if (a > b) 
     return;
 if (a == b) {
  v[x] = y;
  tree[node] = y;
 }
 else
 {
  long mid = (a + b) / 2;
  if (x <= mid) update(node * 2 + 1, a, mid, x, y);
  else update(node * 2 + 2, mid + 1, b, x, y);
  if (tree[node * 2 + 1] > tree[node * 2 + 2])
   tree[node]= tree[node * 2 + 1];
  else 
   tree[node] = tree[node * 2 + 2];
 }
}
long query(long node, long a, long b, long l, long r) // to query segment tree 
{
 if (a > b || a > r || b < l)
 {
  long temp;
  temp = -9223372036854775807;
  return temp;
 }
 if (a >= l && b <= r) 
     return tree[node];
 long p1 = query(node * 2 + 1, a, (a + b) / 2, l, r);
 long p2 = query(node * 2 + 2, (a + b) / 2 + 1, b, l, r);
 if (p1 > p2) 
     return p1;
 else 
     return p2;
}
int main() 
{
 long n, q, l, r, i, x, y, ch;
 cin >> n >> q;                  // reading number of elements and number of queries
 for (i = 0; i < n; i++) 
  cin >> v[i];                // reading input array

 build(0, 0, n - 1);

 while (q--) 
 {
  cin >> ch;
  if (ch == 1)
  {
   cin >> x >> y;
   update(0, 0, n - 1, x, y);
  }
  else
  {
   cin >> l >> r;
   cout<<query(0,0,n-1,l,r);
  }
 }
return 0;
}

Input :

6             --number of array elements

3             --number of queries

-2  4  2  -1  7  5             array elements


2 0 3       -maximum element in index between 0 and 3

1 4 2       -update 4th index element to 2

2 0 5       -maximum element in index between 0 and 5


Output:45

=========================================================================

Problem: http://codeforces.com/problemset/problem/339/D

Solution using Segment trees: 

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 131073;
vector<long> v(M);
vector<pair<long, long>> tree(M * 4);
void build(long node, long a, long b) {
 if (a > b) return;
 if (a == b) 
 {
  tree[node].first = v[a];
  tree[node].second=1;
  return;
 }
 build(node * 2 + 1, a, (a + b) / 2);
 build(node * 2 + 2, (a + b) / 2 + 1, b);
 tree[node].second = tree[2*node+1].second+1;
 if (tree[node].second&1)
  tree[node].first = tree[node * 2 + 1].first ^ tree[node * 2 + 2].first;
 else
     tree[node].first = tree[node * 2 + 1].first | tree[node * 2 + 2].first;
}
void update(long node, long a, long b, long x, long y) 
{
 if (a > b)
     return;
 if (a == b)
 {
  tree[node].first = y;
 }
 else 
 {
  long mid = (a + b) / 2;
  if (a<=x && x <= mid) 
      update(node * 2 + 1, a, mid, x, y);
  else 
      update(node * 2 + 2, mid + 1, b, x, y);
  if (tree[node].second&1)
      tree[node].first = tree[node * 2 + 1].first ^ tree[node * 2 + 2].first;
     else
         tree[node].first = tree[node * 2 + 1].first | tree[node * 2 + 2].first;
 }
}

int main() 
{
 long n, q, l, r, i, x, y, ch;
 cin >> n >> q;
 n=pow(2,n);
 for (i = 0; i < n; i++) 
  cin >> v[i];
 build(0, 0, n - 1);
 
 while (q--) 
 {
  cin >> x >> y;
  update(0, 0, n - 1, x - 1, y);
  cout<<tree[0].first<<endl;
 
 }
return 0;
}
service lane problem in hacker rank