Sunday, 2 June 2019

Count the number of occurrences of sub string in a given string,Sort the words in a given sentence, convert binary to decimal number and vice versa.

Count the number of occurrences of sub string in a given string

st=list(x for x in input().split())                                 # converting a sentence into list
sub=input()                                                                # reading a string
print(st.count(sub))

Input:
i like like like like python programming
like

Output:
4
========================================================================
Sort the words in a given sentence

st=list(x for x in input().split())                       # converting a sentence into list
temp=st.copy()                                                # string copy
temp.sort()
temp = ' '.join(temp)                                       #converting list back to sentence
print(temp)

Input:
python is a powerful language

Output:
a is language powerful python
======================================================================== 

Write a program convert binary to decimal number and vice versa.

n = int(input())
print(bin(n).replace('0b',''))

Output:
7
111

n = input("enter a binary number")
print(int(n,2))

Output:
enter a binary number 111
7
========================================================================
Remove the duplicates from the given array.
n = [x for x in input().split()]
d = list(dict.fromkeys(n))
for x in d:
    print(x, end = ' ')

Output:
1 1 1 1 1 2 3 4
1 2 3 4
=======================================================================

Passing a 2D array to a function in C

#include <stdio.h>
void read(int r,int c, int a[][c])
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
void print(int r,int c, int a[][c])
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
main()
{
int r,c;
printf("Enter the number of rows and columns\n");
scanf("%d%d",&r,&c);
int a[r][c],b[r][c],i,j;
printf("enter the array elements\n");
read(r,c,a);
printf("the array elements are\n");
print(r,c,a);
}
============================================================
#include <stdio.h>
void read(int *a,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[c*i+j]);
}
void print(int *a,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",a[c*i+j]);
printf("\n");
}
}
main()
{
int r,c;
printf("Enter the number of rows and columns\n");
scanf("%d%d",&r,&c);
int a[r][c],b[r][c],i,j;
printf("enter the array elements\n");
read(&a[0][0],r,c);
printf("the array elements are\n");
print(&a[0][0],r,c);
}

Output:

Saturday, 1 June 2019

Merge Sort program in C

#include <stdio.h>
#include <stdlib.h>
void msort(int a[],int low ,int high,int n)
{
if(low < high)
{
int mid = (low+high)/2;
msort(a,low,mid,n);
msort(a,mid+1,high,n);
int i=low,j=mid+1,k=low;
int temp[n];
while(i<= mid && j <= high)
{
if(a[i] < a[j])
{
temp[k++] = a[i++];
}
else
{
temp[k++] = a[j++];
}
}
while(i <= mid)
{
temp[k++] = a[i++];
}
while(j<=high)
{
temp[k++] = a[j++];
}
for(i=low;i<=high;i++)
a[i]=temp[i];
}
}
main()
{
int n;
scanf("%d",&n);
int *a,i;
a=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
msort(a,0,n-1,n);
for(i=0;i<n;i++)
printf("%d ",a[i]);

}
================Merge sort on Array of Strings=================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void msort(char a[][100],int low ,int high,int n)
{
if(low < high)
{
int mid = (low+high)/2;
msort(a,low,mid,n);
msort(a,mid+1,high,n);
int i=low,j=mid+1,k=low;
char temp[n][100];
while(i<= mid && j <= high)
{
if(strcmp(a[i], a[j])<0)
{
strcpy(temp[k],a[i]);
k++;
i++;
}
else
{
strcpy(temp[k],a[j]);
k++;
j++;
}
}
while(i <= mid)
{
strcpy(temp[k],a[i]);
k++;
i++;
}
while(j<=high)
{
strcpy(temp[k],a[j]);
k++;
j++;
}
for(i=low;i<=high;i++)
strcpy(a[i],temp[i]);
}
}
main()
{
int n;
scanf("%d",&n);
int i;
char a[n][100];
for(i=0;i<n;i++)
scanf("%s",&a[i]);
msort(a,0,n-1,n);
for(i=0;i<n;i++)
printf("%s ",a[i]);
}
===========Merge Sort Simple Program Below===================================

#include <stdio.h>
#define MAX 100

int c[MAX];

merge(int a[], int ,int ,int , int );
mergesort(int a[], int ,int);

main()
{
                int n,i,a[MAX];
                printf("enter n value");
                scanf("%d",&n);
                for(i=0;i<n;i++)
                                scanf("%d",&a[i]);
                mergesort(a,0,n-1);
                for(i=0;i<n;i++)
                                printf("%d\t",a[i]);
}
mergesort(int a[],int low, int high)
{
                int mid;
                if(low<high)
                {
                                mid=(low+high)/2;
                                mergesort(a,low,mid);
                                mergesort(a, mid+1,high);
                                merge(a,low,mid,mid+1,high);
                }
}

/* merge two subparts obtained by dividing the array into 2 halves (low1 to high1 and low2 to high2)*/
merge(int a[], int low1,int high1,int low2, int high2)
{
                int i,j,k;
                i=low1;
                j=low2;
                k=low1;    /*after merging, lower index of merged array begins with low1,ends with high2*/
                while(i<= high1 && j<=high2)
                {
                                while(a[i]<=a[j] && i <=high1)
                                {
                                                c[k++]=a[i++];
                                }
                                while(a[j]<a[i]&& j <= high2)
                                {
                                                c[k++]=a[j++];
                                }
                }
                while(i<=high1)    /* if i <= high1 means there are elements still in subpart1 (low1 to high1)*/
                {
                                c[k++]=a[i++];  
                }
                while(j<=high2)
                {
                                c[k++]=a[j++];                  
                }
                for(k=low1;k<=high2;k++)  /* the sorted sub array in 'c' is written back to original array 'a'*/
                {
                                a[k]=c[k];
                }             
}

Wednesday, 29 May 2019

Replace Mirror characters in a given string : C program

Given a string and a number N, we need to mirror the characters from N-th position
up to the length of the string in the alphabetical order.
In mirror operation,change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on.

Using the fact that the distance from 'z' to that character and
distance from 'a' to mirror of the character are same.

Program:
#include <stdio.h>
main()
{
char s1[100];
scanf("%[^\n]",s1);
int i,n;
scanf("%d",&n);
for(i=n-1;s1[i]!='\0';i++)
s1[i]='z'-s1[i]+'a';
printf("%s",s1);
}

Output:
section
2
svxgrlm

WAP to Find Highest Frequency Character in a String

#include <stdio.h>
main()
{
char s1[100];
scanf("%[^\n]",s1);
int i,h[26] ={0};
for(i=0;s1[i] != '\0';i++)
h[s1[i] -97 ]++;
int max=0,index;
for(i=0;i<26;i++)
if(h[i]>max)
{
index=i;
max=h[i];
}
printf("%c occured %d times",index+97,max);
}

Monday, 27 May 2019

Array Rotations Questions in C, Arrays: Left Rotation Hacker Rank Solution in C, Circular Array Rotation Hacker Rank Solution in C


Link to Question:
https://www.hackerrank.com/challenges/array-left-rotation/problem
https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem

Program:
#include <stdio.h>

main()

{

 int n,i,d;

 scanf("%d%d",&n,&d);

 int a[n];

 for(i=0;i<n;i++)

  scanf("%d",&a[i]);

  for(i=d;i<d+n;i++)

   printf("%d ",a[i%n]); // we start printing from last index

 }

==============================================================
Circular Array Rotation

Link to Question
https://www.hackerrank.com/challenges/circular-array-rotation/problem

Program
#include <stdio.h>
int main()
{
    int n,i,k,q,j=0;
    scanf("%d%d%d",&n,&k,&q);
    int a[n],b[n];
    for(i=0;i<n;i++)
      scanf("%d",&a[i]);
    //after every n rotations, the rotated array becomes original array
    int r=k%n;
    for(int i=0;i<n;i++)
        b[(i+r)%n]=a[i];
    while(q--)
    {
        int id;
        scanf("%d",&id);
        printf("%d\n",b[id]);

    }
}
You can write the above program without using array b
by using
printf("%d\n",a[(n-(k%n)+m)%n]);
===============================================================================
Just understand the below two programs, you will be able to solve any array rotation problem
output:
5
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
1 2 3 4 5


#include <stdio.h>
main()
{
int n,i,j;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n+1;i++)
{
for(j=i;j<i+n;j++)
printf("%d ",a[j%n]); // we start printing from first index
printf("\n");
}
}

=======================================================================
output:
5
1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
1 2 3 4 5


#include <stdio.h>
main()
{
int n,i,j;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1;i>=0;i--)
{
for(j=i;j<i+n;j++)
printf("%d ",a[j%n]); // we start printing from last index
printf("\n");
}
}