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) + ...
随机推荐
- 读DEDECMS找后台目录有感
本文作者:红日安全团队——Mochazz 早上看了先知论坛的这篇文章:解决DEDECMS历史难题–找后台目录 不得不说作者思路确实巧妙,作者巧妙的利用了Windows FindFirstFile和织梦 ...
- Android应用安全防护和逆向分析 ——apk混淆成其他语言代码
现在很多人对于app的安全是越来越重视了,尤其是金融相关的行业,对于app的防范可是下足了功夫.各种加固,各种加密算法,层出不穷.我个人觉得,在安全技术这块,没有绝对安全的.也许今天这个技术起到了防范 ...
- Numpy 创建数组2
Numpy数组除了可以使用底层 ndarray 构造器来创建外,也可以同伙一下集中方式来创建. numpty.empty numpy.empty方法用来创建一个指定形状(shaoe).数据类型(dty ...
- c#连接oracle的几种方式
一:通过System.Data.OracleClient(需要安装Oracle客户端并配置tnsnames.ora)1. 添加命名空间System.Data.OracleClient引用2. usin ...
- python3 使用 zabbix_client模块
除了上一篇使用自己定义的函数,我们还可使用pipy提供的zabbix_client模块,这样就不用我们自己去写登录函数,只要在模块开始指定用户名密码即可,方便很多. #!/usr/bin/env py ...
- ViewPage最全解析
简单说明: ViewPager是android扩展包v4包中的类,直接继承了ViewGroup类,和LinearLayout等布局一样,都是一个容器,需要在里面添加我们想要显示的内容. 一.在xml中 ...
- C# 终极基类Object介绍
一.简介 Object这个类型,相信everyone都不陌生,这个是CLR定义的最基础的类型,俗称"上帝类".CLR(运行时)要求所有类型,不管是系统定义的类型还是自定义的类型,都 ...
- xampp启动失败 Apache shutdown unexpectedly
我知道是端口被我的虚拟机占用,但是我的虚拟机也要同时打开,所以 我需要修改xampp的端口 1, 2修改二个配置文件 修改httpd.conf 修改httpd-ssl.conf 4,重启xampp
- BugFree3.0.4Linux环境安装指南
bugfree安装的前提是配置LAMP(apache+mysql+php),我安装的linux系统是centos6.0 一.安装Apache服务器 1.安装apache yum install htt ...
- vue制作小程序--server
服务端代码,开发者工具有提供 指南 https//cloud.tencent.com/document/product/619/11442 参考文档API https://developers.wei ...