You are on page 1of 12

ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY

(Unit of Alva’s Education Foundation (R), Moodbidri)


Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

Python Assignment Question 2

1. Python program to check whether the string is Symmetrical or


Palindrome.

def palindrome(a):

# finding the mid, start

# and last index of the string

mid = (len(a)-1)//2 #you can remove the -1 or you add <= sign in line
21

start = 0 #so that you can compare the middle elements also.

last = len(a)-1

flag = 0

# A loop till the mid of the

# string

while(start <= mid):

# comparing letters from right

# from the letters from left

if (a[start]== a[last]):

start += 1
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

last -= 1

else:

flag = 1

break;

# Checking the flag variable to

# check if the string is palindrome

# or not

if flag == 0:

print("The entered string is palindrome")

else:

print("The entered string is not palindrome")

# Function to check whether the

# string is symmetrical or not

def symmetry(a):

n = len(a)

flag = 0
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

# Check if the string's length

# is odd or even

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

# Checking the flag variable to

# check if the string is symmetrical


ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

# or not

if flag == 0:

print("The entered string is symmetrical")

else:

print("The entered string is not symmetrical")

# Driver code

string = 'amaama'

palindrome(string)

symmetry(string)

2. Reverse Words in a Given String in Python.

string = "geeks quiz practice code"

# reversing words in a given string

s = string.split()[::-1]

l = []

for i in s:

# appending reversed words to l

l.append(i)

# printing reverse words

print(" ".join(l))
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

3. Python program to convert the given string into Uppercase Half String.

test_str = 'geeksforgeeks'

# printing original string

print("The original string is : " + str(test_str))

# computing half index

hlf_idx = len(test_str) // 2

res = ''

for idx in range(len(test_str)):

# uppercasing later half

if idx >= hlf_idx:

res += test_str[idx].upper()

else:

res += test_str[idx]

# printing result

print("The resultant string : " + str(res))


ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

4. Python program to find Word location in String.

import re

# initializing string

test_str = 'geeksforgeeks is best for geeks'

# printing original string

print("The original string is : " + test_str)

# initializing word

wrd = 'best'

# Word location in String

# Using findall() + index()

test_str = test_str.split()

res = -1

for idx in test_str:

if len(re.findall(wrd, idx)) > 0:

res = test_str.index(idx) + 1

# printing result
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

print("The location of word is : " + str(res))

5. Python program to demonstrate the class and instance variables.

class Dog:

# Class Variable

animal = 'dog'

# The init method or constructor

def __init__(self, breed, color):

# Instance Variable

self.breed = breed

self.color = color

# Objects of Dog class

Rodger = Dog("Pug", "brown")

Buzo = Dog("Bulldog", "black")

print('Rodger details:')
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

print('Rodger is a', Rodger.animal)

print('Breed: ', Rodger.breed)

print('Color: ', Rodger.color)

print('\nBuzo details:')

print('Buzo is a', Buzo.animal)

print('Breed: ', Buzo.breed)

print('Color: ', Buzo.color)

# Class variables can be accessed using class

# name also

print("\nAccessing class variable using class name")

print(Dog.animal)

6. Python program to demonstrate the constructors in class.

class GeekforGeeks:

# default constructor

def __init__(self):

self.geek = "GeekforGeeks"
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

# a method for printing data members

def print_Geek(self):

print(self.geek)

# creating object of the class

obj = GeekforGeeks()

# calling the instance method using the object obj

obj.print_Geek()

7. Python program to demonstrate the read(), write() and open() function

file = open("geeks.txt", "r")

print (file.read(5))

file = open('geek.txt','w')

file.write("This is the write command")

file.write("It allows us to write in a particular file")

file.close()

8. Python to Count the Number of matching characters in a pair of string

def commonfun(str1,str2):
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

return(len((set(str1)).intersection(set(str2))))

#string1=input("Enter String 1 : ")

string1="VISHAV"

string2="VANSHIKA"

#string2=input("Enter String 2 : ")

no_of_common_character=commonfun(string1.lower(),string2.lower())

print("NO. OF COMMON CHRACTERS ARE : ",no_of_common_character)

9. Python program to count Least Frequent Character in String.

test_str = "GeeksforGeeks"

# printing original string

print ("The original string is : " + test_str)

# using naive method to get

# Least Frequent Character in String

all_freq = {}

for i in test_str:
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

if i in all_freq:

all_freq[i] += 1

else:

all_freq[i] = 1

res = min(all_freq, key = all_freq.get)

# printing result

print ("The minimum of all characters in GeeksforGeeks is : " + str(res))

10.Python program to find uncommon words from two Strings.

def UncommonWords(A, B):

# count will contain all the word counts

count = {}

# insert words of string A to hash

for word in A.split():

count[word] = count.get(word, 0) + 1

# insert words of string B to hash

for word in B.split():


ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
(Unit of Alva’s Education Foundation (R), Moodbidri)
Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi. Recognized by Government of
Karnataka.
Accredited by NAAC with A+ Grade
Shobhavana Campus, MIJAR-574225, Moodbidri, D.K., Karnataka
Ph: 08258-262725; Mob:722262724,7026262725,mail:principalaiet08@gmail.com

count[word] = count.get(word, 0) + 1

# return required list of words

return [word for word in count if count[word] == 1]

# Driver Code

A = "Geeks for Geeks"

B = "Learning from Geeks for Geeks"

# Print required answer

print(UncommonWords(A, B))

You might also like