[學習筆記] 紀錄codewars解題過程

Visits: 0

最近學了python,為了快速增進自己熟悉語言,在網路上搜尋了一下,看看有沒有適合的練習環境。發現很多人會推薦leetcode與codewars,那這兩個平台有什麼不一樣的地方呢?根據林信良在iThome發表的文章表示,leetcode上比較多的題目是針對效能、演算法、資料結構、空間、時間、數學等,而codewars則偏重於IDE、重構技巧、開發工法、Dojo/Kata題庫、語言特性等。

我目前用比較多的平台是在codewars上,除了上述原因之外,codewars是我目前搜尋少數有提供R語言練習的環境(因為R根本就不被認為是程式語言),因此,近期就先在codewars上練習一下,讓自己趕快熟悉python的語言特性,之後有空再來練練R與SQL囉。

python

清單篩選(List filtering)

題目:給定一個list,將list內的數字挑出
範例:

filter_list([1,2,'a','b']) == [1,2]
filter_list([1,'a','b',0,15]) == [1,0,15]
filter_list([1,2,'aasf','1','123',123]) == [1,2,123]

解法:

def filter_list(l):
    new_l = []
    for obj in l:
        if type(obj) == int:
            print(obj)
            new_l.append(obj)
            continue
        else:
            continue
    return new_l

數字反轉(Reverse a number)

題目:將數字的順序反轉,若為負值亦同
範例:

Test.assert_equals(reverse_number(123), 321)
Test.assert_equals(reverse_number(-123), -321)
Test.assert_equals(reverse_number(1000), 1)

解法:

def reverse_number(n):
    if n >= 0:
        n = int(str(n)[::-1])
    else:
        n = int(str(abs(n))[::-1]) * -1
    return n

Who is going to pay for the wall

題目:撰寫一個狼來了的程式,符合下列要求

  1. 狼與羊的隊伍以list表示,每個隊伍至少會有一隻狼
  2. 觀察者的位置是在list的最後面,也就是說領頭羊的位置是在隊伍的最後面,如下所示
[sheep, sheep, sheep, sheep, sheep, wolf, sheep, sheep]      (YOU ARE HERE AT THE FRONT OF THE QUEUE)
   7      6      5      4      3            2      1
  1. 若狼在領頭羊的位置,則顯示訊息"Pls go away and stop eating my sheep"
  2. 若狼在隊伍中,則警告位於狼前一隻的羊,顯示訊息"Oi! Sheep number 狼前一隻! You are about to be eaten by a wolf!"

範例:

注意狼的位置與第幾隻羊的順序是不同的

test.assert_equals(warn_the_sheep(['sheep', 'sheep', 'sheep', 'sheep', 'sheep', 'wolf', 'sheep', 'sheep']), 'Oi! Sheep number 2! You are about to be eaten by a wolf!')
test.assert_equals(warn_the_sheep(['sheep', 'wolf', 'sheep', 'sheep', 'sheep', 'sheep', 'sheep']), 'Oi! Sheep number 5! You are about to be eaten by a wolf!')
test.assert_equals(warn_the_sheep(['wolf', 'sheep', 'sheep', 'sheep', 'sheep', 'sheep', 'sheep']), 'Oi! Sheep number 6! You are about to be eaten by a wolf!')
test.assert_equals(warn_the_sheep(['sheep', 'wolf', 'sheep']), 'Oi! Sheep number 1! You are about to be eaten by a wolf!')
test.assert_equals(warn_the_sheep(['sheep', 'sheep', 'wolf']), 'Pls go away and stop eating my sheep')

解法:

def warn_the_sheep(queue):
    ql = len(queue) # ql: queue length
    for pos in range(ql): # pos: position
        if queue[pos] == "sheep":
            continue
        elif queue[pos] == "wolf" and queue[-1] != "wolf":
            sp = pos + 1 # sp: sheep position
            sq = ql - sp # sq: sheep queue, it's reversed from sheep position
            return f"Oi! Sheep number {sq}! You are about to be eaten by a wolf!"
        elif queue[-1] == "wolf":
            return 'Pls go away and stop eating my sheep'

Mumbling(字串處理的一種)

題目:
設計一個函數(accum),把字串拆解,並把字串的第一個字大寫後,以"-"相接,並按照字串位置,重複該字串的次數

範例:

accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"

解法:

def accum(s):
    j = 1
    for i in range(0, len(s)):
        temp = s[i].upper() + s[i].lower() * i
        if j == 1:
            result = temp
            j = 0
        else:
            result = result + "-" + temp
    return result

看到一個比較python-style的解法,紀錄一下

def accum2(s):
    i = 0
    result = ''
    for letter in s:
        print(letter)
        result += letter.upper() + letter.lower() * i + '-'
        i += 1
    print(result)
    return result[:len(result)-1]

About the Author

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

You may also like these