Codemind Python Strings

BATHULA PRAVEEN (BP)
0

👇👇CHANGE NEWEST TO OLD IN CODEMIND ðŸ‘‡ðŸ‘‡





CAMEL CASE WORD COUNT

a=input()

count=0

for i in a:

    if ord(i)<91:

        count+=1

if ord(a[0])>94:

    count+=1

print(count)

COUNT NO. OF VOWELS ,CONSONANTS ,DIGITS AND SPACES

a=list(input())

vowel=const=space=digit=0

for i in a:

    if i in ['a','e','i','o','u','A','E','I','O','U']:

        vowel+=1

    elif i in ['1','2','3','4','5','6','7','8','9','0']:

        digit+=1

    elif i in ' ':

        space+=1

    else:

        const+=1

print('Vowels:',vowel)

print('Consonants:',const)

print('Digits:',digit)

print('White spaces:',space)

COUNT NO.OF WORDS

a=input()

print(a.count(' ')+1)

COUNTING THE OCCURANCES

a=input()

b=input()

c=a.count(b)

if c:

    print(c)

else:

    print(-1)


s=input()
c=0
c1=0
v='aeiou'
for i in range(len(s)):
    if s[i]=='a' or s[i]=='e' or s[i]=='o' or s[i]=='u' or s[i]=='i':
        c+=1
    else:
        if c1<c:
            c1=c
        c=0
if c1<c:
    c1=c
print(c1)

NUMBERS IN STRING

a=input()

count=0

for i in a:

    if i in '1234567890':

        count+=int(i)

print(count)

MAX OF STRING

a=input()

print(max(a))

STRING CONCAT

a=input()

b=input()

c=a+b

print(''.join(sorted(c)))

STRING CONTAIN DIGIT OR NOT

a=input()

count=0

for i in a:

    if i.isdigit():

        count+=1

if(count):

    print('Contains',count,'digit')

else:

    print('Doesn\'t contain digit')

MONK TEACHES PALINDROME

t=int(input())

for i in range(t):

    s=input()

    if s!=s[::-1]:

        print('NO')

    elif(len(s)%2==0):

        print('YES EVEN')

    else:

        print('YES ODD')

STRING CONTAINS DIGITS OR NOT 

t=int(input())

for i in range(t):

    a=input()

    for j in a:

        if j.isdigit():

            print('Yes')

            break

    else:

        print('No')

TO LOWER

a=input()

print(a.lower()

ROBOT RETURN TO ORIGIN

a=input()

x=y=0

for i in a:

    if i is 'L':

        x-=1

    elif i is 'R':

        x+=1

    elif i is 'U':

        y+=1

    else:

        y-=1

print(x==0 and y==0)

REVERSE WORDS IN A STRING III

l=input().split(' ')

l2=[]

for i in l:

    l2.append(i[::-1])

print(' '.join(l2))


s=input()
c=0
c1=0
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        if c1<c:
            c1=c
        c=0
if c1<c:
    c1=c
print(c1+1)


class Solution(object):
   def freqAlphabets(self, s):
      m = {}
      x = 'a'
      for i in range(1, 27):
         m[str(i)] = x
         x = chr(ord(x) + 1)
      ans = ""
      m['']=''
      i = len(s) - 1
      while i >= 0:
         if s[i] == "#":
            temp = ""
            for j in range(i - 2, i):
               temp += s[j]
            ans = m[str(temp)] + ans
            i -= 3
         else:
            ans = m[s[i]] + ans
            i -= 1
      return ans
ob1 = Solution()
print(ob1.freqAlphabets(input()))


s=input()
b=0
a=0
l=0
o=0
n=0
for i in s:
    if i=='b':
        b+=1
    if i=='a':
        a+=1
    if i=='l':
        l+=1
    if i=='o':
        o+=1
    if i=='n':
        n+=1
san=0
while b>0 and a>0 and l>0 and o>0 and n>0:
    b-=1
    a-=1
    l-=2
    if l<0:
        break
    o-=2
    if o<0:
        break
    n-=1
    san+=1
print(san)

REVERSE STRING

a=input()

print(a[::-1])

DEFANGING AN IP ADDRESS

a=input()

print(a.replace('.','[.]'))

GOAT LATIN

a=list(map(list,input().split()))

for i in range(len(a)):

    if a[i][0] in ['a','e','i','o','u','A','E','I','O','U']:

        a[i].append('ma')

    else:

        t=a[i][0]

        a[i].pop(0)

        a[i].append(t)

        a[i].append('ma')

    a[i].append('a'*(i+1))

for i in range(len(a)):

    b="".join(a[i])

    print(b,end=" ")


def value(r):
    if (r=='I'):
        return 1
    if (r=='V'):
        return 5
    if (r=='X'):
        return 10
    if (r=='L'):
        return 50
    if (r=='C'):
        return 100
    if (r=='D'):
        return 500
    if (r=='M'):
        return 1000
    return -1
 
def roman(str):
    res=0
    i=0
    while(i<len(str)):
        s1=value(str[i])
        if (i+1<len(str)):
            s2=value(str[i+1])
            if (s1>=s2):
                res=res+s1
                i=i+1
            else:
                res=res+s2-s1
                i=i+2
        else:
            res=res+s1
            i=i+1
    return res
n=input()
print(roman(n))

RANSOM NOTE

def num(x,y):

    for i in x:

        if i in y:

            y.remove(i)

        else:

            return False

    return True

x,y=map(list,input().split())

print(num(x,y))

FIRST UNIQUE CHARACTER IN A STRING

a=input()

for i in a:

    j=a.index(i)

    if i not in a[j+1:]:

        print(j)

        break

else:

    print(-1)


s=input()
n=len(s)
if s[0]=='(' and s[n-1]==')':
    print('True')
else:
    print('False')

MULTIPLY STRINGS

a,b=map(int,input().split())

print(a*b)


s=input()
i=0
j=len(s)
for k in s:
    if k=='I':
        print(i,end=' ')
        i+=1
    if k=='D':
        print(j,end=' ')
        j-=1
print(i)

HELLO

a=input()

print('Hello Technicalhub')

print(a)

EXACT SLICE OF STRING 

a=input()

b=int(input())

c=int(input())

print(a[b:c+1])

STRING DECIMAL

t=int(input())

for i in range(t):

    a=input()

    print(a.isnumeric())

n=int(input())
for k in range(n):
    str1, str2 = input(),input()
    if (len(str1)==len(str2)):
        for i in range(len(str1)):
            if (str1[i]>str2[i]):
                print('NO')
                break
        else:
                print('YES')
    else:
          print('NO')

STRING REVERSE

a=input().split()

print(' '.join(a[::-1]))


ZOO

from collections import Counter

a=list(input())

b=dict(Counter(a))

x=b.get('z')

y=b.get('o')

if 2*x==y:

    print('Yes')

else:

    print('No')

FIRST NON REPEATED CHARACTER

t=int(input())

for i in range(t):

    a=int(input())

    s=input()

    for i in s:

        if i not in s[s.index(i)+1:]:

            print(i)

            break

    else:

        print(-1)


x=int(input())
for i in range(x):
    s=input()
    n=len(s)
    c=0
    for j in range(len(s)-1):
        if s[j]==s[j+1]:
            c+=1
    print(c)

PROGRAM TO FIND SUM OF ALL NUMBERS PRESENT IN THE STRING 

a=input()

sum=0

for i in a:

    if i in '123456789':

        sum+=int(i)

print(sum)

s=input()
c=0
c1=0
v='aeiou'
for i in s:
    if i in v:
        c+=1
    if i not in v:
        if c>c1:
            c1=c
        c=0
if c>c1:
    c1=c
print(c1)

PRINT ALL PERMUTATIONS OF STRING 

from itertools import permutations as p

a=input()

l=p(a)

for i in l:

    print(''.join(i))

import math
s=str(input())
print(eval(s))

n=int(input())
s=input()
a=0
i=0
while i<n-1:
    a+=abs(int(s[i+1])-int(s[i]))
    i+=1
a=n-a-1
if(a%3==0):
    print('Sudhir')
else:
    print('Ashok')


s=input()
l=list(s)
alp=[]
spe=[]
spei=[]
for i in range(len(l)):
    if l[i].isalpha():
        alp.append(l[i])
    else:
        spe.append(l[i])
        spei.append(i)
rev=alp[::-1]
for i in range((len(spei))):
    rev.insert(spei[i],spe[i])
print(*rev,sep='')


from itertools import *
x = input()
b=[]
e=[]
c=0
for i in x:
    if i.isdigit():
        b.append(int(i))
        c=1
b = sorted(set(b),key=b.index)
b = sorted(b,reverse=True)
for i in b:
    if i%2==0:
        e.append(i)
if c==0:
    print('-1')
elif e==[]:
    print('-1')
else:
    l = min(e)
    for i in range(len(b)):
        if b[i]==l:
            m=i
    b[len(b)-1],b[m]=b[m],b[len(b)-1]
    for i in b:
        print(i,end="")


s=input()
a1=list(s.split(","))
for i in a1:
    a2=list(i.split(":"))
    n1=a2[0]
    n2=a2[1]
    m='0'
    for i in n2:
        if ord(i)>ord(m):
            if ord(i)-48<=len(n1):
                m=i
    ind=ord(m)-48
    name10=n1*10
    if m=='0':
        print("X",end="")
    else:
        print(name10[ind-1],end="")


s=input()
l=[]
f=1
for i in range(len(s)):
    if s[i] in '({[':
        l.append(s[i])
        f=1
    else:
        if len(l)>0:
            if l[-1]=='(' and s[i]==')':
                l.pop()
            elif l[-1]=='{' and s[i]=='}':
                l.pop()
            elif l[-1]=='[' and s[i]==']':
                l.pop()
            else:
                print(i+1)
                f=0
                break
        else:
            print(i+1)
            f=0
            break
if f==1:
    if(len(l)==0):
        print(0)
    else:
        print(len(s)+1)

s=input()
n=int(input())
le=len(s)
c1,c3=0,0
for i in s:
    if i=='a':
        c1+=1
l=n//le
k=n%le
c3=l*c1
for i in range(k):
    if s[i]=='a':
        c3+=1
print(c3)

Post a Comment

0Comments

Post a Comment (0)