Files
task-3-2-1-Text-Processing-…/ljh.py
2026-04-21 11:25:29 +08:00

67 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

print("题目1 ")
hello1 = "Hello"
hello2 = 'Hello'
print("双引号表示:", hello1)
print("单引号表示:", hello2)
print("\n1. 'Hello' 每个字符的ASCII码:")
for c in hello1:
print(f"字符 '{c}' → ASCII: {ord(c)}")
print("\n2. 验证ASCII码65对应的字符:")
print(f"chr(65) → {chr(65)}")
import math
print("\n题目3")
A = [3, 4]
B = [1, 2]
A_plus_B = [A[0] + B[0], A[1] + B[1]]
print(f"1. A + B = {A_plus_B}")
two_times_A = [2 * A[0], 2 * A[1]]
print(f"2. 2 × A = {two_times_A}")
len_A = math.sqrt(A[0] ** 2 + A[1] ** 2)
print(f"3. 向量A的长度 = {len_A}")
print("\n题目4")
A3 = [1, 2, 3]
B3 = [4, 5, 6]
dot_product = A3[0]*B3[0] + A3[1]*B3[1] + A3[2]*B3[2]
print(f"1. 点积A·B = {dot_product}")
def cosine_similarity(a, b):
dot = sum(x*y for x,y in zip(a,b))
norm_a = math.sqrt(sum(x*x for x in a))
norm_b = math.sqrt(sum(x*x for x in b))
return dot / (norm_a * norm_b)
cos_sim = cosine_similarity(A3, B3)
print(f"2. 余弦相似度 = {cos_sim:.4f}")
A_test = [1, 0]
B_test = [0, 1]
cos_sim_test = cosine_similarity(A_test, B_test)
print(f"3. A=[1,0], B=[0,1]的余弦相似度 = {cos_sim_test}")
print("原因两个向量正交垂直方向完全不同所以余弦相似度为0")