鱼C论坛

 找回密码
 立即注册
查看: 3810|回复: 30

[已解决]Python:每日一题 154(高额悬赏)

[复制链接]
发表于 2018-2-13 12:57:55 | 显示全部楼层 |阅读模式
20鱼币
本帖最后由 新手·ing 于 2018-2-13 16:26 编辑

我们的玩法做了一下改变:

1. 增加悬赏机制,答对题目的前十位鱼油有资格参加悬赏竞争(我是穷人)代码简洁,运行效率高者获胜
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内
4. 根据答案的质量给予前十名鱼油5鱼币的奖励。
5. 当然我们的题目是面向新人出滴,也是想通过鱼币奖励给他们动力。所以,@YBKathus ,你不能竞争20鱼币,下一期才可以(给大家留点机会吧)

题目:

Given a string of words, you need to find the highest scoring word.

Each letter of a word scores points according to it's position in the alphabet: a = 1, b = 2, c = 3 etc.

You need to return the highest scoring word as a string.

If two words score the same, return the word that appears earliest in the original string.

All letters will be lowercase and all inputs will be valid.

翻译:

给定一串单词,你需要找到最高的得分单词。

一个字的每个字母根据它在字母表中的位置得分:a = 1,b = 2,c = 3等

您需要将最高得分的单词作为字符串返回。

如果两个单词得分相同,则返回原始字符串中最早出现的单词。

所有字母都是小写字母,所有输入都是有效的。

Test:
  1. test.assert_equals(high('man i need a taxi up to ubud'), 'taxi')
  2. test.assert_equals(high('what time are we climbing up the volcano'), 'volcano')
  3. test.assert_equals(high('take me to semynak'), 'semynak')
复制代码


楼主的代码,怎么感觉误打误撞效率更高:
游客,如果您要查看本帖隐藏内容请回复
最佳答案
2018-2-13 12:57:56
  1. def high(str1):
  2.     list1 = str1.split(' ')
  3.     list1.sort(key = lambda x: - sum([ord(i) - 96 for i in x]))
  4.     return list1[0]
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 14:08:41 | 显示全部楼层
  1. def high(sentence):
  2.     score_high = 0
  3.     best_word = ''
  4.     while sentence:
  5.         score_word = 0
  6.         word_tuple = sentence.partition(' ')
  7.         word = word_tuple[0]
  8.         for each in word:
  9.             each_score = ord(each) - ord('a') + 1
  10.             score_word += each_score
  11.         if score_word > score_high:
  12.             score_high = score_word
  13.             best_word = word
  14.         sentence = word_tuple[2]
  15.     return best_word
复制代码

评分

参与人数 1鱼币 +5 收起 理由
新手·ing + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 14:19:05 | 显示全部楼层
  1. words =input().split(" ")
  2. table = list('abcdefghijklmnopqrstuvwxyz')
  3. max=0
  4. maxword=''
  5. for word in words:
  6.     sum=0
  7.     for n in list(word):
  8.         sum+=table.index(n)
  9.     if sum>max:
  10.         max = sum
  11.         maxword = word
  12. print(maxword)
复制代码

评分

参与人数 1鱼币 +5 收起 理由
新手·ing + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 14:19:29 | 显示全部楼层
  1. a = ['man i need a taxi up to ubud',
  2.      'what time are we climbing up the volcano',
  3.      'take me to semynak']


  4. def high(a):
  5.     alist = a.split(' ')
  6.     numList = []
  7.     for i in alist:
  8.         k = [(ord(j)-96) for j in i]
  9.         snum = sum(k)
  10.         numList.append(snum)
  11.     maxnum = max(numList)
  12.     return alist[numList.index(maxnum)]

  13. for i in a:
  14.     var = high(i)
  15.     print(var)
复制代码

评分

参与人数 1鱼币 +5 收起 理由
新手·ing + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 14:21:18 | 显示全部楼层
def high(string):
        str1 = 'abcdefghijklmnopqrstuvwxyz'
        list1 = string.split()
        r1 = 0
        r2 = 0
        for each_word in list1:
                for each_letter in each_word:
                        r1 += str1.find(each_letter) + 1
                #print(each_word , r1)检查用程序
                       
                if r1 > r2:
                        r2 = r1
                        result = each_word
                r1 = 0
        return result
       
print(high('man i need a taxi up to ubud'), 'taxi')
print(high('what time are we climbing up the volcano'), 'volcano')
print(high('take me to semynak'), 'semynak')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 14:36:47 | 显示全部楼层
string=input("Please input a string with a lot of words:")
strlist=string.split()
alphaScore=dict(a=0,b=1,c=2,d=3,e=4,f=5,g=6,h=7,i=8,j=9,k=10,l=11,m=12,n=13,o=14,p=15,q=16,r=17,s=18,t=19,u=20,v=21,w=22,x=23,y=24,z=25)
highScore=0
for word in strlist:
    score=0
    for alpha in word:
        score+=alphaScore[alpha]
        
    if score>highScore:
        highScore=score
        highScoreStr=word

print("high(",end="'")
print(string,end="'")
print("),",end="'")
print(highScoreStr,end="'")

评分

参与人数 1鱼币 +5 收起 理由
新手·ing + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 12:57:56 | 显示全部楼层    本楼为最佳答案   
  1. def high(str1):
  2.     list1 = str1.split(' ')
  3.     list1.sort(key = lambda x: - sum([ord(i) - 96 for i in x]))
  4.     return list1[0]
复制代码

评分

参与人数 1鱼币 +5 收起 理由
新手·ing + 5 大神,,,你别这样。

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 15:43:56 | 显示全部楼层
  1. a = 'abcdefghijklmnopqrstuvwxyz'
  2. def high(string):
  3.         string = string.lower().split()
  4.         result = ''
  5.         highS = 0
  6.         for word in string:
  7.                 score = sum([a.index(char)+1 for char in word])
  8.                 if score > highS:
  9.                         highS = score
  10.                         result = word
  11.         return result
复制代码

评分

参与人数 1鱼币 +5 收起 理由
新手·ing + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 15:45:46 | 显示全部楼层
  1. #创建字典
  2. list1=list('abcdefghijklmnopqrstuvwxyz')
  3. dict1={}
  4. j=1
  5. for i in list1:
  6.    
  7.     dict1.setdefault(i,j)
  8.     j+=1
  9. def max_file(str_test):#参数给你要测试的字符串
  10.     global dict1
  11.     sum_one=0#记录一个单词每次字符对应数字的总和
  12.     list2=[]#记录字符串每个单词总和的列表
  13.     list_file=str_test.split(' ')#测试字符串转换成列表 以空格区分每个单词
  14.    
  15.     for each in list_file:
  16.         for i in list(each):  
  17.             sum_one+=dict1[i]
  18.         list2.append(sum_one)
  19.     max_dciwz=max(list2)#计算字符串中数值最大的
  20.     for j in range(0,len(list2)):
  21.         if list2[j]==max_dciwz:
  22.             return list_file[j]#返回字符串中最大单词
  23. jj=max_file(input('请输入你要测试的数据,一个单词默认以空格结束:'))
  24. print(jj)
复制代码

评分

参与人数 1鱼币 +5 收起 理由
新手·ing + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 14:18:04 | 显示全部楼层
words =input().split(" ")
table = list('abcdefghijklmnopqrstuvwxyz')
max=0
maxword=''
for word in words:
    sum=0
    for n in list(word):
        sum+=table.index(n)
    if sum>max:
        max = sum
        maxword = word
print(maxword)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 16:18:38 | 显示全部楼层
  1. def score(s):
  2.     score = 0
  3.     for i in s: score += ord(i)
  4.     return score
  5.    
  6. def high(s):
  7.     L = s.split()
  8.     result = L[0]
  9.     for x in L:
  10.         if score(x) > score(result):
  11.             result = x
  12.     return result
复制代码

评分

参与人数 1鱼币 +5 收起 理由
新手·ing + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-13 16:25:08 | 显示全部楼层

错了,再想想吧,提示97和26
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 17:26:54 | 显示全部楼层
请问你们用的ord()是什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 19:23:19 | 显示全部楼层
s = input('请输入一串字符:')
def found(s):
    a = s.split(' ')
    m1 = 0
    for i in range(len(a)):
        x = 0
        for j in range(len(a[i])):
            x = x + ord(a[i][j])
        if x > m1:
            m1 = x
            m2 = a[i]
    print('得分最高的为:' + m2)
found(s)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 19:29:02 | 显示全部楼层
  1. def high(s):
  2.     return max( [(sum(ord(c) - 96 for c in x), x) for x in s.split()] )[1]
  3. 好乱。。算了
复制代码

评分

参与人数 1鱼币 +10 收起 理由
新手·ing + 10 雾草,你要是早发,你就是第一了

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 19:34:22 | 显示全部楼层
waitforlove 发表于 2018-2-13 17:26
请问你们用的ord()是什么

转ascii 的函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-13 22:17:27 | 显示全部楼层
新手·ing 发表于 2018-2-13 16:25
错了,再想想吧,提示97和26

本来想着对输出结果没影响,特地把“-96”给去了……
  1. def score(s):
  2.     score = 0
  3.     for i in s: score += ord(i) - 96
  4.     return score
  5.    
  6. def high(s):
  7.     L = s.split()
  8.     result = L[0]
  9.     for x in L:
  10.         if score(x) > score(result):
  11.             result = x
  12.     return result
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-14 01:08:51 | 显示全部楼层

第二也不亏,啊哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-14 13:41:18 | 显示全部楼层
大头目 发表于 2018-2-13 14:21
def high(string):
        str1 = 'abcdefghijklmnopqrstuvwxyz'
        list1 = string.split()

为啥唔这没分=。=##
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-17 21:58:01 | 显示全部楼层
  1. text = input()
  2. allwords = text.split(" ")
  3. dic = []
  4. for each in allwords:
  5.         num = 0
  6.         for x in list(each):
  7.                 num += ord(x)
  8.         dic.append((each,num))
  9. dic.sort(key = lambda k:k[1],reverse = True)
  10. print(dic[0][0])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 13:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表