[心得] 自我描述數列與String pattern
自我描述數列是這玩意兒
http://zh.wikipedia.org/wiki/%E5%A4%96%E8%A7%80%E6%95%B8%E5%88%97
= 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211 (說明見上)
[實作1]
LookSay[str_String] := StringJoin @@
StringCases[str, p : ((x_(*?DigitQ*)) .. ~~ (x_ | "")) :>
ToString@StringLength[p] <> StringTake[p, {1}]];
- -
NestList[LookSay, "1", 10]
=> {"1", "11", "21", "1211", "111221", "312211", "13112221",
"1113213211", "31131211131221", "13211311123113112211",
"11131221133112132113212221"}
- -
說明: 函數核心部分在於
StringCases[str, ((x_(*?DigitQ*)) .. ~~ (x_ | ""))]
它的作用是把 "11223aaaab" 拆成 {"11", "22", "3", "aaaa", "b"}
若把(*?DigitQ*)的註解拿掉,則忽略掉字母。
叫做核心因為string pattern我最不熟,這部分改了最久XD
其他部分只是在做 "111" -> "3個1" -> "31" 這件事而已,是個拼拼湊湊。
- * - *
ListPlot[Log[StringLength /@ NestList[LookSay, "1", 40]],
PlotLabel -> "Log[length[LS[n]]]"]
(*數列的長度依指數增長的圖示,原理的說明見wiki跟下*)
http://www.njohnston.ca/2010/10/a-derivation-of-conways-degree-71-look-and-say-polynomial/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.213.88
→
03/27 23:33, , 1F
03/27 23:33, 1F
推
03/28 00:05, , 2F
03/28 00:05, 2F
→
03/28 00:05, , 3F
03/28 00:05, 3F
→
03/28 00:06, , 4F
03/28 00:06, 4F
推
03/28 00:09, , 5F
03/28 00:09, 5F
→
03/28 00:09, , 6F
03/28 00:09, 6F
→
03/28 00:12, , 7F
03/28 00:12, 7F
→
03/28 00:15, , 8F
03/28 00:15, 8F
→
03/28 00:17, , 9F
03/28 00:17, 9F
→
03/28 00:18, , 10F
03/28 00:18, 10F
推
03/28 00:28, , 11F
03/28 00:28, 11F
→
03/28 00:30, , 12F
03/28 00:30, 12F
→
03/28 00:30, , 13F
03/28 00:30, 13F
→
03/28 00:31, , 14F
03/28 00:31, 14F
→
03/28 00:50, , 15F
03/28 00:50, 15F
→
03/28 00:52, , 16F
03/28 00:52, 16F
推
03/28 02:06, , 17F
03/28 02:06, 17F
Mathematica 近期熱門文章
PTT數位生活區 即時熱門文章