iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 3
0
自我挑戰組

Daily Coding!一日一刷題系列 第 3

Day02|Codewars 刷題日 (1)

仔細看了看昨天信件剩餘的題目,
hmm.. 我們還是換題好了 σ`∀´)σ

5kyu. Number of trailing zeros of N!

Write a program that will calculate the number of trailing zeros in a factorial of a given number.

N! = 1 * 2 * 3 * ... * N

Examples

zeros(6) = 1
# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero

zeros(12) = 2
# 12! = 479001600 --> 2 trailing zeros

坦白說第一想法是:爆開(X
但爆開只會通往 TLE 的道路。
所以我們還是乖乖的求救 Google 大神 qwq

尾數有 0 的情況為 五的倍數 * 二的倍數所形成的,
雖然可以找這個數字有幾個 2 及 幾個 5,
但 2 的倍數太多了,
所以決定性的因素就在 5 身上。

def zeros(n):
    count = 0
    while n >= 5:
        count += n//5
        n //= 5
    return count

我這個笨蛋想了頗久,
為甚麼可以一直取 5 的商數,
後來才理解可以整理為這個公式:
count = n // 5 + n // 25 + n // 125 + ...


6kyu. Duplicate Encoder

The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.

Examples

"din"      =>  "((("
"recede"   =>  "()()()"
"Success"  =>  ")())())"
"(( @"     =>  "))((" 

這題對於 python 來說很簡單,
有重複 => ")"
沒重複 => "("

另外記得全部統一大小寫RRR
(本人犯的低級錯誤)

def duplicate_encode(word):
    word = word.lower()
    ans = ""
    for i in word:
        if word.count(i) > 1:
            ans += ")"
        else: ans += "("
    return ans

題外話,
今天好不容易把 AD 驗證的密碼到期日判定寫完,
其實頗有成就感的啦 XD
果然睡了一覺起床就解決了 (X
離開學剩下三天,
希望不要再晚睡了RRRR qwq


上一篇
Day01|Weekly Coding Challenges
下一篇
Day03|Codewars 刷題日 (2)
系列文
Daily Coding!一日一刷題7
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言