Files
task-3-2-1-Text-Processing-…/260421 2505155046.py
2026-04-21 11:28:19 +08:00

46 lines
1.4 KiB
Python
Raw Permalink 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 运行结果 =====")
s = "Hello"
print("字符串 'Hello' 每个字符的ASCII码")
for char in s:
print(f"字符 '{char}' 的ASCII码为{ord(char)}")
print("\n验证chr(65)")
print(f"ASCII码65对应的字符是{chr(65)}")
print("\n" + "="*50 + "\n")
print("===== 题目3 运行结果 =====")
A = [3, 4]
B = [1, 2]
A_plus_B = [A[0] + B[0], A[1] + B[1]]
print(f"A + B 的结果:{A_plus_B}")
A_times_2 = [2 * A[0], 2 * A[1]]
print(f"2 × A 的结果:{A_times_2}")
import math
A_norm = math.sqrt(A[0]**2 + A[1]**2)
print(f"A 的模(长度):{A_norm}")
print("\n" + "="*50 + "\n")
print("===== 题目4 运行结果 =====")
A1 = [1, 2, 3]
B1 = [4, 5, 6]
dot_product = sum(a * b for a, b in zip(A1, B1))
print(f"A·B 的点积:{dot_product}")
norm_A1 = math.sqrt(sum(a**2 for a in A1))
norm_B1 = math.sqrt(sum(b**2 for b in B1))
cos_similarity = dot_product / (norm_A1 * norm_B1)
print(f"A 和 B 的余弦相似度:{cos_similarity:.4f}")
A2 = [1, 0]
B2 = [0, 1]
dot_product2 = sum(a * b for a, b in zip(A2, B2))
norm_A2 = math.sqrt(sum(a**2 for a in A2))
norm_B2 = math.sqrt(sum(b**2 for b in B2))
cos_similarity2 = dot_product2 / (norm_A2 * norm_B2)
print(f"\n向量 A={A2} 和 B={B2} 的余弦相似度:{cos_similarity2}")
print("原因两个向量相互垂直正交方向完全不同因此余弦相似度为0")