题目:

Your task is to add up letters to one letter.

The function will be given a variable amount of arguments, each one being a letter to add.

Notes:

  • Letters will always be lowercase.
  • Letters can overflow (see second to last example of the description)
  • If no letters are given, the function should return 'z'

Examples:

add_letters('a', 'b', 'c') = 'f'
add_letters('a', 'b') = 'c'
add_letters('z') = 'z'
add_letters('z', 'a') = 'a'
add_letters('y', 'c', 'b') = 'd' # notice the letters overflowing
add_letters() = 'z'

-------------------------------------------------------------------------------------------------

题目大意:字母a-z代表了1-26,给定一个list,将里面字母对应的数字加起来的值对应到字母中。

解题思路:

def add_letters(*letters):
# your code here
if letters == '' or letters == 'z':
return 'z'
else:
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}
dic2 = {1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g', 8:'h', 9:'i', 10:'j', 11:'k', 12:'l', 13:'m', 14:'n', 15:'o', 16:'p',
17:'q', 18:'r', 19:'s', 20:'t', 21:'u', 22:'v', 23:'w', 24:'x', 25:'y', 26:'z'}
sum = 0
for i in letters:
sum += dic[i]
res = sum % 26
if res == 0:
return 'z'
else:
return dic2[res]

个人的代码总是那么的不堪入目啊 QAQ。基本思路是:做两个字典,分别将字母和数字为key值,然后将数值相加后取余,得到的数字再对应到字母表。

看一下网友们的答案:

num = 'abcdefghijklmnopqrstuvwxyz'
def add_letters(*letters):
x = 0
x = sum(num.index(i)+1 for i in letters)
while x-1 > 25:
x -= 26 return num[x-1]

另一种:

def add_letters(*letters):
return chr( (sum(ord(c)-96 for c in letters)-1)%26 + 97)

知识点:

1、ord(c) 将c转为数字,chr(c)将c转为ASCII表中的字母

2、def add_letters(*letters):带星号是代表传入元组

【Kata Daily 190912】Alphabetical Addition(字母相加)的更多相关文章

  1. 【Kata Daily 190916】String end with?(字母结尾)

    题目: Complete the solution so that it returns true if the first argument(string) passed in ends with ...

  2. [LeetCode] Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

  3. [LeetCode] 370. Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

  4. 【Kata Daily 190929】Password Hashes(密码哈希)

    题目: When you sign up for an account somewhere, some websites do not actually store your password in ...

  5. 【Kata Daily 190904】Calculating with Functions(函数计算)

    原题: This time we want to write calculations using functions and get the results. Let's have a look a ...

  6. 【Kata Daily 190903】String incrementer(字符串增量器)

    原题: Your job is to write a function which increments a string, to create a new string. If the string ...

  7. 【Kata Daily 191012】Find numbers which are divisible by given number

    题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...

  8. 【Kata Daily 191010】Grasshopper - Summation(加总)

    题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...

  9. 【Kata Daily 190927】Counting sheep...(数绵羊)

    题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...

随机推荐

  1. 046 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 08 for循环的注意事项

    046 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 08 for循环的注意事项 本文知识点:for循环的注意事项 for循环的注意事项 for循环有3个 ...

  2. 012 01 Android 零基础入门 01 Java基础语法 02 Java常量与变量 06 浮点型“字面值”

    012 01 Android 零基础入门 01 Java基础语法 02 Java常量与变量 06 浮点型"字面值" 浮点型字面值 首先要知道一点:在整型部分中,默认情况下,即整型数 ...

  3. PADS Layout VX.2.3 将PCB中的元器件封装保存到库

    工具1:PADS Layout VX.2.3 菜单File > Library...,打开Library Manager,点击Create New Lib...新建一个库. 使用快捷键Ctrl ...

  4. appium 环境安装指引

    1.安装Appium-Python-Client Pip install Appium-Python-Client 2.安装nodejs https://nodejs.org/ 安装成功验证:node ...

  5. ok6410 3.0.1内核调用V4L接口出错解决方法(转)

    在做视频监控项目,以前一直用的是2.6.36的内核,一直很正常,但是这几天换3.0.1内核,启动程序,却出现了错误,如下: ./test_usb_camera XXXXXXXXXXXXXXXXXXXX ...

  6. 福州11911.562(薇)xiaojie:福州哪里有xiaomei

    福州哪里有小姐服务大保健[微信:11911.562倩儿小妹[福州叫小姐服务√o服务微信:11911.562倩儿小妹[福州叫小姐服务][十微信:11911.562倩儿小妹][福州叫小姐包夜服务][十微信 ...

  7. 使用Spring Boot创建docker image

    目录 简介 传统做法和它的缺点 使用Buildpacks Layered Jars 自定义Layer 简介 在很久很久以前,我们是怎么创建Spring Boot的docker image呢?最最通用的 ...

  8. java流程控制之习题

     经过近段时间的学习,差不多也掌握了java的流程控制以及基本知识,下面就来一起练练习题吧,看能做出来几道. 第一道题:假设小明有100块钱,这时候小明去超市需要换零钱,超市提供的零钱有1元面值,2元 ...

  9. File、Blob、ArrayBuffer等文件类的对象有什么区别和联系

    前言 在前端中处理文件时会经常遇到File.Blob.ArrayBuffer以及相关的处理方法或方式如FileReader.FormData等等这些名词,对于这些常见而又不常见的名词,我相信大多数人对 ...

  10. 使用composer 显示错误美化

    新建comoser.json { "name": "brady_frmwork", "description":"php fram ...