Thursday, 6 June 2019

merge two strings with alternate characters in python3

st="sar"
st1="lax"
ans=''.join(map(''.join, zip(st, st1))) 
print(ans)

Finding second largest in a string of numbers

#include <stdio.h>
#include <limits.h>

main()
{
char st[100];
printf("\nEnter a string\n");
scanf("%s",st);
int i,first,second;
first = second =INT_MIN;
for(i=0;st[i]!='\0';i++)
{
if(st[i]-'0' > first)
{
second = first;
first=st[i]-'0';
}
else if(st[i]-'0' > second && st[i]-'0' != first)
second=st[i]-'0';
}
if(second == INT_MIN)
printf("there is no second minimum");
else
printf("second min = %d",second);
}

=========================Using sorting=========================


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int co(const void *p, const void *q)
{
   return *(char *)p-*(char *)q;
}
main()
{
char st[100];
printf("\nEnter a string\n");
int i,max=0,l=strlen(st);
qsort(st,l,sizeof(char),co); // for sorting characters in string
max=st[l-1]-'0';
for(i=l-2;i>=0;i--)
{
if(max != st[i]-'0'){

printf("%d",st[i]-'0');
break;
}
}
}

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

Sunday, 2 June 2019

Regular Expressions in Java



To check it is a valid mobile or not:
A mobile number is valid if the starting digit is either 7 or 8 or 9
and must also contain 10 digits

String st="7123456789"

if(st.matches("[7-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"))
        System.out.println("valid mobile");

(or)
if(st.matches("[7-9][0-9]{9}"))
        System.out.println("valid mobile");
(or)
if(st.matches("[789][0-9]{9}"))
        System.out.println("valid mobile");

if u want to allow 11 digit mobile number then first digit must be zero
if(st.matches("0? [7-9][0-9]{9}"))
        System.out.println("valid mobile");

0? means atmost one zero as first digit 
0 means compulsory one zero as first digit
0+ atleast one 0
0* means  0 may be there or may not be there and 0 can occur any number of times.

if u want to allow 10 or 11 or 12 digit mobile number then 
if(st.matches("(0|91)? [7-9][0-9]{9}"))
        System.out.println("valid mobile");

For email the regular expressions is
[a-z A-Z 0-9]@[a-z A-Z]. [a-z]{2-3}

=========
[^abc]  --means except abc

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];
                }             
}