Assignment 2 : Strings and Functions
Lab Assignments
SET A
Strings
1) Write a python program to check whether the string is Symmetrical or Palindrome.
def palindrome(a):
mid = (len(a)-1)//2
start = 0
last = len(a)-1
flag = 0
while(start<mid):
if (a[start]== a[last]):
start += 1
last -= 1
else:
flag = 1
break;
if flag == 0:
print("The entered string is palindrome")
else:
print("The entered string is not palindrome")
def symmetry(a):
n = len(a)
flag = 0
if n%2:
mid = n//2 +1
else:
mid = n//2
start1 = 0
start2 = mid
while(start1 < mid and start2 < n):
if (a[start1]== a[start2]):
start1 = start1 + 1
start2 = start2 + 1
else:
flag = 1
break
if flag == 0:
print("The entered string is symmetrical")
else:
print("The entered string is not symmetrical")
string = 'amaama'
palindrome(string)
symmetry(string)
2) Write a python program to Reverse words in a given String
def rev_words(string):
words = string.split(' ')
rev = ' '.join(reversed(words))
return rev
s= "Python is good"
print ("reverse: ",rev_words(s))
s2= "Hello from Study tonight"
print ("reverse: ",rev_words(s2))
3) Write a python program to remove i’th character from string in different ways
- By using Naive method
- By using replace() function
- By using slice and concatenation
- By using join() and list comprehension
- By using translate() method
1. Removing a Character from String using the Naive method
input_str = "DivasDwivedi"
# Printing original string
print ("Original string: " + input_str)
result_str = ""
for i in range(0, len(input_str)):
if i != 3:
result_str = result_str + input_str[i]
# Printing string after removal
print ("String after removal of i'th character : " + result_str)
2. Removal of Character from a String using replace() Method
str = "Engineering"
print ("Original string: " + str)
res_str = str.replace('e', '')
print ("The string after removal of character: " + res_str)
res_str = str.replace('e', '', 1)
print ("The string after removal of character: " + res_str)
3. Removal of Character from a String using Slicing and Concatenation
str = "Engineering"
print ("Original string: " + str)
res_str = str[:2] + str[3:]
print ("String after removal of character: " + res_str)
4. Removal of Character from a String using join() method and list comprehension
str = "Engineering"
print ("Original string: " + str)
res_str = ''.join([str[i] for i in range(len(str)) if i != 2])
print ("String after removal of character: " + res_str)
5. Removal of character from a string using translate() method
str = 'Engineer123Discipline'
print(str.translate({ord(i): None for i in '123'}))
Functions
1) Write a Python function to find the Max of three numbers.
def max_of_two( x, y ):
if x > y:
return x
return y
def max_of_three( x, y, z ):
return max_of_two( x, max_of_two( y, z ) )
print(max_of_three(3, 6, -5))
2) Write a Python function to sum all the numbers in a list.
def sum(numbers):
total = 0
for x in numbers:
total += x
return total
print(sum((8, 2, 3, 0, 7)))
3) Write a Python program to reverse a string.
def reverse(str):
string = " "
for i in str:
string = i + string
return string
str = "PythonGuides"
print("The original string is:",str)
print("The reverse string is:", reverse(str))
SET B
Strings
1. Write a python program to print even length words in a string
def PrintEvenWords(s):
s=s.split(' ')
for word in s:
if len(word)%2==0:
print(word)
string= " Python is good for me"
PrintEvenWords(string)
2. Write a python program to accept the strings which contains all vowels
#function
def CheckString(s):
s = s.lower()
vowels = set("aeiou")
for char in s:
if char in vowels:
vowels.remove(char)
if len(vowels) == 0:
print("Accepted")
else:
print("Not accepted")
s1= "AeBIdeffoBUw"
print(s1)
CheckString(s1)
s2="python"
print(s2)
CheckString(s2)
3. Write a python program to Count the Number of matching characters in a pair of string
Method 1:-
def cnt(s1, s2):
c=0 #counter variable
j=0
for i in s1:
if s2.find(i)>-0 and j==s1.find(i):
c=c+1
j=j+1
print("Matching char: ",c)
s1="aabcdefk12"
s2="b2acdefk1"
cnt(s1,s2)
Method 2:-
def cnt(s1, s2):
char_of_s1=set(s1)
char_of_s2=set(s2)
same_char= char_of_s1 & char_of_s2
print("Matching Characters: ",len(same_char))
s1="abgfhij2@1"
s2="@1khgabop"
cnt(s1,s2)
Functions
1. Write a Python function that takes a list and returns a new list with unique elements of the first list.
def unique_list(l):
x = []
for a in l:
if a not in x:
x.append(a)
return x
print(unique_list([1,2,3,3,3,3,4,5]))
2. Write a Python function that takes a number as a parameter and check the number is prime or not.
def chk_prime(n):
if (n==1):
return False
elif (n==2):
return True;
else:
for x in range(2,n):
if(n % x==0):
return False
return True
print(chk_prime(9))
3. Write a Python function to check whether a number is perfect or not.
def perfect_no(n):
sum = 0
for x in range(1, n):
if n % x == 0:
sum += x
return sum == n
print(perfect_no(6))
PROGRAMS FOR PRACTICE:
1. Write a Python program to append items from a specified list.
2. Write a python program Check if a Substring is Present in a Given String
3. Write a python program Words Frequency in String Shorthands
4. Write a python program Convert Snake case to Pascal case
5. Write a Python function to calculate the factorial of a number (a non-negative integer). The function
accepts the number as an argument.
6. Write a Python function to check whether a number is in a given range.
7. Write a Python function that accepts a string and calculate the number of upper case letters and lower
case letters.
8. Write a Python program to detect the number of local variables declared in a function.
9. Write a python program to Remove all duplicates from a given string in Python
10. Write a Python function that checks whether a passed string is palindrome or not.
Outside the function : 30
47
11. Write a Python program that accepts a hyphen-separated sequence of words as input and prints the
words in a hyphen-separated sequence after sorting them alphabetically.
Sample Items : green-red-yellow-black-white Expected Result : black-green-red-white-yellow
12. Write a Python function to create and print a list where the values are square of numbers between 1
and 50 (both included).
13. Write a Python program to execute a string containing Python code.
14. Write a Python program to access a function inside a function
No comments:
Post a Comment