【Kata Daily 190912】Alphabetical Addition(字母相加)
题目:
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(字母相加)的更多相关文章
- 【Kata Daily 190916】String end with?(字母结尾)
题目: Complete the solution so that it returns true if the first argument(string) passed in ends with ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- [LeetCode] 370. Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- 【Kata Daily 190929】Password Hashes(密码哈希)
题目: When you sign up for an account somewhere, some websites do not actually store your password in ...
- 【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 ...
- 【Kata Daily 190903】String incrementer(字符串增量器)
原题: Your job is to write a function which increments a string, to create a new string. If the string ...
- 【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 ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- 【Kata Daily 190927】Counting sheep...(数绵羊)
题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...
随机推荐
- 【题解】[国家集训队]happiness
题目戳我 \(\text{Solution:}\) 显然还是一个分组问题.对于理科和文科我们可以看出最小割模型,而处理同时选择某一学科的时候,需要我们根据套路建立虚点处理. 同 小M的作物 一题,这题 ...
- randomatic
下载 randomaticrandomatic 使用简单的字符序列生成指定长度的随机字符串.原generate-password. 请考虑下面这个项目的作者,Jon Schlinkert主演的,考虑项 ...
- Windows 10 系统 - business editions 和 consumer editions 的区别
我们在使用微软操作系统(Windows 10)的时候,因为系统版本太多导致我们不知道如何选择.对于 Windows 10 系统,应该下载安装 business 还是 consumer 版本这个问题,这 ...
- BeetleX之webapi自定义响应内容
输出内容多样性在webapi服务中比较普遍的,有的情况使用json,xml,图片和二进制流下载等等:为了适应用不同情况的需要,组件支持自定义内容输出.接下来的主要描述组件在webapi如何定义各种内容 ...
- python post与get请求的区别
post:请求的url不带参数 查询参数在WebForms保存 get:请求的url会附带查询参数 查询参数在QueryString保存
- OpenCV计算机视觉学习(5)——形态学处理(腐蚀膨胀,开闭运算,礼帽黑帽,边缘检测)
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 形态 ...
- Thumbnailator处理图片
读取源图 of(String... files) of(File... files) of(InputStream... inputStreams) of(URL... urls) 输出文件 toFi ...
- zookeeper-(单机,伪集群)
安装zookeeper(单机,伪集群): 1.下载 登陆zookeeper官网下载 https://zookeeper.apache.org/ 或者 https://mirror.bit.e ...
- 【Azure Redis 缓存 Azure Cache For Redis】Redis性能问题,发现Server Load非常的高,导致正常连接/操作不成功
问题描述 在正常使用Azure Redis的服务中,突然发现Redis 的CPU达到了100%, 正常的使用中发现性能问题严重.从Redis的门户图表中,观察到CPU, Connection,Lent ...
- c++ 获取文件创建时间、修改时间、访问时间、文件内容长度
int GetFileInfo(string& strPath, int& iCreateTime, int& iModifyTime, int& iAccessTim ...