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

Popular posts from this blog

Is-A and Has-A relationships in python

  In object-oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying "A is a B type of thing". For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example, House is a Building. But Building is not a House. #Is-A relationship --> By Inheritance class  A:    def   __init__ ( self ):      self .b= 10    def   mym1 ( self ):      print ( 'Parent method' ) class  B(A):    def   mym2 ( self ):      print ( 'Child method' ) d = B() d.mym1() #output: Parent method d.mym2() #output: Child method HAS-A Relationship:  Composition(HAS-A) simply mean the use of instance variables that are references to other objects. For example Maruti has Engine, or House has Bathroom. Let’s understand...

Magic Methods in Python

  What Are Dunder Methods ? In Python, special methods are a set of predefined methods you can use to enrich your classes.  They are easy to recognize because they start and end with double underscores, for example  __init__  or  __str__ . Dunder methods let you emulate the behavior of built-in types.  For example, to get the length of a string you can call  len('string') . But an empty class definition doesn’t support this behavior out of the box: These “dunders” or “special methods” in Python are also sometimes called “magic methods.” class NoLenSupport : pass >>> obj = NoLenSupport () >>> len ( obj ) TypeError : "object of type 'NoLenSupport' has no len()" To fix this, you can add a  __len__  dunder method to your class: class LenSupport : def __len__ ( self ): return 42 >>> obj = LenSupport () >>> len ( obj ) 42 Object Initialization:  __init__ "__init __ ...

Inheritance and Types in Python

  Inheritance   Creating a new class from existing class is known as inheritance . The class from which features are inherited is known as base class and the class into which features are derived into is called derived class . Syntax: class  derived- class (base  class ):       < class -suite>      Inheritance promotes reusability of code by reusing already existing classes.  Inheritance is used to implement  is-a  relationship between classes.   Following hierarchy is an example representing inheritance between classes:   Single inheritance   When a derived class inherits only from syntax, the base class is called single inheritance. If it has one base class and one derived class it is called single inheritance.   Diagram     Syntax class  A:  #parent class         #some code       class  b(A):...