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

No comments:

Post a Comment