Code Signal_缁冧範棰榑isLucky
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code Signal_缁冧範棰榑isLucky相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/git' title='git'>git
src 鍒嗕韩鍥剧墖 pre red 鍏充簬 falseTicket 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鈥榮 lucky or not.
Example
- For
n = 1230
, the output should beisLucky(n) = true
; - For
n = 239017
, the output should beisLucky(n) = false
.
鎴戠殑瑙g瓟锛?/strong>
1 def isLucky(n): 2 n = str(n) 3 li = [] 4 for i in n: 5 li.append(i) 6 f = int(len(li)/2) 7 sum_l = 0 8 sum_r = 0 9 for x in li[:f]: 10 sum_l += int(x) 11 for y in li[f:]: 12 sum_r += int(y) 13 print(sum_l,sum_r) 14 return sum_l == sum_r 15 16 鏈€鍚庝竴鍙ユ湰鏉ユ兂杩欐牱鍐欑殑锛? 17 if sum_l == sum_r: 18 return True 19 else: 20 return False 21 鍋氫簡鍑犻亾棰樺彂鐜板叾浠栦汉閮戒竴鍙ヨ瘽瀹屼簨锛屼簬鏄篃瀛﹀埌浜?..
鑶滄嫓澶т浆锛?/strong>
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)) 鍒氬浜唌ap锛屼篃鐭ラ亾鎬庝箞鍥炰簨锛屽氨鏄兂涓嶈捣鏉ョ敤..杩樻槸寰楀缁冪粌鍏充簬map鐨?/span>
以上是关于Code Signal_缁冧範棰榑isLucky的主要内容,如果未能解决你的问题,请参考以下文章