iT邦幫忙

第 11 屆 iT 邦幫忙鐵人賽

DAY 7
0
自我挑戰組

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

Day06|Codewars 刷題日 (5)

開學了,
結果事情接踵而來...
好想一直睡覺啊 qwq

7kyu. Shortest Word

Simple, given a string of words, return the length of the shortest word(s).
String will never be empty and you do not need to account for different data types.

最間單的方法就是將字串切開,
一個一個去比對XD

def find_short(s):
    l = 999999
    s = s.split()
    for i in s:
        if l > len(i): l = len(i)
    return l

看了解答,
原來

min(len(x) for x in s.split())

就可以辦到了XD
果然還是不熟悉這種寫法呀...


7kyu. Greatest common divisor

Find the greatest common divisor of two positive integers. The integers can be large, so you need to find a clever solution.

The inputs x and y are always greater or equal to 1, so the the greatest common divisor will always be an integer that is also greater or equal to 1.

簡單講就是找最大公因數,
使用輾轉相除法即可~

def mygcd(x,y):
    while y > 0:
        t = y
        y = x % y
        x = t
    return x

今天就先這樣子,
要來去備課了 qq


上一篇
Day05|Codewars 刷題日 (4)
系列文
Daily Coding!一日一刷題7

尚未有邦友留言

立即登入留言