String manipulation

 # String manipulation


"""

Implementing programs using Strings. (reverse, palindrome, character count, replacing characters,

remove characters)

"""


def fnReverse(s):

return s[::-1]


def fnPalindrome(s):

if(s==s[::-1]):

return "String is a palindrome"

else:

return "String is not a palindrome"


def fnCharcount(s):

charcount = 0

for ch in s:

if(ch.isalpha()):

charcount+=1

return charcount


def fnWordCount(s):

L=s.split()

return len(L)

def fnReplace(s,ss,rs):

if(ss in s):

return s.replace(ss,rs)

else:

return "Search string not found. Cannot replace"


def fnRemove(s, rs):

if(rs in s):

return s.replace(rs,"")

else:

return "String not found. Cannot remove" 



S = input("Enter string : ")

print("Reverse of the string : ",fnReverse(S))

print("Palindrome check : ")

print(fnPalindrome(S))

print("Character count =",fnCharcount(S))

print("Word count =",fnWordCount(S))

searchstr = input("Enter search string : ")

replacestr = input("Enter string to replace : ")


print("Replaced string :")

print(fnReplace(S,searchstr,replacestr))



removestr = input("Enter string to remove : ")


print("Remove string :")

print(fnRemove(S,removestr))


"""

Sample output

>python 7_stringmanip.py

Enter string : HELLO IT

Reverse of the string :  TI OLLEH

Palindrome check :

String is not a palindrome

Character count = 7

Word count = 2

Enter search string : IT

Enter string to replace : CSE

Replaced string :

HELLO CSE

Enter string to remove : IT

Remove string :

HELLO

"""

Electricity bill calculation

 

# Electricity bill calculation

"""

Tariff rates in TN

Scheme

Unit

Per unit(₹)

0 to 100

0-100

0

0 to 200

0-100

0

101-200

1.5

0 to 500

0-100

0

101-200

2

201-500

3

> 500

0-100

0

101-200

3.5

201-500

4.6

>500

6.6

 

"""

 

Unit = int(input("Enter number of units : "))

L = [[0],[0,1.5],[0,2,3],[0,3.5,4.6,6.6]]

Bill = 0

 

if(Unit>=0 and Unit<=100):

          Bill = 0

elif(Unit<=200):

          Bill = (Unit-100)*L[1][1]

elif(Unit<=500):

          Bill = 100*L[2][1]+(Unit-200)*L[2][2]

else:

          Bill = 100*L[3][1]+300*L[3][2]+(Unit-500)*L[3][3]

 

print("Unit :", Unit)

print("Bill : Rs.", Bill)

 

"""

Sample output

>python EBBill.py

Enter number of units : 510

Unit : 510

Bill : Rs. 1796.0

"""

 

Largest number in a List

 # Function - largest number in a list


def fnMax(L):

return max(L)


L1 = eval(input("Enter a List of numbers : "))

print("Largest number in given List = ", fnMax(L1))