Skip to main content

Some Basic Python Programs

 

Write a Python script for finding the index of unique values in a string


Example

Input			output

alphabete		[1,2,3,5,7] or 12357

tetessyaay		-1
------------------------------------------------------------------------

Output:

s=input().strip()
f=0
l=[]
d=dict()
for i in s:
    d[i]=d.get(i,0)+1
for i in range(len(s)):
    if(d[s[i]]==1):
        l.append(i+1)
        f=1

if(f==0):
    print("-1")
else:
    print(*l)

In an even word, each letter occurs an even number of times.
Write a python function solution that, given a string consisting of N characters, returns the
minimum of letters that must be deleted to obtain an even word.
Examples:
1.Given S = ‘acbcbba’,the function should return 1(one letter b must be deleted)
2. Given S = ‘axxaxa’, the function should return 2 (one letter a and one letter x must be
deleted)
3.Given S = ‘aaaa’, your function should return 0(no need to delete any letters).
Write an efficient algorithm for the following assumptions:
● N is an integer within the range [0..200,000];
● String S consists only of lowercase letters (a-z)

-------------------------------------------------------------------------

def fun(d):
    c=0
    for i in d:
        if d[i]%2!=0:
            c+=1
    return c

s=input().strip()

f=0
l=[]
d=dict()
for i in s:
    d[i]=d.get(i,0)+1
c=fun(d)
print(c)

You are given a string consisting of lowercase letters of the english alphabet.
 You must split this string into a minimal number of substrings in such a way that no letter occurs
 more than once in each substring 
For example, here are some correct split of the string "abacdec" : ('a', 'bac', 'dec'), ('a', 'bacd', 'ec') 
and ('ab', 'ac', 'dec').
 

------------------------------------------------------------------------------

s=input().strip()
i=0
le=len(s)
final_str=""
l=[]
while(i<le):
    if(s[i] in final_str):
        l.append(final_str)
        final_str=""
    final_str+=s[i]
    if(i==le-1):
        l.append(final_str)
    i=i+1
print(tuple(l))




Comments