題目:
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:
- customers: an array (list in python) of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
- n: a positive integer, the number of checkout tills.
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:
- There is only ONE queue, and
- The order of the queue NEVER changes, and
- Assume that the front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.
- The diagram on the wiki page I linked to at the bottom of the description may be useful.
輸出樣例:
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)