0% found this document useful (0 votes)
2 views

Assignment-5. py

The document contains a series of Python function assignments that demonstrate string manipulation techniques. Functions include reversing a string, counting consonants, removing vowels, converting to uppercase, removing special characters, checking for anagrams, compressing strings, applying a Caesar cipher, removing duplicates, and validating email addresses. Each function is accompanied by sample input and output for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment-5. py

The document contains a series of Python function assignments that demonstrate string manipulation techniques. Functions include reversing a string, counting consonants, removing vowels, converting to uppercase, removing special characters, checking for anagrams, compressing strings, applying a Caesar cipher, removing duplicates, and validating email addresses. Each function is accompanied by sample input and output for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment-5

##1. Write a function reverse_string(s) that takes a string s


##as input and returns the reversed string.
##
##Sample Input: "hello"
##Sample Output: "olleh"
##
##def reverse_string(s):
## rev=""
## for i in s:
## rev=i+rev
## return rev
##s=input("Enter a String")
##r= reverse_string(s)
##print(f"Reverse is {r}")

##2. Write a function count_consonants(s) that takes a string s


##as input and returns the number of consonants in the string.
##
##Sample Input: "hello world"
##Sample Output: 7
##
##def count_consonants(s):
## con="bcdfghjklmnpqrstvwxyz"
## s=s.lower()
## c=0
## for i in con:
## if i in s:
## c=c+1
## return c
##s=input("Enter a String")
##r= count_consonants(s)
##print(f"Number of consonants = {r}")
##3. Write a function remove_vowels(s) that takes a string s as
##input and returns the string with all vowels removed.
##
##Sample Input: "hello world"
##Sample Output: "hll wrld"
##
##def remove_vowels(s):
## vow="aeiouAEIOU"
## res=""
## for i in s:
## if i not in vow:
## res=res+i
## return res
##s=input("Enter a String")
##r= remove_vowels(s)
##print(f"After all vowels removed = {r}")
##4. Write a function convert_to_uppercase(s) that takes a string s
##as input and returns the string in uppercase.
##
##Sample Input: "hello world"
##Sample Output: "HELLO WORLD"
##
##def convert_to_uppercase(s):
## res=""
## for i in s:
## if i >= "a" and i <="z":
## res=res+chr(ord(i)-32)
## else:
## res=res+i
## return res
##s=input("Enter a String")
##r= convert_to_uppercase(s)
##print(f"In Upper case = {r}")

##5. Write a function remove_special_characters(s) that takes a


##string s as input and returns the string with all special
##characters removed.
##
##Sample Input: "hello! world?"
##Sample Output: "hello world"
##
##def remove_special_characters(s):
## res=""
## for i in s:
## if (i >= "a" and i <="z") or (i >= "A" and i <="Z") or (i >= "0" and i <="9"):
## res=res+i
## return res
##s=input("Enter a String")
##r= remove_special_characters(s)
##print(f"Output = {r}")
##6. Write a function is_anagram(s1, s2) that takes two strings s1
##and s2 as input and returns True if the strings are anagrams,
##and False otherwise.
##
##Sample Input: "Listen", "Silent"
##Sample Output: True
##
##def is_anagram(s1, s2):
## s1 = s1.lower()
## s2 = s2.lower()
## if sorted(s1)==sorted(s2):
## return True
## else:
## return False
##
##print(is_anagram("Listen", "Silent"))

##7. Write a function compress_string(s) that takes a string s as


##input and returns the compressed string. The compression should
##be done by replacing consecutive repeated characters with a
##single character.
##
##Sample Input: "aaabbbccc"
##Sample Output: "abc"
##

##def compress_string(s):
## cs = ""+s[0]
## for i in s:
## if i !=cs[-1]:
## cs=cs+i
## return cs
##
##s=input("Enter a String")
##r= compress_string(s)
##print(f"Compressed String = {r}")

##8. Write a function caesar_cipher(text, shift) that takes a


##string text and an integer shift as input and returns the
##encrypted text using the Caesar cipher algorithm.
##
##Sample Input: "Hello", 3
##Sample Output: "Khoor"
##

##def caesar_cipher(text, shift):


## et = ""
## for i in text:
## if i >= "A" and i <="Z":
## c = chr((ord(i) - 65 + shift) % 26 + 65)
## et=et+c
##
## elif i >= "a" and i <="z":
## c = chr((ord(i) - 97 + shift) % 26 + 97)
## et=et+c
## else:
## et=et+c
##
## return et
##t=input("Enter the message")
##s=int(input("Enter the shift"))
##r= caesar_cipher(t, s)
##print(f"Encripted text = {r}")

##9. Write a function remove_duplicates(s) that takes a string s as


##input and returns the string with all duplicate characters removed.
##
##Sample Input: "hello"
##Sample Output: "helo"
##

##def remove_duplicates(s):
## rd = ""
## for i in s:
## if i not in rd:
## rd=rd+i
## return rd
##
##s=input("Enter a String")
##r= remove_duplicates(s)
##print(f"Output String = {r}")
##10. Write a function validate_email(email) that takes a string email
##as input and returns True if the email is valid, False otherwise.
##
##Sample Input: "example@example.com"
##Sample Output: True

##def validate_email(email):
##
## if email.count('@') != 1:
## return False
##
## local_part, domain = email.split('@')
##
## for char in local_part:
## if not (char.isalnum() or char in ['_', '.']):
## return False
## for char in domain:
## if not (char.isalnum() or char in ['-', '.']):
## return False
##
## if '.' not in domain:
## return False
##
## return True
##print(validate_email("example@example.com"))
##print(validate_email("dibyadasadhikary@soa.ac.in"))

You might also like