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

Lab 03

This document contains a summary of a physics lab assignment. It provides the solutions to Assignment 1, hints for Assignment 2, and offers to answer any other questions. For Question 1 on Assignment 1, it describes the grading criteria and provides sample Python code to capitalize words in a string. For Question 2, it lists the grading criteria and provides sample code to find the multiplicative inverse and decrypt a phrase. Hints for Assignment 2 encourage replacing for loops with array operations and using NumPy functions like random integers and where to simplify the code. It also refers students to lecture slides for one part of the question.

Uploaded by

harrisonyky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lab 03

This document contains a summary of a physics lab assignment. It provides the solutions to Assignment 1, hints for Assignment 2, and offers to answer any other questions. For Question 1 on Assignment 1, it describes the grading criteria and provides sample Python code to capitalize words in a string. For Question 2, it lists the grading criteria and provides sample code to find the multiplicative inverse and decrypt a phrase. Hints for Assignment 2 encourage replacing for loops with array operations and using NumPy functions like random integers and where to simplify the code. It also refers students to lecture slides for one part of the question.

Uploaded by

harrisonyky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PHYS 3142

Lab 03
Xingkai [email protected]
Xizhi [email protected]
Catalog
• Solutions for Assignment 1
• Hints for Assignment 2
• Any other questions
Question 1
50 points
Grading points:
1. Capitalize words successfully (30 points)
2. Two words connected by ‘-’, i.e. Two-dimensional (10 points)
3. The first words in the sentence, i.e. ‘The Origin of’ (5 points)
4. Special professional terms ‘WTe2’ (5 points)

We require more general ways:


def Capitalized(words):

Code if ord(words[0])<123 and ord(words[0])>96: #only deal with a-z, skip A-Z
C_words = chr(ord(words[0])-32) + words[1:]
Capitalization function

else:
C_words = words

return C_words

Word = []
for i in range(len(text)):
if i==0:
flag = 0
else:
flag = 0
for j in prepositions:
if text[i]==j: main function
flag = 1
break
if flag:
word.append(text[i])
else:
word.append(Capitalized(text[i]))
Question 2
50 points

Grading points:
(a). Multiple inverses:12 totally (someone missed 1,1 and 25,25)
m=26
for a in range(0,26):
for b in range(1,26):
if (a*b)%m==1:
print(a,b)

print((7*15)%26)

print((7*3+18)%26)
(b). Almost correct

(c). With a=9 and b=14, we have computationalmethodsinphysics


def decipher(phrase): # find a and b
global secret
count=0
for a in range(1,26):
if a%2!=0 and a!=13:
for b in range(1,26):
phrase2=""
for i in range(len(phrase)):
if phrase[i]!=" ":
a_inv=mul_inv(a)
number2=np.mod((letter_to_number(phrase[i])*a_inv-b*a_inv)
,26)
phrase2=phrase2+number_to_letter(number2)
#print(number2)
else:
phrase2=phrase2+" "
if phrase2[0]==secret[0] and phrase2[1]==secret[1]:
print(a,b,phrase2)
count+=1
return 0

secret="computationalmethodsinphysics"
Question 3
N=9
last_row=[1]

for i in range(0,N+1):
last_row=[0]+last_row+[0]
this_row=[]
row=""
for j in range(i+1):
this_row.append(last_row[j]+last_row[j+1])
row=row+(4-len(str(this_row[j])))*" "+str(this_row[j])
front=" "*(2*(N+2)-int(len(row)/2)-2)
back=" "*(2*(N+2)-int(len(row)/2))
last_row=this_row
print(front+row+back)

Follow Youtube for Q4


Question 5
Hints for Assignment 2
For Q1, you should replace all for loops with arrays operations.

For example, in the original program, you have the lines


for n in range(num_pos):
if A[n]==1:
if n in pos_take:
num_lose += 1
break

But range(num_pos) is simply [0,1,2,3,…,20] and range(pos_take) is (0,1,2,3,…,10).

You can just write for some index i, if i>=10, num_lose+=1.

The structure is different while the output is the same.


You can use np.random.randint(20,size=num_test) and np.where().

You are encouraged to use other ways.


Hint for Assignment 2
For Q2.1, you can simplify the notations and use inverse matrix to solve it,
the determinant is just a constant and will not be integrated.

For Q2.2, follow the slides for Lecture 5

You might also like