From 5cfc314524a1ffec2f1f0f980e84e8c71356cae3 Mon Sep 17 00:00:00 2001 From: Hsiang-Jen Li Date: Wed, 20 Aug 2025 22:54:24 +0800 Subject: [PATCH 1/6] translate --- library/itertools.po | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/itertools.po b/library/itertools.po index da0c9a2e0f..3ba7c8cf8e 100644 --- a/library/itertools.po +++ b/library/itertools.po @@ -1188,6 +1188,8 @@ msgid "" "If the input is an iterator, then fully consuming the *islice* advances the " "input iterator by ``max(start, stop)`` steps regardless of the *step* value." msgstr "" +"若輸入為疊代器,則完整耗盡 *islice* 會使輸入的疊代器向前移動 ``max(start, " +"stop)`` 步,與 *step* 的值無關。" #: ../../library/itertools.rst:513 msgid "Return successive overlapping pairs taken from the input *iterable*." @@ -1332,6 +1334,8 @@ msgid "" "`Cartesian product `_ of " "the input iterables." msgstr "" +"輸入可疊代物的 `笛卡爾乘積 `_" #: ../../library/itertools.rst:592 msgid "" From 9c62a69ff2354c6acec2c379ae74f2c575a20757 Mon Sep 17 00:00:00 2001 From: Hsiang-Jen Li Date: Wed, 20 Aug 2025 23:11:18 +0800 Subject: [PATCH 2/6] translate --- library/itertools.po | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/itertools.po b/library/itertools.po index 3ba7c8cf8e..2b14ebb9dd 100644 --- a/library/itertools.po +++ b/library/itertools.po @@ -1608,7 +1608,7 @@ msgstr "" #: ../../library/itertools.rst:734 msgid "The flattening property makes tee iterators efficiently peekable:" -msgstr "" +msgstr "展平特性讓 tee 疊代器具備高效的預覽能力:" #: ../../library/itertools.rst:736 msgid "" From 6c0befe242cf1f0bd503ee3b4c53c8a9f89e1743 Mon Sep 17 00:00:00 2001 From: Hsiang-Jen Li Date: Wed, 20 Aug 2025 23:14:16 +0800 Subject: [PATCH 3/6] translate --- library/itertools.po | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/itertools.po b/library/itertools.po index 2b14ebb9dd..7a429d5b5a 100644 --- a/library/itertools.po +++ b/library/itertools.po @@ -1617,6 +1617,10 @@ msgid "" " [forked_iterator] = tee(tee_iterator, 1)\n" " return next(forked_iterator)" msgstr "" +"def lookahead(tee_iterator):\n" +" \"回傳下一個值,但不推進輸入\"\n" +" [forked_iterator] = tee(tee_iterator, 1)\n" +" return next(forked_iterator)" #: ../../library/itertools.rst:743 msgid "" From 73b7d30ca6a4509b298c7552f1877c8cfdc876c9 Mon Sep 17 00:00:00 2001 From: Hsiang-Jen Li Date: Wed, 20 Aug 2025 23:46:23 +0800 Subject: [PATCH 4/6] translate --- library/itertools.po | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/itertools.po b/library/itertools.po index 7a429d5b5a..01ea90d03a 100644 --- a/library/itertools.po +++ b/library/itertools.po @@ -1605,6 +1605,10 @@ msgid "" "`tee` calls to share the same underlying data chain and to have a single " "update step rather than a chain of calls." msgstr "" +"當輸入的 *iterable* 已經是一個 tee 疊代物件時,回傳的 tuple(元組)中所有成員" +"都會被建立,就如同它們是由上游的 :func:`tee` 呼叫所產生的一樣。這個「展平步" +"驟 (flattening step)」讓巢狀的 :func:`tee` 呼叫能共享相同的底層資料鏈,並以單" +"一的更新步驟取代一連串的呼叫。" #: ../../library/itertools.rst:734 msgid "The flattening property makes tee iterators efficiently peekable:" From f8674857845e97604989b13f4add9edaf6d4c69c Mon Sep 17 00:00:00 2001 From: Hsiang-Jen Li Date: Wed, 20 Aug 2025 23:56:49 +0800 Subject: [PATCH 5/6] original --- library/itertools.po | 292 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) diff --git a/library/itertools.po b/library/itertools.po index 01ea90d03a..fa2e3edaa0 100644 --- a/library/itertools.po +++ b/library/itertools.po @@ -1981,6 +1981,175 @@ msgid "" " while True:\n" " yield function()" msgstr "" +"from collections import Counter, deque\n" +"from contextlib import suppress\n" +"from functools import reduce\n" +"from math import comb, prod, sumprod, isqrt\n" +"from operator import itemgetter, getitem, mul, neg\n" +"\n" +"def take(n, iterable):\n" +" \"Return first n items of the iterable as a list.\"\n" +" return list(islice(iterable, n))\n" +"\n" +"def prepend(value, iterable):\n" +" \"Prepend a single value in front of an iterable.\"\n" +" # prepend(1, [2, 3, 4]) → 1 2 3 4\n" +" return chain([value], iterable)\n" +"\n" +"def tabulate(function, start=0):\n" +" \"Return function(0), function(1), ...\"\n" +" return map(function, count(start))\n" +"\n" +"def repeatfunc(function, times=None, *args):\n" +" \"Repeat calls to a function with specified arguments.\"\n" +" if times is None:\n" +" return starmap(function, repeat(args))\n" +" return starmap(function, repeat(args, times))\n" +"\n" +"def flatten(list_of_lists):\n" +" \"Flatten one level of nesting.\"\n" +" return chain.from_iterable(list_of_lists)\n" +"\n" +"def ncycles(iterable, n):\n" +" \"Returns the sequence elements n times.\"\n" +" return chain.from_iterable(repeat(tuple(iterable), n))\n" +"\n" +"def loops(n):\n" +" \"Loop n times. Like range(n) but without creating integers.\"\n" +" # for _ in loops(100): ...\n" +" return repeat(None, n)\n" +"\n" +"def tail(n, iterable):\n" +" \"Return an iterator over the last n items.\"\n" +" # tail(3, 'ABCDEFG') → E F G\n" +" return iter(deque(iterable, maxlen=n))\n" +"\n" +"def consume(iterator, n=None):\n" +" \"Advance the iterator n-steps ahead. If n is None, consume entirely.\"\n" +" # Use functions that consume iterators at C speed.\n" +" if n is None:\n" +" deque(iterator, maxlen=0)\n" +" else:\n" +" next(islice(iterator, n, n), None)\n" +"\n" +"def nth(iterable, n, default=None):\n" +" \"Returns the nth item or a default value.\"\n" +" return next(islice(iterable, n, None), default)\n" +"\n" +"def quantify(iterable, predicate=bool):\n" +" \"Given a predicate that returns True or False, count the True results." +"\"\n" +" return sum(map(predicate, iterable))\n" +"\n" +"def first_true(iterable, default=False, predicate=None):\n" +" \"Returns the first true value or the *default* if there is no true " +"value.\"\n" +" # first_true([a,b,c], x) → a or b or c or x\n" +" # first_true([a,b], x, f) → a if f(a) else b if f(b) else x\n" +" return next(filter(predicate, iterable), default)\n" +"\n" +"def all_equal(iterable, key=None):\n" +" \"Returns True if all the elements are equal to each other.\"\n" +" # all_equal('4٤௪౪໔', key=int) → True\n" +" return len(take(2, groupby(iterable, key))) <= 1\n" +"\n" +"def unique_justseen(iterable, key=None):\n" +" \"Yield unique elements, preserving order. Remember only the element " +"just seen.\"\n" +" # unique_justseen('AAAABBBCCDAABBB') → A B C D A B\n" +" # unique_justseen('ABBcCAD', str.casefold) → A B c A D\n" +" if key is None:\n" +" return map(itemgetter(0), groupby(iterable))\n" +" return map(next, map(itemgetter(1), groupby(iterable, key)))\n" +"\n" +"def unique_everseen(iterable, key=None):\n" +" \"Yield unique elements, preserving order. Remember all elements ever " +"seen.\"\n" +" # unique_everseen('AAAABBBCCDAABBB') → A B C D\n" +" # unique_everseen('ABBcCAD', str.casefold) → A B c D\n" +" seen = set()\n" +" if key is None:\n" +" for element in filterfalse(seen.__contains__, iterable):\n" +" seen.add(element)\n" +" yield element\n" +" else:\n" +" for element in iterable:\n" +" k = key(element)\n" +" if k not in seen:\n" +" seen.add(k)\n" +" yield element\n" +"\n" +"def unique(iterable, key=None, reverse=False):\n" +" \"Yield unique elements in sorted order. Supports unhashable inputs.\"\n" +" # unique([[1, 2], [3, 4], [1, 2]]) → [1, 2] [3, 4]\n" +" sequenced = sorted(iterable, key=key, reverse=reverse)\n" +" return unique_justseen(sequenced, key=key)\n" +"\n" +"def sliding_window(iterable, n):\n" +" \"Collect data into overlapping fixed-length chunks or blocks.\"\n" +" # sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG\n" +" iterator = iter(iterable)\n" +" window = deque(islice(iterator, n - 1), maxlen=n)\n" +" for x in iterator:\n" +" window.append(x)\n" +" yield tuple(window)\n" +"\n" +"def grouper(iterable, n, *, incomplete='fill', fillvalue=None):\n" +" \"Collect data into non-overlapping fixed-length chunks or blocks.\"\n" +" # grouper('ABCDEFG', 3, fillvalue='x') → ABC DEF Gxx\n" +" # grouper('ABCDEFG', 3, incomplete='strict') → ABC DEF ValueError\n" +" # grouper('ABCDEFG', 3, incomplete='ignore') → ABC DEF\n" +" iterators = [iter(iterable)] * n\n" +" match incomplete:\n" +" case 'fill':\n" +" return zip_longest(*iterators, fillvalue=fillvalue)\n" +" case 'strict':\n" +" return zip(*iterators, strict=True)\n" +" case 'ignore':\n" +" return zip(*iterators)\n" +" case _:\n" +" raise ValueError('Expected fill, strict, or ignore')\n" +"\n" +"def roundrobin(*iterables):\n" +" \"Visit input iterables in a cycle until each is exhausted.\"\n" +" # roundrobin('ABC', 'D', 'EF') → A D E B F C\n" +" # Algorithm credited to George Sakkis\n" +" iterators = map(iter, iterables)\n" +" for num_active in range(len(iterables), 0, -1):\n" +" iterators = cycle(islice(iterators, num_active))\n" +" yield from map(next, iterators)\n" +"\n" +"def subslices(seq):\n" +" \"Return all contiguous non-empty subslices of a sequence.\"\n" +" # subslices('ABCD') → A AB ABC ABCD B BC BCD C CD D\n" +" slices = starmap(slice, combinations(range(len(seq) + 1), 2))\n" +" return map(getitem, repeat(seq), slices)\n" +"\n" +"def iter_index(iterable, value, start=0, stop=None):\n" +" \"Return indices where a value occurs in a sequence or iterable.\"\n" +" # iter_index('AABCADEAF', 'A') → 0 1 4 7\n" +" seq_index = getattr(iterable, 'index', None)\n" +" if seq_index is None:\n" +" iterator = islice(iterable, start, stop)\n" +" for i, element in enumerate(iterator, start):\n" +" if element is value or element == value:\n" +" yield i\n" +" else:\n" +" stop = len(iterable) if stop is None else stop\n" +" i = start\n" +" with suppress(ValueError):\n" +" while True:\n" +" yield (i := seq_index(value, i, stop))\n" +" i += 1\n" +"\n" +"def iter_except(function, exception, first=None):\n" +" \"Convert a call-until-exception interface to an iterator interface.\"\n" +" # iter_except(d.popitem, KeyError) → non-blocking dictionary iterator\n" +" with suppress(exception):\n" +" if first is not None:\n" +" yield first()\n" +" while True:\n" +" yield function()" #: ../../library/itertools.rst:1008 msgid "The following recipes have a more mathematical flavor:" @@ -2112,3 +2281,126 @@ msgid "" " n -= n // prime\n" " return n" msgstr "" +"def multinomial(*counts):\n" +" \"Number of distinct arrangements of a multiset.\"\n" +" # Counter('abracadabra').values() → 5 2 2 1 1\n" +" # multinomial(5, 2, 2, 1, 1) → 83160\n" +" return prod(map(comb, accumulate(counts), counts))\n" +"\n" +"def powerset(iterable):\n" +" \"Subsequences of the iterable from shortest to longest.\"\n" +" # powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)\n" +" s = list(iterable)\n" +" return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))\n" +"\n" +"def sum_of_squares(iterable):\n" +" \"Add up the squares of the input values.\"\n" +" # sum_of_squares([10, 20, 30]) → 1400\n" +" return sumprod(*tee(iterable))\n" +"\n" +"def reshape(matrix, columns):\n" +" \"Reshape a 2-D matrix to have a given number of columns.\"\n" +" # reshape([(0, 1), (2, 3), (4, 5)], 3) → (0, 1, 2), (3, 4, 5)\n" +" return batched(chain.from_iterable(matrix), columns, strict=True)\n" +"\n" +"def transpose(matrix):\n" +" \"Swap the rows and columns of a 2-D matrix.\"\n" +" # transpose([(1, 2, 3), (11, 22, 33)]) → (1, 11) (2, 22) (3, 33)\n" +" return zip(*matrix, strict=True)\n" +"\n" +"def matmul(m1, m2):\n" +" \"Multiply two matrices.\"\n" +" # matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) → (49, 80), (41, 60)\n" +" n = len(m2[0])\n" +" return batched(starmap(sumprod, product(m1, transpose(m2))), n)\n" +"\n" +"def convolve(signal, kernel):\n" +" \"\"\"Discrete linear convolution of two iterables.\n" +" Equivalent to polynomial multiplication.\n" +"\n" +" Convolutions are mathematically commutative; however, the inputs are\n" +" evaluated differently. The signal is consumed lazily and can be\n" +" infinite. The kernel is fully consumed before the calculations begin.\n" +"\n" +" Article: https://betterexplained.com/articles/intuitive-convolution/\n" +" Video: https://www.youtube.com/watch?v=KuXjwB4LzSA\n" +" \"\"\"\n" +" # convolve([1, -1, -20], [1, -3]) → 1 -4 -17 60\n" +" # convolve(data, [0.25, 0.25, 0.25, 0.25]) → Moving average (blur)\n" +" # convolve(data, [1/2, 0, -1/2]) → 1st derivative estimate\n" +" # convolve(data, [1, -2, 1]) → 2nd derivative estimate\n" +" kernel = tuple(kernel)[::-1]\n" +" n = len(kernel)\n" +" padded_signal = chain(repeat(0, n-1), signal, repeat(0, n-1))\n" +" windowed_signal = sliding_window(padded_signal, n)\n" +" return map(sumprod, repeat(kernel), windowed_signal)\n" +"\n" +"def polynomial_from_roots(roots):\n" +" \"\"\"Compute a polynomial's coefficients from its roots.\n" +"\n" +" (x - 5) (x + 4) (x - 3) expands to: x³ -4x² -17x + 60\n" +" \"\"\"\n" +" # polynomial_from_roots([5, -4, 3]) → [1, -4, -17, 60]\n" +" factors = zip(repeat(1), map(neg, roots))\n" +" return list(reduce(convolve, factors, [1]))\n" +"\n" +"def polynomial_eval(coefficients, x):\n" +" \"\"\"Evaluate a polynomial at a specific value.\n" +"\n" +" Computes with better numeric stability than Horner's method.\n" +" \"\"\"\n" +" # Evaluate x³ -4x² -17x + 60 at x = 5\n" +" # polynomial_eval([1, -4, -17, 60], x=5) → 0\n" +" n = len(coefficients)\n" +" if not n:\n" +" return type(x)(0)\n" +" powers = map(pow, repeat(x), reversed(range(n)))\n" +" return sumprod(coefficients, powers)\n" +"\n" +"def polynomial_derivative(coefficients):\n" +" \"\"\"Compute the first derivative of a polynomial.\n" +"\n" +" f(x) = x³ -4x² -17x + 60\n" +" f'(x) = 3x² -8x -17\n" +" \"\"\"\n" +" # polynomial_derivative([1, -4, -17, 60]) → [3, -8, -17]\n" +" n = len(coefficients)\n" +" powers = reversed(range(1, n))\n" +" return list(map(mul, coefficients, powers))\n" +"\n" +"def sieve(n):\n" +" \"Primes less than n.\"\n" +" # sieve(30) → 2 3 5 7 11 13 17 19 23 29\n" +" if n > 2:\n" +" yield 2\n" +" data = bytearray((0, 1)) * (n // 2)\n" +" for p in iter_index(data, 1, start=3, stop=isqrt(n) + 1):\n" +" data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))\n" +" yield from iter_index(data, 1, start=3)\n" +"\n" +"def factor(n):\n" +" \"Prime factors of n.\"\n" +" # factor(99) → 3 3 11\n" +" # factor(1_000_000_000_000_007) → 47 59 360620266859\n" +" # factor(1_000_000_000_000_403) → 1000000000000403\n" +" for prime in sieve(isqrt(n) + 1):\n" +" while not n % prime:\n" +" yield prime\n" +" n //= prime\n" +" if n == 1:\n" +" return\n" +" if n > 1:\n" +" yield n\n" +"\n" +"def is_prime(n):\n" +" \"Return True if n is prime.\"\n" +" # is_prime(1_000_000_000_000_403) → True\n" +" return n > 1 and next(factor(n)) == n\n" +"\n" +"def totient(n):\n" +" \"Count of natural numbers up to n that are coprime to n.\"\n" +" # https://mathworld.wolfram.com/TotientFunction.html\n" +" # totient(12) → 4 because len([1, 5, 7, 11]) == 4\n" +" for prime in set(factor(n)):\n" +" n -= n // prime\n" +" return n" From 5eccca862c0c1e684cb9e8fcaef2579482d2df50 Mon Sep 17 00:00:00 2001 From: Hsiang-Jen Li Date: Thu, 21 Aug 2025 20:34:28 +0800 Subject: [PATCH 6/6] translate --- library/itertools.po | 107 +++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 56 deletions(-) diff --git a/library/itertools.po b/library/itertools.po index fa2e3edaa0..5e56a8805c 100644 --- a/library/itertools.po +++ b/library/itertools.po @@ -1988,74 +1988,71 @@ msgstr "" "from operator import itemgetter, getitem, mul, neg\n" "\n" "def take(n, iterable):\n" -" \"Return first n items of the iterable as a list.\"\n" +" \"回傳可疊代物件的前 n 個元素為串列。\"\n" " return list(islice(iterable, n))\n" "\n" "def prepend(value, iterable):\n" -" \"Prepend a single value in front of an iterable.\"\n" +" \"在可疊代物件前插入單一值。\"\n" " # prepend(1, [2, 3, 4]) → 1 2 3 4\n" " return chain([value], iterable)\n" "\n" "def tabulate(function, start=0):\n" -" \"Return function(0), function(1), ...\"\n" +" \"回傳 function(0), function(1), ...\"\n" " return map(function, count(start))\n" "\n" "def repeatfunc(function, times=None, *args):\n" -" \"Repeat calls to a function with specified arguments.\"\n" +" \"重複呼叫一個帶指定引數的函式。\"\n" " if times is None:\n" " return starmap(function, repeat(args))\n" " return starmap(function, repeat(args, times))\n" "\n" "def flatten(list_of_lists):\n" -" \"Flatten one level of nesting.\"\n" +" \"將巢狀結構攤平一層。\"\n" " return chain.from_iterable(list_of_lists)\n" "\n" "def ncycles(iterable, n):\n" -" \"Returns the sequence elements n times.\"\n" +" \"回傳序列的元素重複 n 次。\"\n" " return chain.from_iterable(repeat(tuple(iterable), n))\n" "\n" "def loops(n):\n" -" \"Loop n times. Like range(n) but without creating integers.\"\n" +" \"執行 n 次的迴圈。類似 range(n) 但不建立整數序列。\"\n" " # for _ in loops(100): ...\n" " return repeat(None, n)\n" "\n" "def tail(n, iterable):\n" -" \"Return an iterator over the last n items.\"\n" +" \"回傳一個疊代器,疊代最後 n 個元素。\"\n" " # tail(3, 'ABCDEFG') → E F G\n" " return iter(deque(iterable, maxlen=n))\n" "\n" "def consume(iterator, n=None):\n" -" \"Advance the iterator n-steps ahead. If n is None, consume entirely.\"\n" -" # Use functions that consume iterators at C speed.\n" +" \"將疊代器往前推進 n 步。如果 n 為 None,則完全消耗。\"\n" +" # 使用以 C 語言的速度消耗疊代器的函式。\n" " if n is None:\n" " deque(iterator, maxlen=0)\n" " else:\n" " next(islice(iterator, n, n), None)\n" "\n" "def nth(iterable, n, default=None):\n" -" \"Returns the nth item or a default value.\"\n" +" \"回傳第 n 個元素或預設值。\"\n" " return next(islice(iterable, n, None), default)\n" "\n" "def quantify(iterable, predicate=bool):\n" -" \"Given a predicate that returns True or False, count the True results." -"\"\n" +" \"給定一個回傳 True 或 False 的判斷函式,計算為 True 的結果。\"\n" " return sum(map(predicate, iterable))\n" "\n" "def first_true(iterable, default=False, predicate=None):\n" -" \"Returns the first true value or the *default* if there is no true " -"value.\"\n" +" \"回傳第一個為 true 的值,若無則回傳*預設值*。\"\n" " # first_true([a,b,c], x) → a or b or c or x\n" " # first_true([a,b], x, f) → a if f(a) else b if f(b) else x\n" " return next(filter(predicate, iterable), default)\n" "\n" "def all_equal(iterable, key=None):\n" -" \"Returns True if all the elements are equal to each other.\"\n" +" \"回傳 True,如果所有元素兩兩相等。\"\n" " # all_equal('4٤௪౪໔', key=int) → True\n" " return len(take(2, groupby(iterable, key))) <= 1\n" "\n" "def unique_justseen(iterable, key=None):\n" -" \"Yield unique elements, preserving order. Remember only the element " -"just seen.\"\n" +" \"產生唯一的元素,並保留原始順序。只記住剛看見的元素。\"\n" " # unique_justseen('AAAABBBCCDAABBB') → A B C D A B\n" " # unique_justseen('ABBcCAD', str.casefold) → A B c A D\n" " if key is None:\n" @@ -2063,8 +2060,7 @@ msgstr "" " return map(next, map(itemgetter(1), groupby(iterable, key)))\n" "\n" "def unique_everseen(iterable, key=None):\n" -" \"Yield unique elements, preserving order. Remember all elements ever " -"seen.\"\n" +" \"產生唯一的元素,並保留原始順序。記住所有曾見過的元素。\"\n" " # unique_everseen('AAAABBBCCDAABBB') → A B C D\n" " # unique_everseen('ABBcCAD', str.casefold) → A B c D\n" " seen = set()\n" @@ -2080,13 +2076,13 @@ msgstr "" " yield element\n" "\n" "def unique(iterable, key=None, reverse=False):\n" -" \"Yield unique elements in sorted order. Supports unhashable inputs.\"\n" +" \"產生排序後的不重複元素。支援不可雜湊的輸入。\"\n" " # unique([[1, 2], [3, 4], [1, 2]]) → [1, 2] [3, 4]\n" " sequenced = sorted(iterable, key=key, reverse=reverse)\n" " return unique_justseen(sequenced, key=key)\n" "\n" "def sliding_window(iterable, n):\n" -" \"Collect data into overlapping fixed-length chunks or blocks.\"\n" +" \"將資料收集成重疊的固定長度區段或區塊。\"\n" " # sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG\n" " iterator = iter(iterable)\n" " window = deque(islice(iterator, n - 1), maxlen=n)\n" @@ -2095,7 +2091,7 @@ msgstr "" " yield tuple(window)\n" "\n" "def grouper(iterable, n, *, incomplete='fill', fillvalue=None):\n" -" \"Collect data into non-overlapping fixed-length chunks or blocks.\"\n" +" \"將資料收集成不重疊的固定長度區段或區塊。\"\n" " # grouper('ABCDEFG', 3, fillvalue='x') → ABC DEF Gxx\n" " # grouper('ABCDEFG', 3, incomplete='strict') → ABC DEF ValueError\n" " # grouper('ABCDEFG', 3, incomplete='ignore') → ABC DEF\n" @@ -2111,22 +2107,22 @@ msgstr "" " raise ValueError('Expected fill, strict, or ignore')\n" "\n" "def roundrobin(*iterables):\n" -" \"Visit input iterables in a cycle until each is exhausted.\"\n" +" \"以循環方式依序輸入可疊代物件,直到全部耗盡。\"\n" " # roundrobin('ABC', 'D', 'EF') → A D E B F C\n" -" # Algorithm credited to George Sakkis\n" +" # 演算法出自 George Sakkis\n" " iterators = map(iter, iterables)\n" " for num_active in range(len(iterables), 0, -1):\n" " iterators = cycle(islice(iterators, num_active))\n" " yield from map(next, iterators)\n" "\n" "def subslices(seq):\n" -" \"Return all contiguous non-empty subslices of a sequence.\"\n" +" \"回傳序列的所有連續非空子切片。\"\n" " # subslices('ABCD') → A AB ABC ABCD B BC BCD C CD D\n" " slices = starmap(slice, combinations(range(len(seq) + 1), 2))\n" " return map(getitem, repeat(seq), slices)\n" "\n" "def iter_index(iterable, value, start=0, stop=None):\n" -" \"Return indices where a value occurs in a sequence or iterable.\"\n" +" \"回傳在序列或可疊代物件中某值出現的索引位置。\"\n" " # iter_index('AABCADEAF', 'A') → 0 1 4 7\n" " seq_index = getattr(iterable, 'index', None)\n" " if seq_index is None:\n" @@ -2143,8 +2139,8 @@ msgstr "" " i += 1\n" "\n" "def iter_except(function, exception, first=None):\n" -" \"Convert a call-until-exception interface to an iterator interface.\"\n" -" # iter_except(d.popitem, KeyError) → non-blocking dictionary iterator\n" +" \"將一個 call-until-exception 轉換為疊代器介面。\"\n" +" # iter_except(d.popitem, KeyError) → 非阻塞的字典疊代器\n" " with suppress(exception):\n" " if first is not None:\n" " yield first()\n" @@ -2282,53 +2278,52 @@ msgid "" " return n" msgstr "" "def multinomial(*counts):\n" -" \"Number of distinct arrangements of a multiset.\"\n" +" \"多重集合的不同排列數。\"\n" " # Counter('abracadabra').values() → 5 2 2 1 1\n" " # multinomial(5, 2, 2, 1, 1) → 83160\n" " return prod(map(comb, accumulate(counts), counts))\n" "\n" "def powerset(iterable):\n" -" \"Subsequences of the iterable from shortest to longest.\"\n" +" \"來自可疊代物件的子序列,從最短到最長。\"\n" " # powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)\n" " s = list(iterable)\n" " return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))\n" "\n" "def sum_of_squares(iterable):\n" -" \"Add up the squares of the input values.\"\n" +" \"將輸入值的平方加總。\"\n" " # sum_of_squares([10, 20, 30]) → 1400\n" " return sumprod(*tee(iterable))\n" "\n" "def reshape(matrix, columns):\n" -" \"Reshape a 2-D matrix to have a given number of columns.\"\n" +" \"將 2 維矩陣重新塑形為指定的行數。\"\n" " # reshape([(0, 1), (2, 3), (4, 5)], 3) → (0, 1, 2), (3, 4, 5)\n" " return batched(chain.from_iterable(matrix), columns, strict=True)\n" "\n" "def transpose(matrix):\n" -" \"Swap the rows and columns of a 2-D matrix.\"\n" +" \"交換 2 維矩陣的列和行。\"\n" " # transpose([(1, 2, 3), (11, 22, 33)]) → (1, 11) (2, 22) (3, 33)\n" " return zip(*matrix, strict=True)\n" "\n" "def matmul(m1, m2):\n" -" \"Multiply two matrices.\"\n" +" \"矩陣相乘。\"\n" " # matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) → (49, 80), (41, 60)\n" " n = len(m2[0])\n" " return batched(starmap(sumprod, product(m1, transpose(m2))), n)\n" "\n" "def convolve(signal, kernel):\n" -" \"\"\"Discrete linear convolution of two iterables.\n" -" Equivalent to polynomial multiplication.\n" +" \"\"\"兩個可疊代物件的離散線性捲積。\n" +" 等同於多項式相乘。\n" "\n" -" Convolutions are mathematically commutative; however, the inputs are\n" -" evaluated differently. The signal is consumed lazily and can be\n" -" infinite. The kernel is fully consumed before the calculations begin.\n" +" 在數學上捲積是可交換的;但輸入的處理方式不同。\n" +" 訊號以惰性方式被讀取,且可以是無限;核心會在計算開始前被全部讀取。\n" "\n" -" Article: https://betterexplained.com/articles/intuitive-convolution/\n" -" Video: https://www.youtube.com/watch?v=KuXjwB4LzSA\n" +" 文章:https://betterexplained.com/articles/intuitive-convolution/\n" +" 影片:https://www.youtube.com/watch?v=KuXjwB4LzSA\n" " \"\"\"\n" " # convolve([1, -1, -20], [1, -3]) → 1 -4 -17 60\n" -" # convolve(data, [0.25, 0.25, 0.25, 0.25]) → Moving average (blur)\n" -" # convolve(data, [1/2, 0, -1/2]) → 1st derivative estimate\n" -" # convolve(data, [1, -2, 1]) → 2nd derivative estimate\n" +" # convolve(data, [0.25, 0.25, 0.25, 0.25]) → 移動平均(模糊)\n" +" # convolve(data, [1/2, 0, -1/2]) → 一階導數估計\n" +" # convolve(data, [1, -2, 1]) → 二階導數估計\n" " kernel = tuple(kernel)[::-1]\n" " n = len(kernel)\n" " padded_signal = chain(repeat(0, n-1), signal, repeat(0, n-1))\n" @@ -2336,20 +2331,20 @@ msgstr "" " return map(sumprod, repeat(kernel), windowed_signal)\n" "\n" "def polynomial_from_roots(roots):\n" -" \"\"\"Compute a polynomial's coefficients from its roots.\n" +" \"\"\"由多項式的根計算其係數。\n" "\n" -" (x - 5) (x + 4) (x - 3) expands to: x³ -4x² -17x + 60\n" +" (x - 5) (x + 4) (x - 3) 展開為: x³ -4x² -17x + 60\n" " \"\"\"\n" " # polynomial_from_roots([5, -4, 3]) → [1, -4, -17, 60]\n" " factors = zip(repeat(1), map(neg, roots))\n" " return list(reduce(convolve, factors, [1]))\n" "\n" "def polynomial_eval(coefficients, x):\n" -" \"\"\"Evaluate a polynomial at a specific value.\n" +" \"\"\"在指定值計算多項式的值。\n" "\n" -" Computes with better numeric stability than Horner's method.\n" +" 此方法在數值穩定性上比 Horner 方法更好。\n" " \"\"\"\n" -" # Evaluate x³ -4x² -17x + 60 at x = 5\n" +" # 計算 x³ -4x² -17x + 60 在 x = 5\n" " # polynomial_eval([1, -4, -17, 60], x=5) → 0\n" " n = len(coefficients)\n" " if not n:\n" @@ -2358,7 +2353,7 @@ msgstr "" " return sumprod(coefficients, powers)\n" "\n" "def polynomial_derivative(coefficients):\n" -" \"\"\"Compute the first derivative of a polynomial.\n" +" \"\"\"計算多項式的一階導數。\n" "\n" " f(x) = x³ -4x² -17x + 60\n" " f'(x) = 3x² -8x -17\n" @@ -2369,7 +2364,7 @@ msgstr "" " return list(map(mul, coefficients, powers))\n" "\n" "def sieve(n):\n" -" \"Primes less than n.\"\n" +" \"小於 n 的質數。\"\n" " # sieve(30) → 2 3 5 7 11 13 17 19 23 29\n" " if n > 2:\n" " yield 2\n" @@ -2379,7 +2374,7 @@ msgstr "" " yield from iter_index(data, 1, start=3)\n" "\n" "def factor(n):\n" -" \"Prime factors of n.\"\n" +" \"n 的質因數。\"\n" " # factor(99) → 3 3 11\n" " # factor(1_000_000_000_000_007) → 47 59 360620266859\n" " # factor(1_000_000_000_000_403) → 1000000000000403\n" @@ -2393,14 +2388,14 @@ msgstr "" " yield n\n" "\n" "def is_prime(n):\n" -" \"Return True if n is prime.\"\n" +" \"回傳 True,若 n 為質數。\"\n" " # is_prime(1_000_000_000_000_403) → True\n" " return n > 1 and next(factor(n)) == n\n" "\n" "def totient(n):\n" -" \"Count of natural numbers up to n that are coprime to n.\"\n" +" \"計算不大於 n 且與 n 互質的自然數個數。\"\n" " # https://mathworld.wolfram.com/TotientFunction.html\n" -" # totient(12) → 4 because len([1, 5, 7, 11]) == 4\n" +" # totient(12) → 4 因爲 len([1, 5, 7, 11]) == 4\n" " for prime in set(factor(n)):\n" " n -= n // prime\n" " return n"