”’
/****************************************************************************************************/
/* #This is solution v1.2 (basic) the the mock test 2 */
/* #Author: Dr. Adrian Euler */
/* #Module: SMM283 – Introduction to Python */ */
/****************************************************************************************************/
Tested with Python 3.7, 3.8, and 3.9
As an exercise you can modify/change, etc.
”’
##THIS IS A ROUGH SOLUTION DRAFT, AS AN EXTERCISE YOU MAY TRY TO IMPROVE IT,
##ESPECIALLY THE VALIDATIONS.
import math
import time
##mean: computes the simple arithmetic average …
def mean(x=[]):
i, sumtemp, n = (0, 0.0, len(x))
while (i < n):
sumtemp += x[i]
i += 1
return (sumtemp/i)
##pmean: computes the probabilistic average
def pmean(p = [], x = []):
i, sump, n=(0, 0.0, len(p))
while i < n:
sump += p[i]*x[i]
i += 1
return (sump)
##var: computes the variance
def var(x = []):
i, sumtemp, m, n = (0, 0.0, mean(x), len(x))
##m = mean(x); /*obtain the mean..we need it for the deviations*/
while i < n:
sumtemp += ((x[i] - m)*(x[i] - m))
i += 1
i -= 1
return (sumtemp/i)
##pvar: computes the variance of a probabilistic data set
def pvar(p=[], x=[]):
i, sump, m, n = (0, 0.0, mean(x), len(p))
##m = mean(x); /*obtain the mean..we need it for the deviations*/
while i < n:
sump += (p[i]*((x[i] - m)*(x[i] - m)))
i += 1
return (sump)
##transpose: swap rows with columns....
def transpose(m=[[]]):
mr = len(m)
mc = len(m[0])
tloc = [[m[j][i] for j in range(mr)] for i in range(mc)]
return(tloc)
##------------------------------------------------------------------------------------------
##main: the main control function of the program
def main():
start = time.time() #star time
##-------------------------------------------------------------------------------------
x, p, i, j = ([], [], 0, 0)
while True:
try:
n = int(input("Enter size of array (> 1): “)) ##notice n is declared globally
if n <= 1:
print('Increase the size of the arrray...')
continue
break
except:
print('Should be an integer value...')
pass
counter = n
while True:
try:
print("Enter %d real numbers. " % counter)
while i < n:
x.insert(i, float(input('x[' + str(i) + ']:= ')))
i += 1
print(x)
print("The mean is: %.2lf" % mean(x)) ##the mean works just fine
print("The variance is: %.2f" % var(x))
break
except:
print('Should be real values (floats).....')
pass
while True:
try:
print("Enter %d probabilities in the range <0 to 1>: ” % n)
i = 0
while i < n:
prob = float(input('p[' + str(i) + ']:= '))
p.insert(i, prob if (prob >=0 and prob <= 1) else 0.0)
i += 1
#p.insert(n-1) = 1.0 - math.fsum(p)
print("The sumproduct is: %.2f"% pmean(p, x)) ##the mean works just fine
print("The probability-based variance is: %.2f" % pvar(p, x))
break
except:
pass
while True:
try:
print("Enter the number of rows: ")
r = int(input()); ##initialize global variable
print("Enter the number of columns: ")
c = int(input()) ##initialize global variable
tr, tc = (c, r)
##t, m = ([[0.0]*tc]*tr, [[0.0]*c]*r)
t = [[] for k in range(tr)]
m = [[] for l in range(r)]
for i in range(0, r):
for j in range(0, c):
m[i].insert(j, float(input('Enter:= ')))
print('value stored @ m(%d,%d) '%( i, j))
#print('\n')
'''
for i in range(0, r):
for j in range(0, c):
i, j = (i, j)
print('%s ' % str(m[i][j]), end = '')
print('\n')
'''
print('The matrix is:')
for v in m:
print(v)
t = transpose(m) ##call transpose function
print('The transposed matrix is:')
for w in t:
print(w)
break
except:
print('Should be integer values and greater than 0....')
pass
##----------------------------------------------------------------------------------------
end = time.time()
print('It took {0} seconds to run this program......'.format(str(end-start)))
##---------------------------------------------------------------------------------------------
main() ##call the main function