技术标签: python Python入门 Python基础 codewars
题目:
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.
题意:
就是给出一句字符串,计算出得分最大的那个单词;比如abc,得分就是1+2+3=6分;a代表1分,b2分,c3分以此类推……
我的解题思路是:
(1)首先先建立一个字典:
dic={'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10,'k':11,'l':12,'m':13,'n':14,'o':15,'p':16,'q':17,'r':18,'s':19,'t':20,'u':21,'v':22,'w':23,'x':24,'y':25,'z':26}
(2)然后将字符串转化成列表,运用了split()函数
(3)遍历列表的每一个元素
(4)针对每一个元素,遍历元素中的字符,计算该元素的得分
(5)将得分放在一个新的列表中
(6)求最大值对应的得分函数的下标
(7)找到下标对应的列表元素将其转成字符串输出,运用了"".join()函数
代码如下:
dic = {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10,'k':11,'l':12,'m':13,'n':14,'o':15,'p':16,'q':17,'r':18,'s':19,'t':20,'u':21,'v':22,'w':23,'x':24,'y':25,'z':26}
def high(x):
y=x.split()
c=[]
for i in y:
sum1=0
maxsum=0
for j in i:
sum1=sum1+dic[j]
c.append(sum1)
maxindex=c.index(max(c))
return "".join(y[maxindex])
看了别人最优答案,学到了一个新的东西就是lambda表示匿名函数
下面来解释一下:
python用关键字lambda表示匿名函数。
例1.对于函数f(x)=x*x,以下两种表达方式等价;
def f(x):
return x*x
>>>f(5)
>>>25
等价于下面的:
f=lambda x:x*x
>>>f(5)
>>>25
冒号前的x表示函数的参数。
例2.将列表按照元素绝对值大小进行排序
list1=[2,5,6,-4,8-5,3,-7]
def get_abs(x):
return abs(x)
sorted(list1,key=get_abs)
#等价于下面的:
list1=[2,5,6,-4,8-5,3,-7]
sorted(list1,key=lambda x:abs(x))
从上面这个例子可以看出:sorted(传入一个列表,按照什么规则排序(这里是绝对值))
例3.找到句子中字母ASCII码之和最大的单词
同理:
x='i want a banana'
max(x.split(),key=lambda k:sum(ord(c)-96 for c in k))
输出:'want'
max(传入要找最大得分的列表,按照什么规则找最大值格式是key=lambda k:……)
第二题:
There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!
The function has two input variables:
The function should return an integer, the total time required.
EDIT: A lot of people have been confused in the comments. To try to prevent any more confusion:
输出样例:
queue_time([5,3,4], 1)
# should return 12
# because when n=1, the total time is just the sum of the times
queue_time([10,2,3,3], 2)
# should return 10
# because here n=2 and the 2nd, 3rd, and 4th people in the
# queue finish before the 1st person has finished.
queue_time([2,3,10], 2)
# should return 12
题意:
说白了就是有个队列cutomer,有n个结账的收银台处,计算排队总时间;就是顾客找最空闲的结账窗口排队
比如[5,3,4],n=1,明显只有一个结账处,就是5+3+4=12个总时间
再比如:[10,2,3,3],n=2,明显10个总时间
代码如下:
def queue_time(customers, n):
queue = [0]*n
for i in customers:
queue.sort()
queue[0]+=i
return max(queue)
文章浏览阅读235次。关于android开发设置View Pager的直接跳转页set CurrentItem会阻塞主线程ANR。根据网上解决的说法,分析源码:if (mFirstLayout) { // We don't have any idea how big we are yet and shouldn't have any pages either. // Just..._viewpager#mfirstlayout
文章浏览阅读1.1k次。Windows下Nginx负载均衡配置和优化方案方案配置的环境:安装环境:Windows系统Nginx版本:nginx-1.18.0代理网站服务器:Windows server系统1.Nginx下载进入下载地址:http://nginx.org/en/download.html选择Windows版本进行下载。2.Nginx安装2.1解压安装将下载好的Nginx压缩文件解压在相关的安装位置,建议放在非C盘的根目录下,原因是网站访问量大时,产生的日志容量会很大,从而占据C盘的存储空间,增加_windows nginx优化
文章浏览阅读640次。以下是基于小程序wux框架中refresh组件拓展的小程序滚动切换页码组件。用法<page-list id="wux-refresher" elid="biddingList" isShowPage="{{false}}" bind:scrollbot="scrollbot" bind:refresh="onRefresh"> <template is="b..._wux-refresher loadmore
文章浏览阅读2.3k次。由于服务器断电,启动 oracle 时报 ORA-00600 错误查看 oracle trace 日志1. 执行 sqlplus 登录 oraclesqlplus / as sysdba1.2 启动,报错信息如下SQL> startupORACLE instance started.Total System Global Area 583008256 bytesFix_ora-00600: internal error code, arguments: [kgepop: no error frame to pop to
文章浏览阅读2.2k次,点赞4次,收藏6次。假设我们在一个 Dialog 中放置了两个 PushButton ,分别叫 pushButton 和 pushButton_2。要实现功能:当鼠标悬浮到其中一个pushButton上时,两个pushbutton的QSS样式互换newdialog.hclass newDialog : public QDialog{ ... 代码省略 ...public..._qt悬浮按钮
文章浏览阅读1.1w次,点赞7次,收藏16次。编写一个程序,对给定字符串中出现的a~z字母频率进行分析,忽略大小写,采用 降序方式输出。##################################################txt=input("请输入一段英文文本:")txt=txt.lower()counts={}for i in txt: if i in "abcdefghijklmnopqrstuvwx..._英文字符频率统计,a~z字母频率统计,采取降序输出
文章浏览阅读1.5k次。修改/etc/my.cnfcharacter-set-server=utf8[client]default-character-set=utf8注释掉 sql_mode重新新建mysql表在hive的元数据库中执行以下SQL语句,然后重新创建刚才的表即可 。alter table COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;alter table TABLE_PARAMS modify column PAR_sqoop --default-character-set=utf8
文章浏览阅读845次。一、简介 在用python写脚本时,通常需要处理xml格式的文件或字符串。由于json格式处理的方便性, 我们可将其转为json格式进行处理。 二、步骤 1、安装工具包xmltodict 在命令行输入:pip install xmltodict 2、在代码使用xmltodict.parse(xml_str)进行处理 如 def load_json(xml_path): #获取xml文件 xml_f..._python xml 转json 格式
文章浏览阅读1.6k次。1111_mybatis sql 标签里面写入参
文章浏览阅读661次。比如说:模板书写:{'param1'|functionName:'param2':'param3'}php函数原型:echo functionName('param1','param2','param3');实例:{'1234567'|substr_smarty中的内置函数可以实现邮件链接的是
文章浏览阅读2.4k次。转载:https://towardsdatascience.com/ensemble-methods-bagging-boosting-and-stacking-c9214a10a205作者:Joseph Rocca Ensemble methods: bagging, boosting andstackingIntroduction“Unity is ..._bagging方法结合深度学习模型
文章浏览阅读6.7k次。//返回文件名称(文件夹读取文件)public static ArrayList<String> getFilesPath(String path) throws Exception {//目标集合fileListArrayList<String> fileList = new ArrayList<String>();File file = new ..._文件的创建和删除java