Thursday, 6 June 2019

Extern variables and Functions example in C

Save the below code as ext1.c program

#include <stdio.h>
#include "ext2.c"
/* By using 'extern', you are telling the compiler that whatever
follows it will be found (non-static) at link time; don't reserve
anything for it in the current pass since it will be encountered later.
 Functions and variables are treated equally in this regard.*/
extern void fun(void);
extern int a;
int main(void)
{
    fun();
    printf("%d",a);
  return 0;
}


Save the below code as ext2.c program
#include <stdio.h>
int a=10;
void fun(void)
{
      printf("Hello, world!\n");
}


When you compile and run ext1.c , we get the below output

decimal to binary and alternatively merging binary numbers and then converting merged binary back to decimal python3

st=input();
l1=list(map(int,st))

print(l1)
ans=[]
for x in l1:
    result=''
    c=0
    while x != 0:
        r = x % 2  # gives the exact remainder
        x = x // 2
        result = str(r) + result
        c=c+1
    while(c<4):
        result='0'+result
        c=c+1
    ans.append(result)
f=''
for j in range(4):
    for i in range(len(ans)):
        f=f+ans[i][j]
print(int(f,2))


Input:
23

output:
[2, 3]
['0010', '0011']
00001101
13

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
=======================================================================