@@ -1988,83 +1988,79 @@ msgstr ""
19881988"from operator import itemgetter, getitem, mul, neg\n"
19891989"\n"
19901990"def take(n, iterable):\n"
1991- " \" Return first n items of the iterable as a list. \" \n"
1991+ " \" 回傳可疊代物件的前 n 個元素為串列。 \" \n"
19921992" return list(islice(iterable, n))\n"
19931993"\n"
19941994"def prepend(value, iterable):\n"
1995- " \" Prepend a single value in front of an iterable. \" \n"
1995+ " \" 在可疊代物件前插入單一值。 \" \n"
19961996" # prepend(1, [2, 3, 4]) → 1 2 3 4\n"
19971997" return chain([value], iterable)\n"
19981998"\n"
19991999"def tabulate(function, start=0):\n"
2000- " \" Return function(0), function(1), ...\" \n"
2000+ " \" 回傳 function(0), function(1), ...\" \n"
20012001" return map(function, count(start))\n"
20022002"\n"
20032003"def repeatfunc(function, times=None, *args):\n"
2004- " \" Repeat calls to a function with specified arguments. \" \n"
2004+ " \" 重複呼叫一個帶指定引數的函式。 \" \n"
20052005" if times is None:\n"
20062006" return starmap(function, repeat(args))\n"
20072007" return starmap(function, repeat(args, times))\n"
20082008"\n"
20092009"def flatten(list_of_lists):\n"
2010- " \" Flatten one level of nesting. \" \n"
2010+ " \" 將巢狀結構攤平一層。 \" \n"
20112011" return chain.from_iterable(list_of_lists)\n"
20122012"\n"
20132013"def ncycles(iterable, n):\n"
2014- " \" Returns the sequence elements n times. \" \n"
2014+ " \" 回傳序列的元素重複 n 次。 \" \n"
20152015" return chain.from_iterable(repeat(tuple(iterable), n))\n"
20162016"\n"
20172017"def loops(n):\n"
2018- " \" Loop n times. Like range(n) but without creating integers. \" \n"
2018+ " \" 執行 n 次的迴圈。類似 range(n) 但不建立整數序列。 \" \n"
20192019" # for _ in loops(100): ...\n"
20202020" return repeat(None, n)\n"
20212021"\n"
20222022"def tail(n, iterable):\n"
2023- " \" Return an iterator over the last n items. \" \n"
2023+ " \" 回傳一個疊代器,疊代最後 n 個元素。 \" \n"
20242024" # tail(3, 'ABCDEFG') → E F G\n"
20252025" return iter(deque(iterable, maxlen=n))\n"
20262026"\n"
20272027"def consume(iterator, n=None):\n"
2028- " \" Advance the iterator n-steps ahead. If n is None, consume entirely. \" \n"
2029- " # Use functions that consume iterators at C speed. \n"
2028+ " \" 將疊代器往前推進 n 步。如果 n 為 None,則完全消耗。 \" \n"
2029+ " # 使用以 C 語言的速度消耗疊代器的函式。 \n"
20302030" if n is None:\n"
20312031" deque(iterator, maxlen=0)\n"
20322032" else:\n"
20332033" next(islice(iterator, n, n), None)\n"
20342034"\n"
20352035"def nth(iterable, n, default=None):\n"
2036- " \" Returns the nth item or a default value. \" \n"
2036+ " \" 回傳第 n 個元素或預設值。 \" \n"
20372037" return next(islice(iterable, n, None), default)\n"
20382038"\n"
20392039"def quantify(iterable, predicate=bool):\n"
2040- " \" Given a predicate that returns True or False, count the True results."
2041- "\" \n"
2040+ " \" 給定一個回傳 True 或 False 的判斷函式,計算為 True 的結果。\" \n"
20422041" return sum(map(predicate, iterable))\n"
20432042"\n"
20442043"def first_true(iterable, default=False, predicate=None):\n"
2045- " \" Returns the first true value or the *default* if there is no true "
2046- "value.\" \n"
2044+ " \" 回傳第一個為 true 的值,若無則回傳*預設值*。\" \n"
20472045" # first_true([a,b,c], x) → a or b or c or x\n"
20482046" # first_true([a,b], x, f) → a if f(a) else b if f(b) else x\n"
20492047" return next(filter(predicate, iterable), default)\n"
20502048"\n"
20512049"def all_equal(iterable, key=None):\n"
2052- " \" Returns True if all the elements are equal to each other. \" \n"
2050+ " \" 回傳 True,如果所有元素兩兩相等。 \" \n"
20532051" # all_equal('4٤௪౪໔', key=int) → True\n"
20542052" return len(take(2, groupby(iterable, key))) <= 1\n"
20552053"\n"
20562054"def unique_justseen(iterable, key=None):\n"
2057- " \" Yield unique elements, preserving order. Remember only the element "
2058- "just seen.\" \n"
2055+ " \" 產生唯一的元素,並保留原始順序。只記住剛看見的元素。\" \n"
20592056" # unique_justseen('AAAABBBCCDAABBB') → A B C D A B\n"
20602057" # unique_justseen('ABBcCAD', str.casefold) → A B c A D\n"
20612058" if key is None:\n"
20622059" return map(itemgetter(0), groupby(iterable))\n"
20632060" return map(next, map(itemgetter(1), groupby(iterable, key)))\n"
20642061"\n"
20652062"def unique_everseen(iterable, key=None):\n"
2066- " \" Yield unique elements, preserving order. Remember all elements ever "
2067- "seen.\" \n"
2063+ " \" 產生唯一的元素,並保留原始順序。記住所有曾見過的元素。\" \n"
20682064" # unique_everseen('AAAABBBCCDAABBB') → A B C D\n"
20692065" # unique_everseen('ABBcCAD', str.casefold) → A B c D\n"
20702066" seen = set()\n"
@@ -2080,13 +2076,13 @@ msgstr ""
20802076" yield element\n"
20812077"\n"
20822078"def unique(iterable, key=None, reverse=False):\n"
2083- " \" Yield unique elements in sorted order. Supports unhashable inputs. \" \n"
2079+ " \" 產生排序後的不重複元素。支援不可雜湊的輸入。 \" \n"
20842080" # unique([[1, 2], [3, 4], [1, 2]]) → [1, 2] [3, 4]\n"
20852081" sequenced = sorted(iterable, key=key, reverse=reverse)\n"
20862082" return unique_justseen(sequenced, key=key)\n"
20872083"\n"
20882084"def sliding_window(iterable, n):\n"
2089- " \" Collect data into overlapping fixed-length chunks or blocks. \" \n"
2085+ " \" 將資料收集成重疊的固定長度區段或區塊。 \" \n"
20902086" # sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG\n"
20912087" iterator = iter(iterable)\n"
20922088" window = deque(islice(iterator, n - 1), maxlen=n)\n"
@@ -2095,7 +2091,7 @@ msgstr ""
20952091" yield tuple(window)\n"
20962092"\n"
20972093"def grouper(iterable, n, *, incomplete='fill', fillvalue=None):\n"
2098- " \" Collect data into non-overlapping fixed-length chunks or blocks. \" \n"
2094+ " \" 將資料收集成不重疊的固定長度區段或區塊。 \" \n"
20992095" # grouper('ABCDEFG', 3, fillvalue='x') → ABC DEF Gxx\n"
21002096" # grouper('ABCDEFG', 3, incomplete='strict') → ABC DEF ValueError\n"
21012097" # grouper('ABCDEFG', 3, incomplete='ignore') → ABC DEF\n"
@@ -2111,22 +2107,22 @@ msgstr ""
21112107" raise ValueError('Expected fill, strict, or ignore')\n"
21122108"\n"
21132109"def roundrobin(*iterables):\n"
2114- " \" Visit input iterables in a cycle until each is exhausted. \" \n"
2110+ " \" 以循環方式依序輸入可疊代物件,直到全部耗盡。 \" \n"
21152111" # roundrobin('ABC', 'D', 'EF') → A D E B F C\n"
2116- " # Algorithm credited to George Sakkis\n"
2112+ " # 演算法出自 George Sakkis\n"
21172113" iterators = map(iter, iterables)\n"
21182114" for num_active in range(len(iterables), 0, -1):\n"
21192115" iterators = cycle(islice(iterators, num_active))\n"
21202116" yield from map(next, iterators)\n"
21212117"\n"
21222118"def subslices(seq):\n"
2123- " \" Return all contiguous non-empty subslices of a sequence. \" \n"
2119+ " \" 回傳序列的所有連續非空子切片。 \" \n"
21242120" # subslices('ABCD') → A AB ABC ABCD B BC BCD C CD D\n"
21252121" slices = starmap(slice, combinations(range(len(seq) + 1), 2))\n"
21262122" return map(getitem, repeat(seq), slices)\n"
21272123"\n"
21282124"def iter_index(iterable, value, start=0, stop=None):\n"
2129- " \" Return indices where a value occurs in a sequence or iterable. \" \n"
2125+ " \" 回傳在序列或可疊代物件中某值出現的索引位置。 \" \n"
21302126" # iter_index('AABCADEAF', 'A') → 0 1 4 7\n"
21312127" seq_index = getattr(iterable, 'index', None)\n"
21322128" if seq_index is None:\n"
@@ -2143,8 +2139,8 @@ msgstr ""
21432139" i += 1\n"
21442140"\n"
21452141"def iter_except(function, exception, first=None):\n"
2146- " \" Convert a call-until-exception interface to an iterator interface. \" \n"
2147- " # iter_except(d.popitem, KeyError) → non-blocking dictionary iterator \n"
2142+ " \" 將一個 call-until-exception 轉換為疊代器介面。 \" \n"
2143+ " # iter_except(d.popitem, KeyError) → 非阻塞的字典疊代器 \n"
21482144" with suppress(exception):\n"
21492145" if first is not None:\n"
21502146" yield first()\n"
@@ -2282,74 +2278,73 @@ msgid ""
22822278" return n"
22832279msgstr ""
22842280"def multinomial(*counts):\n"
2285- " \" Number of distinct arrangements of a multiset. \" \n"
2281+ " \" 多重集合的不同排列數。 \" \n"
22862282" # Counter('abracadabra').values() → 5 2 2 1 1\n"
22872283" # multinomial(5, 2, 2, 1, 1) → 83160\n"
22882284" return prod(map(comb, accumulate(counts), counts))\n"
22892285"\n"
22902286"def powerset(iterable):\n"
2291- " \" Subsequences of the iterable from shortest to longest. \" \n"
2287+ " \" 來自可疊代物件的子序列,從最短到最長。 \" \n"
22922288" # powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)\n"
22932289" s = list(iterable)\n"
22942290" return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))\n"
22952291"\n"
22962292"def sum_of_squares(iterable):\n"
2297- " \" Add up the squares of the input values. \" \n"
2293+ " \" 將輸入值的平方加總。 \" \n"
22982294" # sum_of_squares([10, 20, 30]) → 1400\n"
22992295" return sumprod(*tee(iterable))\n"
23002296"\n"
23012297"def reshape(matrix, columns):\n"
2302- " \" Reshape a 2-D matrix to have a given number of columns. \" \n"
2298+ " \" 將 2 維矩陣重新塑形為指定的行數。 \" \n"
23032299" # reshape([(0, 1), (2, 3), (4, 5)], 3) → (0, 1, 2), (3, 4, 5)\n"
23042300" return batched(chain.from_iterable(matrix), columns, strict=True)\n"
23052301"\n"
23062302"def transpose(matrix):\n"
2307- " \" Swap the rows and columns of a 2-D matrix. \" \n"
2303+ " \" 交換 2 維矩陣的列和行。 \" \n"
23082304" # transpose([(1, 2, 3), (11, 22, 33)]) → (1, 11) (2, 22) (3, 33)\n"
23092305" return zip(*matrix, strict=True)\n"
23102306"\n"
23112307"def matmul(m1, m2):\n"
2312- " \" Multiply two matrices. \" \n"
2308+ " \" 矩陣相乘。 \" \n"
23132309" # matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) → (49, 80), (41, 60)\n"
23142310" n = len(m2[0])\n"
23152311" return batched(starmap(sumprod, product(m1, transpose(m2))), n)\n"
23162312"\n"
23172313"def convolve(signal, kernel):\n"
2318- " \"\"\" Discrete linear convolution of two iterables. \n"
2319- " Equivalent to polynomial multiplication. \n"
2314+ " \"\"\" 兩個可疊代物件的離散線性捲積。 \n"
2315+ " 等同於多項式相乘。 \n"
23202316"\n"
2321- " Convolutions are mathematically commutative; however, the inputs are\n"
2322- " evaluated differently. The signal is consumed lazily and can be\n"
2323- " infinite. The kernel is fully consumed before the calculations begin.\n"
2317+ " 在數學上捲積是可交換的;但輸入的處理方式不同。\n"
2318+ " 訊號以惰性方式被讀取,且可以是無限;核心會在計算開始前被全部讀取。\n"
23242319"\n"
2325- " Article: https://betterexplained.com/articles/intuitive-convolution/\n"
2326- " Video: https://www.youtube.com/watch?v=KuXjwB4LzSA\n"
2320+ " 文章: https://betterexplained.com/articles/intuitive-convolution/\n"
2321+ " 影片: https://www.youtube.com/watch?v=KuXjwB4LzSA\n"
23272322" \"\"\" \n"
23282323" # convolve([1, -1, -20], [1, -3]) → 1 -4 -17 60\n"
2329- " # convolve(data, [0.25, 0.25, 0.25, 0.25]) → Moving average (blur) \n"
2330- " # convolve(data, [1/2, 0, -1/2]) → 1st derivative estimate \n"
2331- " # convolve(data, [1, -2, 1]) → 2nd derivative estimate \n"
2324+ " # convolve(data, [0.25, 0.25, 0.25, 0.25]) → 移動平均(模糊) \n"
2325+ " # convolve(data, [1/2, 0, -1/2]) → 一階導數估計 \n"
2326+ " # convolve(data, [1, -2, 1]) → 二階導數估計 \n"
23322327" kernel = tuple(kernel)[::-1]\n"
23332328" n = len(kernel)\n"
23342329" padded_signal = chain(repeat(0, n-1), signal, repeat(0, n-1))\n"
23352330" windowed_signal = sliding_window(padded_signal, n)\n"
23362331" return map(sumprod, repeat(kernel), windowed_signal)\n"
23372332"\n"
23382333"def polynomial_from_roots(roots):\n"
2339- " \"\"\" Compute a polynomial's coefficients from its roots. \n"
2334+ " \"\"\" 由多項式的根計算其係數。 \n"
23402335"\n"
2341- " (x - 5) (x + 4) (x - 3) expands to : x³ -4x² -17x + 60\n"
2336+ " (x - 5) (x + 4) (x - 3) 展開為 : x³ -4x² -17x + 60\n"
23422337" \"\"\" \n"
23432338" # polynomial_from_roots([5, -4, 3]) → [1, -4, -17, 60]\n"
23442339" factors = zip(repeat(1), map(neg, roots))\n"
23452340" return list(reduce(convolve, factors, [1]))\n"
23462341"\n"
23472342"def polynomial_eval(coefficients, x):\n"
2348- " \"\"\" Evaluate a polynomial at a specific value. \n"
2343+ " \"\"\" 在指定值計算多項式的值。 \n"
23492344"\n"
2350- " Computes with better numeric stability than Horner's method. \n"
2345+ " 此方法在數值穩定性上比 Horner 方法更好。 \n"
23512346" \"\"\" \n"
2352- " # Evaluate x³ -4x² -17x + 60 at x = 5\n"
2347+ " # 計算 x³ -4x² -17x + 60 在 x = 5\n"
23532348" # polynomial_eval([1, -4, -17, 60], x=5) → 0\n"
23542349" n = len(coefficients)\n"
23552350" if not n:\n"
@@ -2358,7 +2353,7 @@ msgstr ""
23582353" return sumprod(coefficients, powers)\n"
23592354"\n"
23602355"def polynomial_derivative(coefficients):\n"
2361- " \"\"\" Compute the first derivative of a polynomial. \n"
2356+ " \"\"\" 計算多項式的一階導數。 \n"
23622357"\n"
23632358" f(x) = x³ -4x² -17x + 60\n"
23642359" f'(x) = 3x² -8x -17\n"
@@ -2369,7 +2364,7 @@ msgstr ""
23692364" return list(map(mul, coefficients, powers))\n"
23702365"\n"
23712366"def sieve(n):\n"
2372- " \" Primes less than n. \" \n"
2367+ " \" 小於 n 的質數。 \" \n"
23732368" # sieve(30) → 2 3 5 7 11 13 17 19 23 29\n"
23742369" if n > 2:\n"
23752370" yield 2\n"
@@ -2379,7 +2374,7 @@ msgstr ""
23792374" yield from iter_index(data, 1, start=3)\n"
23802375"\n"
23812376"def factor(n):\n"
2382- " \" Prime factors of n. \" \n"
2377+ " \" n 的質因數。 \" \n"
23832378" # factor(99) → 3 3 11\n"
23842379" # factor(1_000_000_000_000_007) → 47 59 360620266859\n"
23852380" # factor(1_000_000_000_000_403) → 1000000000000403\n"
@@ -2393,14 +2388,14 @@ msgstr ""
23932388" yield n\n"
23942389"\n"
23952390"def is_prime(n):\n"
2396- " \" Return True if n is prime. \" \n"
2391+ " \" 回傳 True,若 n 為質數。 \" \n"
23972392" # is_prime(1_000_000_000_000_403) → True\n"
23982393" return n > 1 and next(factor(n)) == n\n"
23992394"\n"
24002395"def totient(n):\n"
2401- " \" Count of natural numbers up to n that are coprime to n. \" \n"
2396+ " \" 計算不大於 n 且與 n 互質的自然數個數。 \" \n"
24022397" # https://mathworld.wolfram.com/TotientFunction.html\n"
2403- " # totient(12) → 4 because len([1, 5, 7, 11]) == 4\n"
2398+ " # totient(12) → 4 因爲 len([1, 5, 7, 11]) == 4\n"
24042399" for prime in set(factor(n)):\n"
24052400" n -= n // prime\n"
24062401" return n"
0 commit comments