Code Signal_练习题_isLucky
Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half.
Given a ticket number n, determine if it's lucky or not.
Example
- For
n = 1230, the output should beisLucky(n) = true; - For
n = 239017, the output should beisLucky(n) = false.
我的解答:
def isLucky(n):
n = str(n)
li = []
for i in n:
li.append(i)
f = int(len(li)/2)
sum_l = 0
sum_r = 0
for x in li[:f]:
sum_l += int(x)
for y in li[f:]:
sum_r += int(y)
print(sum_l,sum_r)
return sum_l == sum_r 最后一句本来想这样写的:
if sum_l == sum_r:
return True
else:
return False
做了几道题发现其他人都一句话完事,于是也学到了...
膜拜大佬:
def isLucky(n):
s = str(n)
pivot = len(s)//2
left, right = s[:pivot], s[pivot:]
return sum(map(int, left)) == sum(map(int, right)) 刚学了map,也知道怎么回事,就是想不起来用..还是得多练练关于map的
Code Signal_练习题_isLucky的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
随机推荐
- ThinkPHP5.0手把手实现手机阿里云短信验证
阿里云短信服务介绍阿里云短信服务就是以前的阿里大于,不过现在融合得到阿里云平台了.首先,你需要注册一个阿里云账号,这个自行解决. 仅用于测试使用官方送的代金券够用了.相关配置1.开通阿里云Access ...
- Elasticsearch-搜索推荐
对于es中搜索推荐的功能的实现: 1.使用match_phrase_prefix来实现search-time搜索推荐,原理根match_phrase类似,唯一的区别是把最后一个term作为前缀去搜索, ...
- MySQL远程登陆解决
第一句:以权限用户root登录 第二句:选择mysql库 第三句:查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称) 第四句:修改host值(以通配符%的内容增加主机/IP ...
- Linux命令学习与使用2
1.Ctrl+a:跳到命令行首 Ctrl+E: 跳到命令行尾 Ctrl+L:清屏2.切换用户 su - 用户名3.更换yum镜像源 1.进入/etc/yum.repos.d 备份CentOS-Base ...
- 三:MyBatis学习总结(三)——优化MyBatis配置文件中的配置
一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下 <?xml version="1. ...
- 结合 RunLoop 和 Instrument 定位卡顿
iOS 应用,丝般顺滑的理想情况就是 60FPS (对于 iPad Pro 是 240FPS),即在 16ms 之内完成一次渲染.如果找到在每次渲染花费了多久,究竟做了什么事情,那么就可以进行针对性的 ...
- Gson简单使用
最近做个IM类型的Android 应用,由于有三种客户端(pc,ios,Android),所以底层使用的是C++与服务器通信,所以通信部分基本上有c++完成,封装好Jni即可,可以把底层c++通信看成 ...
- iOS多线程---GCD中线程的通信
在子线程的任务完成后,有时候需要从子线程回到主线程,刷新UI. 从子线程中回到主线程,以前已经写过一种方法: [self.imageView performSelectorOnMainThread:@ ...
- Django中指定生成表名的方法
在模型类中定义元类: class Meta: de_table = 'tableName' #指定表名
- ajax--底层代码
ajax:Asynchronous JavaScript And XML,异步的js与XML.ajax并不是一种新的编程语言,而是一种使用现有标准的新方法.ajax能够在不重载整个网页的情况下与服务器 ...