【每日一题0826】得分最高的单词

:woman_mage:已知一个由纯小写英文字母构成的单词所组成的字符串,我们的任务是编写一个python3函数,找到其中分数最高的那个单词。计算得分的规则是按照字母表的位置,例如a=1, b=2, c=3... 。如果两个单词得分相同,那么只取最先出现的那个单词。

示例:
输入:hogwarts is the best ,输出:hogwarts

题目难度:简单
题目来源:codewars

def highest_score(words: str) -> str:
    pass

assert highest_score("hogwarts is the best") == "hogwarts"
assert highest_score("harry potter and the order of the phoenix") == "potter"
assert highest_score("aaa b") == "aaa"
assert highest_score("aa b") == "aa"
assert highest_score("b aa") == "b"
def highest_score(words: str) -> str:
    maximum = 0
    score = lambda x: sum(ord(i)-ord('a')+1 for i in x)
    for word in words.split():
        word_score = score(word)
        if word_score > maximum :
            maximum = word_score
            max_word = word
    return max_word
def highest_score(words: str) -> str:
    score = lambda x: sum(ord(i)-ord("a")+1 for i in x)
    return max(words.split(), key = score)
    public String highestScore(String s) {
        String[] s1 = s.split(" ");
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        for (int i = 0; i < s1.length; i++) {
            int result = 0;
            char[] chars = s1[i].toCharArray();
            for (int j = 0; j < chars.length; j++) {
                result = result + (int) chars[j] - 96;
            }
            stringIntegerHashMap.put(s1[i], result);
        }
        Set<Map.Entry<String, Integer>> entries = stringIntegerHashMap.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        int maxSum = 0;
        String res = "";
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> next = iterator.next();
            Integer value = next.getValue();
            String key = next.getKey();
            if (value > maxSum) {
                maxSum = value;
                res = key;
            }
        }
        return res;
    }

    @Test
    public void highestScoreTest() {
        assert highestScore("hogwarts is the best").equals("hogwarts");
        assert highestScore("harry potter and the order of the phoenix").equals("potter");
        assert highestScore("aaa b").equals("aaa");
    }

一楼是个人才

def highest_score(words: str) -> str:
    word_list=words.split(' ')
    max_score = 0
    result = word_list[0]
    for word_item in word_list:
        temp=0
        for i in word_item:
            temp += ord(i)
        if temp>max_score:
            max_score=temp
            result=word_item
    return result

普通做法

import string
def highest_score(words: str) -> str:
    
    chars = '0'+string.ascii_lowercase
    words = words.split()
   
    all_counts = []
    for word in words:
        count = 0
        for c in word:
            count += chars.index(c)
        all_counts.append(count)
        
    return words[all_counts.index(max(all_counts))]
    
assert highest_score("hogwarts is the best") == "hogwarts"
assert highest_score("harry potter and the order of the phoenix") == "potter"
assert highest_score("aaa b") == "aaa"
assert highest_score("aa b") == "aa"
assert highest_score("b aa") == "b"

思路:先利用string模块的ascii_lowercase()获得所有的小写字母,额外加个前置’0’占位是为了让每个字母的索引值刚好符合需求的数值。然后将目标字符串进行分隔,双层循环分别按顺序统计每个单词的所有字母的数值。最后返回最大数值对应位置的字符串。

参考题解:结合内置函数max()和ord()

def highest_score(words: str) -> str:
    return max(words.split(), key=lambda word:sum(ord(c)-96 for c in word))
    
assert highest_score("hogwarts is the best") == "hogwarts"
assert highest_score("harry potter and the order of the phoenix") == "potter"
assert highest_score("aaa b") == "aaa"
assert highest_score("aa b") == "aa"
assert highest_score("b aa") == "b"

解题思路:利用内置函数sorted()进行排序,排序规则是根据每个单词对应数值的总和。这里使用的是匿名函数进行字母汇总求和,其中对应数值则是通过内置函数ord()获取。例如ord('a')的返回值是97,那么减去96即可获得符合题意的参考值1。最后,排序后数值总和最大的会排在末尾,使用索引[-1]即可得到最大的那个单词。

ord() 函数:以一个字符(长度为1的字符串)作为参数,返回对应的ASCII数值,或者 Unicode 数值,如果所给的 Unicode 字符超出了你的 Python 定义范围,则会引发一个 TypeError 的异常。同时ord()函数也是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数。

max() 函数返回给定参数的最大值,参数可以为序列。

关闭