0% found this document useful (0 votes)
19 views13 pages

Python Assignments Overview and Code

Uploaded by

Hari Maheta
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)
19 views13 pages

Python Assignments Overview and Code

Uploaded by

Hari Maheta
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

Name : Maheta Harilal Prabodhbhai

Div: E

Semester: 8th

Enrollment : 150200107065

Assignment 1

import math

import sys

def divSum(n) :

result = 0

for index in range(2, int([Link](n)) + 1) :

if (n % index == 0) :

if (index == int(n / index)) :

result = result + index

else :

result = result +(index + int(n / index))

return (result + 1)

def areAmicable(x, y) :

if (divSum(x) != y) :

150200107065 Page 1
return False

return (divSum(y) == x)

if((len([Link])) < 3 or (len([Link])) > 3):

print('Missing required inputs')

[Link]()

number1 = int([Link][1])

number2 = int([Link][2])

if (areAmicable(number1,number2)) :

print ('Numbers are Amicable.')

else :

print ('Numbers are not Amicable.')

OUTPUT

150200107065 Page 2
ASSIGNMENT 2

import sys

class Manager:

def __init__(self,accountNumber=0,BranchName=None,Balance=0):

[Link] = accountNumber

[Link] = BranchName

[Link] = Balance

def readFromFile(self,file):

list = []

f1 = open(file,'r')

Lines = [Link]()

[Link]()

for line in Lines:

row = [Link]().split(' ')

object = Manager(int(row[0]),row[1],float(row[2]))

[Link](object)

return list

def getDataBranchWise(self,list,branchName):

totalAccounts = 0

totalBalance = 0.0

higheshBalance = lowestBalance = list[0].Balance

isBranchPresent = False

150200107065 Page 3
for object in list:

if([Link]() == [Link]()):

isBranchPresent = True

totalAccounts += 1

totalBalance += [Link]

if([Link] > higheshBalance):

higheshBalance = [Link]

if([Link] < lowestBalance):

lowestBalance = [Link]

if(not isBranchPresent):

print('No Such Branch Found')

return

averageBalance = (totalBalance/totalAccounts)

output ="\nTotal Accounts : {0}\n".format(totalAccounts)

output +="Average Balance : {0} Rs\n".format(averageBalance)

output +="Highest Balance : {0} Rs\n".format(higheshBalance)

output +="Lowest Balance : {0} Rs".format(lowestBalance)

print(output)

150200107065 Page 4
if(len([Link]) > 2 or len([Link]) < 2):

print('Missing required inputs')

[Link]()

p1 = Manager()

output = [Link]('[Link]')

[Link](output,[Link][1])

[Link]

101 Rajkot 2000.00

102 Rajkot 5000.52

103 Surat 5000.62

104 Ahmedabad 8000.14

105 Jamnagar 9000.62

106 Surat 8000.00

107 Ahmedabad 8000.00

108 Jamnagar 9000.00

109 Surat 8000.00

110 Ahmedabad 8000.00

111 Jamnagar 9000.00

112 Ahmedabad 8000.00

113 Jamnagar 9000.00

114 Surat 1000.00

OUTPUT

150200107065 Page 5
150200107065 Page 6
ASSIGNMENT : 3

import sys
if len([Link])==1:
print("Missing required input")
quit()

K = int([Link][1])

listofnumbers1 = list()
for i in range(2,len([Link])):
[Link](int([Link][i]))

[Link]()
middle = int(len(listofnumbers1)/2)
median=0

if (len(listofnumbers1)%2)!=0:
#If total numbers are odd
median = listofnumbers1[middle]
else:
#If total numbers are even
median = ((listofnumbers1[middle]+listofnumbers1[middle-1])/2)

distancefromMedian = dict()
for num in listofnumbers1:
if num>median:
#Finding distance
distancefromMedian[num] = num-median
else:
#Finding distance
distancefromMedian[num] = median-num

sortedValues = sorted([Link]())
distancefromMedian1 = dict()
for i in range(0,len(sortedValues)):
for key in distancefromMedian:
#Matching sorted values with the keys and sorting the keys according to the distance
from median
if distancefromMedian[key]==sortedValues[i]:
distancefromMedian1[key] = sortedValues[i]

listofnumbers = list()
[Link]()
for key in distancefromMedian1:
#Storing sorted keys in the list

150200107065 Page 7
[Link](key)

for i in range(0,K):
#Fetching first K numbers or fetching K numbers that are nearest to the median
[Link](listofnumbers[i])

[Link]()
print(listofnumbers1)

OUTPUT

150200107065 Page 8
ASSIGNMENT 4

import sys

import re

userString = ([Link][1])

if not [Link]('^[a-zA-Z0-9]+$',userString):

print('String must have only alphabets and digits.')

[Link]()

stringLength = len(userString)

number = ''

output = ''

list = []

for item in range(stringLength):

currentInput = userString[item]

if [Link]():

[Link](currentInput)

number = ''

else:

150200107065 Page 9
number += str(currentInput)

if (item+1) != stringLength and userString[item+1].isalpha():

[Link](int(number))

if number != '':

[Link](int(number))

previousInput = ''

newList = []

for currentInput in list:

if isinstance(currentInput,str) and isinstance(previousInput,str) and previousInput !=


'':

[Link](1)

[Link](currentInput)

else:

[Link](currentInput)

previousInput = currentInput

[Link](1)

for item in range(len(newList)):

if isinstance(newList[item],str):

if (item+1) != len(newList):

output += (newList[item] * newList[item+1])

print(output)

150200107065 Page 10
OUTPUT

150200107065 Page 11
ASSIGNMENT:5

import sys

def function(str, k):

list = []

for i in range(len(str)):

if i %k == 0:

lst = []

sub = str[i:i+k]

for j in sub:

[Link](j)

[Link](''.join(lst))

return list

K = int([Link][1])

userString = '*'.join([Link][2:])

numberOfExtraCharacter = (K - (len(userString) % K))

if(numberOfExtraCharacter != K):

paddingCharacter = "$" * numberOfExtraCharacter

userString = userString + paddingCharacter

output = function(userString,K)

150200107065 Page 12
string = ''

for index in range(K):

temp = ''

for item in output:

temp += item[index]

string += temp[::-1]

print(string)

OUTPUT

150200107065 Page 13

You might also like