From a08d0f7541782a63ce725fbd98ec54a10056e9bc Mon Sep 17 00:00:00 2001 From: Keyacom <70766223+Keyacom@users.noreply.github.com> Date: Tue, 4 Oct 2022 19:39:59 +0200 Subject: added file to another branch Added file to another branch because it could interfere with the `main` branch when a pull request is made. This file adds an explanation on sequence slicing. --- bot/resources/tags/slicing.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 bot/resources/tags/slicing.md diff --git a/bot/resources/tags/slicing.md b/bot/resources/tags/slicing.md new file mode 100644 index 000000000..765060949 --- /dev/null +++ b/bot/resources/tags/slicing.md @@ -0,0 +1,31 @@ +--- +aliases: ["slice", "seqslice", "seqslicing", "sequence-slice", "sequence-slicing"] +embed: + title: "Sequence slicing" +--- +You're trying to get a part of a string, list, or another sequence object, but you don't want to manually increment and concatenate? There comes the need to *slice* it. + +There is a special syntax that can be used to slice a given `some_seq` sequence: `some_seq[i:j:k]`, where `i` is the starting index, `j` is the end index, and `k` is the step, i.e. every how many items should one be kept (the first one is always kept). `i`, `j`, and `k` all must be integers. If any of these values are missing, they're assumed as `some_seq[0:len(some_seq):1]`. + +To slice something, the brackets must have at least at least a colon (cannot be empty). Using just `[:]` or `[::]` (without any numbers) will return a *copy* of the iterable if it's a `list` or a `bytearray`, reducing the need for the `copy()` method. + +**Examples** +```py +>>> l = [1, 2, 3, 4] +>>> l[2:] +[3, 4] +>>> l[:2] +[1, 2] +>>> l[::-1] +[4, 3, 2, 1] +>>> l[:] +[1, 2, 3, 4] +>>> l[::2] +[1, 3] +``` +Using `some_list[::-1]` is the same as `list(reversed(some_list))`. Just like in regular sequence subscriptions, negative integers may be used. + +**Notes** +• If the start index is greater than the end index, the resulting sequence will be empty. +• The number of items before applying the step can be calculated as `n = j - i`. +• The number of items after applying the step is `n / k`, rounded down, but cannot be less than 1, unless `n` is exactly 0. -- cgit v1.2.3 From eff37136806e4058b372e80e263d3d24636cc687 Mon Sep 17 00:00:00 2001 From: mbaruh Date: Fri, 28 Oct 2022 00:46:47 +0300 Subject: Bump PSQL version in docker-compose --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index be7370d6b..bc53c482b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,7 @@ services: postgres: << : *logging << : *restart_policy - image: postgres:13-alpine + image: postgres:15-alpine environment: POSTGRES_DB: pysite POSTGRES_PASSWORD: pysite -- cgit v1.2.3 From d4e162c6fea3e4e3ecb8de1c2854112a4e70557e Mon Sep 17 00:00:00 2001 From: Keyacom <70766223+Keyacom@users.noreply.github.com> Date: Sun, 30 Oct 2022 22:28:16 +0100 Subject: Update slicing.md Did things according to what was proposed by @swfarnsworth, and the following: - Fixed the REPL example (assignments never return) - The word "slicing" at the start was not capitalized in the proposal, even though it started a sentence. I fixed that. --- bot/resources/tags/slicing.md | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/bot/resources/tags/slicing.md b/bot/resources/tags/slicing.md index 765060949..5e28dbb52 100644 --- a/bot/resources/tags/slicing.md +++ b/bot/resources/tags/slicing.md @@ -3,29 +3,24 @@ aliases: ["slice", "seqslice", "seqslicing", "sequence-slice", "sequence-slicing embed: title: "Sequence slicing" --- -You're trying to get a part of a string, list, or another sequence object, but you don't want to manually increment and concatenate? There comes the need to *slice* it. - -There is a special syntax that can be used to slice a given `some_seq` sequence: `some_seq[i:j:k]`, where `i` is the starting index, `j` is the end index, and `k` is the step, i.e. every how many items should one be kept (the first one is always kept). `i`, `j`, and `k` all must be integers. If any of these values are missing, they're assumed as `some_seq[0:len(some_seq):1]`. - -To slice something, the brackets must have at least at least a colon (cannot be empty). Using just `[:]` or `[::]` (without any numbers) will return a *copy* of the iterable if it's a `list` or a `bytearray`, reducing the need for the `copy()` method. +*Slicing* is a way of accessing a part of a sequence by specifying a start, stop, and step. As with normal indexing, negative numbers can be used to count backwards. **Examples** ```py ->>> l = [1, 2, 3, 4] ->>> l[2:] -[3, 4] ->>> l[:2] -[1, 2] ->>> l[::-1] -[4, 3, 2, 1] ->>> l[:] -[1, 2, 3, 4] ->>> l[::2] -[1, 3] +>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] +>>> letters +['a', 'b', 'c', 'd', 'e', 'f', 'g'] +>>> letters[2:] # from element 2 to the end +['c', 'd', 'e', 'f', 'g'] +>>> letters[:4] # up to element 4 +['a', 'b', 'c', 'd'] +>>> letters[3:5] # elements 3 and 4 -- the right bound is not included +['d', 'e'] +>>> letters[2:-1:2] # Every other element between 2 and the last +['c', 'e'] +>>> letters[::-1] # The whole list in reverse +['g', 'f', 'e', 'd', 'c', 'b', 'a'] +>>> words = "Hello world!" +>>> words[2:7] # Strings are also sequences +"llo w" ``` -Using `some_list[::-1]` is the same as `list(reversed(some_list))`. Just like in regular sequence subscriptions, negative integers may be used. - -**Notes** -• If the start index is greater than the end index, the resulting sequence will be empty. -• The number of items before applying the step can be calculated as `n = j - i`. -• The number of items after applying the step is `n / k`, rounded down, but cannot be less than 1, unless `n` is exactly 0. -- cgit v1.2.3 From fb8ad22df1ef5e96bf0af20fdce4d681f8e5c5ab Mon Sep 17 00:00:00 2001 From: Keyacom <70766223+Keyacom@users.noreply.github.com> Date: Mon, 31 Oct 2022 09:11:05 +0100 Subject: Simplify REPL example --- bot/resources/tags/slicing.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/bot/resources/tags/slicing.md b/bot/resources/tags/slicing.md index 5e28dbb52..717fc46b7 100644 --- a/bot/resources/tags/slicing.md +++ b/bot/resources/tags/slicing.md @@ -8,8 +8,6 @@ embed: **Examples** ```py >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] ->>> letters -['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters[2:] # from element 2 to the end ['c', 'd', 'e', 'f', 'g'] >>> letters[:4] # up to element 4 -- cgit v1.2.3