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 be
      isLucky(n) = true;
    • For n = 239017, the output should be
      isLucky(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的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. 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) + ...

随机推荐

  1. day 55 linux 的常用命令

    前言 前面咱们已经成功安装了Linux系统--centos7,那么我们现在提好裤腰带,准备奔向Linux的大门.  Linux命令行的组成结构 [root@oldboy_python ~]# [roo ...

  2. PostgreSQL 数据库错误代码解释

    PostgreSQL 服务器发出的所有消息都赋予了五个字符 的错误代码, 这些代码遵循 SQL 的 "SQLSTATE" 代码的习惯.需要知道发生了什么错误条件的应用通常应该测试错 ...

  3. Window - 浏览器对象模型

    浏览器对象模型 (BOM) 浏览器对象模型(Browser Object Model (BOM))尚无正式标准. 由于现代浏览器已经(几乎)实现了 JavaScript 交互性方面的相同方法和属性,因 ...

  4. webstorm 添加css前缀(兼容)自动添加

    Webstorm自动添加css前缀( 兼容) 百度了很多在webstorm中添加css前缀(兼容)自动添加,autoprefixer插件是首选,对于基本的css,还有less都支持,所以就选择了aut ...

  5. 剑指offer四十六之孩子们的游戏(圆圈中最后剩下的数,约瑟夫环问题)

    一.题目 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指 ...

  6. C++的开源跨平台日志库glog学习研究(二)--宏的使用

    上一篇从整个工程上简单分析了glog,请看C++的开源跨平台日志库glog学习研究(一),这一篇对glog的实现代码入手,比如在其源码中以宏的使用最为广泛,接下来就先对各种宏的使用做一简单分析. 1. ...

  7. Java之集合(十二)TreeMap

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7411935.html 1.前言 本章介绍Map体系中的TreeMap,顾名思义,这个是一个树结构的Map.Tre ...

  8. phpstorm之自定义代码碎片(tab键自动填充代码)

    打开phpstorm 的设置界面(快捷键ctrl+alt+s) 比如上面的form表单,需要在生成以后自动跳转到“名称”的位置,然后更改,可以如下修改

  9. android activity和fragment的生命周期图

    Activity的生命周期: Fragment的生命周期:

  10. java 实现 HTTP请求(GET、POST)的方法

    使用Java进行服务调用时,避免不了要使用模拟HTTP请求来实现模拟,我在开发过程中恰巧遇到了这类的业务需求,所以就对这类的方法进行了一次总结,原理层次的东西暂时不考虑,毕竟HTTP的底层实现啥的,东 ...