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

Pythonassi 2

The document contains 10 programming assignments. Assignment 2 asks to write a recursive program to calculate the sum of a list of numbers. Assignment 3 asks to write a recursive program to calculate the sum of positive integers from n to n-x. Assignment 4 asks to write a program to check if a string is a palindrome or symmetrical.

Uploaded by

PRATIKSHA BHOYAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Pythonassi 2

The document contains 10 programming assignments. Assignment 2 asks to write a recursive program to calculate the sum of a list of numbers. Assignment 3 asks to write a recursive program to calculate the sum of positive integers from n to n-x. Assignment 4 asks to write a program to check if a string is a palindrome or symmetrical.

Uploaded by

PRATIKSHA BHOYAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Assignment No 2

Roll No: MC22F14F010


8.write a program to find most repeated words in text file.
count=0

word=0

maxcount=0

words=[]

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

for line in file:

string=line.lower().replace("","").replace("","").split(" ")

for s in string:

words.append(s)

for i in range(0,len(words)):

count=1

for j in range(i+1,len(words)):

if(words[i]==words[j]):

count=count+1

if(count>maxcount):

maxcount=count

word=words[i]

print("Most repeated words are:"+word)

file.close()

Output:
2. Write a recursive python program to calculate the sum of a list of a
number.
list=[]
len=int(input("Enter number of items in list:"))
for x in range(len):
temp=int(input("Enter list item:"))
list.append(temp)
def sum(copy_list,index):
if index==0:
return copy_list[index]
return copy_list[index]+sum(copy_list,index-1)
print(sum(list,len-1))

Output:
3.Write arecursive python program to calculate the sum of the positivr integers
of n+(n-2)+(n-4)…(until n-x=0).
def sum(p):
if (p-2)<=0:
return p
else:
return p+sum(p-2)
print(sum(16))
print(sum(18))
print(sum(9))

Output:
4. Write a program to check whether string is palindrome or
symmetric.
def SyMPal(string):
if string==string[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
size=len(string)
if size%2==0:
half=size//2
if string[0:half]==string[half:size]:
print("This is symmetrical")

else:
print("This is not symmetrical")
SyMPal("khokho")

Output:
1.Write a Python function to check whether a number is perfect or not from 1 to
10000.
def perfect(num):
div_sum = sum([i for i in range(1, num) if num % i == 0])
return div_sum == num

for number in range(1, 10001):


if perfect(number):
print(number, "is a perfect number")
Output:
10. Write a python class to find the three elements that sum to zero from a list
of 10 numbers
class Summs:
def __init__(self, numbers):
self.numbers = numbers
def find_zero_sum_triplet(self):
triplets = []
for i in range(len(self.numbers) - 2):
for j in range(i + 1, len(self.numbers) - 1):
for k in range(j + 1, len(self.numbers)):
if self.numbers[i] + self.numbers[j] + self.numbers[k] == 0:
triplets.append((self.numbers[i], self.numbers[j], self.numbers[k]))
return triplets
numbers = [-25,-10,-7,-3,2,4,8,10]
zero_sum_finder = Summs(numbers)
triplets = zero_sum_finder.find_zero_sum_triplet()
print("Triplets with zero sum:")
for triplet in triplets:
print(triplet)
Output:
9.Write a python program to overload minus operator which takes a list
and return a new list with unique elements of first list.
class Lists:
def __init__(self, data):
self.data = data
def __invert__(self):
unique_first_letters = []
for item in self.data:
first_letter = item[0]
if first_letter not in unique_first_letters:
unique_first_letters.append(first_letter)
return unique_first_letters
plist = ['Pratiksha', 'Suyash', 'Pranoti', 'Asmita', 'Pratik', 'Vaishali']
obj = Lists(plist)
unique_letters = ~obj

print("Unique First Letters:", unique_letters)


Output:
7.Write a python class BankAccount with atttibutes like
account_number,balance, date_of_opening and customername,and method like
withdraw,deposit and check balance.

from datetime import datetime


class BankAccount:
def __init__(self, account_number, balance, date_of_opening,
customer_name):
self.account_no = account_number
self.balance = balance
self.date_of_opening = date_of_opening
self.customer_name = customer_name
def deposit(self, amount):
self.balance += amount
print("Deposit successful. Current balance:", self.balance)
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print("Withdrawal successful. Current balance:", self.balance)
else:
print("Insufficient funds. Current balance:", self.balance)
def check_balance(self):
print("Account balance:", self.balance)
def get_account_info(self):
print("Account Information")
print("Account Number:", self.account_no)
print("Balance:", self.balance)
print("Date of Opening:", self.date_of_opening)
print("Customer Name:", self.customer_name)
account = BankAccount("71625", 90000, datetime.now(), "Pratiksha Bhoyar")
account.get_account_info()
account.check_balance()
account.deposit(500)
account.withdraw(200)
account.check_balance()
Output:

You might also like