题目:

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. Jupyter 绘图怎么显示中文

    1. 简单加2行代码即可. import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = [u'SimHei'] plt.rcPa ...

  2. SetDlgItemInt(函数详解)

    参考:https://blog.csdn.net/for_cxc/article/details/51799194 SetDlgItemInt(hwnd, IDC_TEXT, FREQ_INIT, F ...

  3. C++库文件解析(conio.h)

    转载:https://blog.csdn.net/ykmzy/article/details/51276596 Conio.h 控制台输入输出库该文内容部分参照百度百科 Conio.h 在C stan ...

  4. windows.h系统函数

    转载:https://blog.csdn.net/u010756046/article/details/82432312 // Windows系统函数.cpp: 定义控制台应用程序的入口点.// #i ...

  5. Oracle缓存表与Oracle缓存的区别

    一.Oracle缓存表 与 Oracle缓存 的概念 Oracle 缓存:是把Oracle近期查询的语句放置在Oracle设定的缓存当中. Oracle 缓存表:是把某个表放置在缓存当中,缓存是Ora ...

  6. 多测师讲解python函数 _zip_高级讲师肖sir

    # zip函数 #zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存.1.使用zip讲两个列表打印出来的结果是 ...

  7. 【7】进大厂必须掌握的面试题-Java面试-Jsp

    1. jsp的生命周期方法是什么? 方法 描述 公共无效的jspInit() 与servlet的init方法相同,仅被调用一次. 公共无效_jspService(ServletRequest requ ...

  8. 物联网wifi模块

    物联网wifi模块 物联网wifi模块 是上海卓岚推出的MQTT+JSON转Modbus物联网WiFi核心模块.支持以MQTT的方式连接云端服务器,支持可以界面话配置,自主采集Modbus仪表/645 ...

  9. localhost与127.0.0.1与0.0.0.0

    localhost localhost其实是域名,一般系统默认将localhost指向127.0.0.1,但是localhost并不等于127.0.0.1,localhost指向的IP地址是可以配置的 ...

  10. Eclipse JSP +Tomcat 环境搭建 错误记录

    环境搭建请参考原文:https://www.cnblogs.com/james-lee/p/5964238.html 错误1:运行时,弹出如下错误:(如果没有此错误,请忽略) 原因是,我们之前点击了T ...