## Mathematical Operations in Python

```
print("***** QUESTION 1 *****\n");
def factorial(n):
"""
Recursively calculat the factorial of n
"""
# Base case
if n == 1:
return 1
return n*factorial(n-1)
def calnWk(n, k):
return n**k
def calnPk(n ,k):
return factorial(n)/factorial(n-k)
def calnCk(n, k):
return calnPk(n, k)/factorial(k)
print("\n***** QUESTION 2 *****\n");
# Use built-in functions to get the coefficient
from sympy import *
x, y = symbols('x y')
exp = (2*x**2-4*y**3)**30
coeff_real = exp.expand().coeff(x**20).coeff(y**60)
print("The coefficient calculated using built-in function for x^20*y^60 is " + str(coeff_real))
print("\n***** QUESTION 3 *****\n");
# Now get the coefficient using the functions from Part 1
# The coefficient of x^90 *y^10 can be calculated as a combination, selecting the element 90 from the 100 available elements
# Coefficients of x and y: (ax^c + by^d)^n
a = 2
b = -4
c = 2
d = 3
n = 30 #
k = 20 #
coeff_calc = calnCk(n,k)*a**(n-k) *b**k
print("The coefficient calculated using the functions from Part 1 for for x^20*y^60 is: " + str(coeff_calc))
"""
print("\n***** QUESTION 4 *****\n");
# Create a random 3x3 matrix
import numpy as np
A = np.random.uniform(low = 0, high = 1, size=(3,3)) # matrix of random integers from 1 to 10
b = np.matrix([[1],[0],[0]])
# Check if Ax = b has a solution
print("Matrix A is:\n" + str(A))
print("\nVector b is:\n" + str(b))
x = np.linalg.solve(A,b)
print("\nThe solution x for Ax = b is:\n" + str(x))
```

## Text parsing in Python

```
Number of users = 500
Number of users with email id ending with .jp = 26
Number of users with email id ending with .uk = 20
Number of users with email id ending with .de = 8
Surnames that have duplicattes are ['Culbard', 'De', 'Schiell', 'Wisbey']
import csv
users=0
first=True
jp=0
uk=0
de=0
last_names=[]
duplicates=[]
print("Hello user.Welcome.")
print("Analyzing mydata.csv file")
with open('mydata.csv','r') as dataFile:
reader=csv.reader(dataFile)
for row in reader:
if not first:
users+=1
if row[1]:
if row[1] in last_names:
if row[1] not in duplicates:
duplicates.append(row[1])
else:
last_names.append(row[1])
if row[2]:
if row[2][-3:]=='.jp':
jp+=1
elifrow[2][-3:]=='.uk':
uk+=1
elifrow[2][-3:]=='.de':
de+=1
first=False
dataFile.close()
print("Completed analyzing")
print("\nNumber of users = "+str(users))
print("Number of users with email id ending with .jp = "+str(jp))
print("Number of users with email id ending with .uk = "+str(uk))
print("Number of users with email id ending with .de = "+str(de))
print("Surnames that have duplicattes are "+str(duplicates))
f=open("Janet_results.txt","w")
f.write("Number of users = "+str(users))
f.write("\nNumber of users with email id ending with .jp = "+str(jp))
f.write("\nNumber of users with email id ending with .uk = "+str(uk))
f.write("\nNumber of users with email id ending with .de = "+str(de))
f.write("\nSurnames that have duplicattes are "+str(duplicates))
f.close()
print("\nThe results are saved in Janet_results.txt file.")
print("Thank you for using the program.Good Bye.")
```