给定如下一段英文 New to programming

给定如下一段英文 

New to programming? Python is free and easy to learn if you know where to start!This guide will help you to get started quickly.

编写一个函数,要求实现以下功能:

(1)统计有多少个不同的单词

(2)根据每个单词ASC||码值的和(如单词they的ASC||码值的和是:116+104+101+121=442)对单词进行从小到大的排序,重复出现的单词只算一次的和,按行输出单词及对应的和

答案
def word_ascii_stat(text):
    # 替换标点符号
    text = text.replace('?','').replace('!','')
    # 分割成单词列表,全部小写(可选,题目没有区分大小写就按原样)
    words = text.split()
    # 去重,保留唯一单词
    unique_words = list(set(words))
    # (1)统计不同单词数量
    count_diff = len(unique_words)
    print(f"不同单词数量:{count_diff}")

    word_sum = []
    for w in unique_words:
        # 计算单词每个字符ascii之和
        s = sum(ord(c) for c in w)
        word_sum.append((s, w))

    # (2)按ascii和从小到大排序
    word_sum.sort(key=lambda x:x[0])
    # 逐行输出单词及和
    for total, word in word_sum:
        print(f"{word} : {total}")

# 原始文本
s = """New to programming? Python is free and easy to learn if you know where to
start! This guide will help you to get started quickly."""

word_ascii_stat(s)

题目信息

题号:8143
题型:简答题
知识点:Python
难度:普通