Saturday, June 19, 2021

Array Exercises in Python

Question
Write a Python Program to print Maximum and Minimum number of a given array

 
from array import *

temper=array('i',[10,0,30,40,5,15,20,19,20,43])

def findmax(arr):
    mx=arr[0]
    for i in range(1,len(arr)):
        if arr[i]>mx:
            mx=arr[i]
    return mx

def findmin(arr):
    mn=arr[0]
    for i in range(1,len(arr)):
        if arr[i]<mn:
            mn=arr[i]
    return mn

print('Maximum',findmax(temper),sep=":")
print('Minimum',findmin(temper),sep=":")

Question
Input 3 months income and expenses of 5 companies and calculate the profit and print it. .

 
from array import *

#define 2D 
income=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
expense=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
profit=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]

#input 3 months income for 5 companies
for i in range(0,5):
    jan=int(input('A company Jan income?'))
    feb=int(input('A company Feb income?'))
    mar=int(input('A company mar income?'))
    income[i]=[jan,feb,mar]

#input 3 months expenses for 5 companies
for i in range(0,5):
    jan=int(input('A company Jan expenses?'))
    feb=int(input('A company Feb expenses?'))
    mar=int(input('A company mar expenses?'))
    expense[i]=[jan,feb,mar]

#Calculate profit and save in 2D array profit
for i in range(0,5):
    profit[i]=[income[i][0]-expense[i][0],income[i][1]-expense[i][1],
               income[i][2]-expense[i][2]]
    
#to print income details
for r in income:
    for c in r:
       print(c,end = " ")
    print()  

#to print expenses details
for r in expense:
    for c in r:
       print(c,end = " ")
    print()   

#to print profit details
for r in profit:
    for c in r:
       print(c,end = " ")
    print()    

No comments:

Post a Comment