首先,這些:
def strHead(s):
"""strHead: String -> String"""
return s[0]
def strTail(s, run):
"""strTail: String -> String"""
return s[:]
他們沒有做任何有用的;第一個返回項0,第二個返回字符串。查克他們。
然後
def runLenAux(s, c):
if s == "":
return 0
else:
if strHead(s)== c:
return 1 + int(runLenAux(strTail(s), c))
else:
return 0
現在有一個明顯的問題。當他們充其量是列表中的數字時,您會將這些數字視爲相同數字。但其他時候,你會像串線一樣對待它們。
讓我們把它們看成像列表一樣,因爲它更明顯。
所以:
def runLenAux(s, c):
什麼是s
和c
?不知道。我想這是爲了獲得下一個值,所以做一個[1, 0] → [1, 1, 1, 0]
映射。所以讓我們拿一個列表:
def look_and_say(term):
"""Looks at and says a term."""
而不是做你的搞笑->
的東西,在Python 3,你可以註釋你的函數。通過向他們展示這個半掩蓋的事實來打動你的同齡人!
def look_and_say(term: "[int]") -> "[int]":
"""Looks at and says a term."""
這裏我用"[int]"
表示 「int
的list
的」。
那麼,下一個部分:
if s == "":
return 0
天哪!你計劃使用遞歸嗎?這太多了!我們不是Haskell在這裏!
行,從頂部。我們想重複一遍,記下我們看過的同一件物品的數量,是的?因此,我們可以做一個循環,如:
for item in term:
count how many are the same
if different, break
(。這聽起來象是一個已經做過好,它有它的所謂itertools.groupby
讓我們假設你不知道的是,雖然)。
count = 0
match = None
for item in term:
# If this isn't the same we should stop counting
if match != item:
do_something_with_count_and_match
# Start again from this item
count = 1
match = item
# If this is the same we should continue counting
else:
count = count + 1
這看起來不錯!
怎麼把do_something_with_count_and_match
?好吧,讓我們保持一個清單,把事情和Chuck在那裏
# This is what we're building
next_term = []
...
for item in term:
...
if match != item:
# Say "One One" or "Two Threes" or "Three Ones", etc.
next_term.append(count)
next_term.append(match)
# Start again from this item
count = 1
match = item
...
所以我們的結果,它運行?
def look_and_say(term: "[int]") -> "[int]":
"""Looks at and says a term."""
# This is what we're building
next_term = []
count = 0
match = None
for item in term:
# If this isn't the same we should stop counting
if match != item:
# Say "One One" or "Two Threes" or "Three Ones", etc.
next_term.append(count)
next_term.append(match)
# Start again from this item
count = 1
match = item
# If this is the same we should continue counting
else:
count = count + 1
look_and_say([0])
#>>>
嗯...沒有輸出。
啊,我們忘記了return
的next_term
:
def look_and_say(term: "[int]") -> "[int]":
... # All that stuff
return next_term
look_and_say([0])
#>>> [0, None]
嗯..嗯,這是沒有好。我們需要確保我們不計算None
佔位match
:
def look_and_say(term: "[int]") -> "[int]":
...
for item in term:
...
if match != item:
if match is not None:
next_term.append(count)
next_term.append(match)
...
...
而且我們還需要加上最後一節,即使它不會觸發else
(循環停止):
def look_and_say(term: "[int]") -> "[int]":
...
for item in term:
...
if match != item:
if match is not None:
next_term.append(count)
next_term.append(match)
return next_term
那就試試吧:
def look_and_say(term: "[int]") -> "[int]":
"""Looks at and says a term."""
# This is what we're building
next_term = []
count = 0
match = None
for item in term:
# If this isn't the same we should stop counting
if match != item:
# Say "One One" or "Two Threes" or "Three Ones", etc.
if match is not None:
next_term.append(count)
next_term.append(match)
# Start again from this item
count = 1
match = item
# If this is the same we should continue counting
else:
count = count + 1
if match is not None:
next_term.append(count)
next_term.append(match)
return next_term
look_and_say([0])
#>>> [1, 0]
look_and_say([1, 0])
#>>> [1, 1, 1, 0]
look_and_say([1, 1, 1, 0])
#>>> [3, 1, 1, 0]
呀!
我這樣做是爲了向你展示編程不是魔術。你只需要繼續嘗試和應用你所知道的。
個人而言,我會實現它是這樣的:
from itertools import groupby
# What we just implemented
def look_and_say(string):
for k, v in groupby(string):
yield sum(1 for _ in v)
yield k
list(look_and_say([0]))
#>>> [1, 0]
list(look_and_say([1, 0]))
#>>> [1, 1, 1, 0]
list(look_and_say([1, 1, 1, 0]))
#>>> [3, 1, 1, 0]
但是,這只是因爲我知道groupby
。
請注意,您的'strTail'返回整個字符串's'的一個副本。要返回's'的尾部(除第一個字符外的所有內容),請使用's [1:]'。 –