Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python to Intro_Sorted_Sets Module #4862

Merged
merged 25 commits into from
Oct 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
85f0dec
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 25, 2024
ed8ee72
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2024
63623f3
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 27, 2024
18e4f28
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 27, 2024
130db1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2024
a6f6fb3
Merge branch 'master' into py
CryoJS Oct 27, 2024
8cff55d
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 27, 2024
bc4edd7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2024
6f15cf1
Update Intro_Sorted_Sets.mdx
SansPapyrus683 Oct 27, 2024
a24ed0b
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 27, 2024
4a17b5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2024
89925ad
Merge branch 'master' into py
CryoJS Oct 27, 2024
fe11a65
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 27, 2024
c0be548
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2024
52c16ef
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 27, 2024
d03d27d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2024
33a8818
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 27, 2024
36db49f
Merge branch 'master' into py
CryoJS Oct 28, 2024
c68a350
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 28, 2024
8579ef3
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 28, 2024
7857cb3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2024
12b1166
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 28, 2024
d31adae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2024
9a95d1a
Merge branch 'master' into py
CryoJS Oct 29, 2024
4cc8617
Update content/3_Silver/Intro_Sorted_Sets.mdx
CryoJS Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 48 additions & 26 deletions content/3_Silver/Intro_Sorted_Sets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,57 @@ which can only be used once before calling the `next()` method.
<PySection>

In Python, we can use `iter()` to obtain the iterator object of any iterable.
This returns the first element, and using `next()` on the iterator gives you the
next one. Below, a dictionary is used instead of a set because dictionaries keep
order.
Using `next()` on the iterator lets you iterate through the iterable.

```py
nums = {1: None, 7: None, 0: None, 2: None, 4: None}
nums = {1, "three", 0, (1, 3, 5), 4}
iterator = iter(nums)

print(iterator) # <set_iterator object at 0x14765f3eb6c0>
print(next(iterator)) # 1
print(next(iterator)) # 7
print(next(iterator)) # 0
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved
print(next(iterator)) # 2
print(next(iterator)) # 1
print(next(iterator)) # 4
print(next(iterator)) # three
print(next(iterator)) # (1, 3, 5)

# Note:
# The numbers appear ordered when iterating due to the hashing implementation
# Sets are not ordered, so do not expect this behavior
```

<Warning>
Python's iterators are fundamental to iteration and are used in for loops and
tuple unpacking, for example. This is useful when you want lower level control
over your iteration.

As of Python 3.6, dictionaries are ordered by **insertion order**.
You can also use an `OrderedDict` from `collections`, which is built with the goal of being ordered
and thus has more features and slightly different speeds compared to a regular Python dictionary.
In competitive programming however, you will find that the `itertools` library
CryoJS marked this conversation as resolved.
Show resolved Hide resolved
provides more common uses that can save you lots of time. The most useful of
these are shown below.

</Warning>
```py
import itertools

# Iterate through and accumulate with a specified function
nums = [1, 0, 5, 2, -4, 3]
psa = list(itertools.accumulate(nums)) # operator.add is the default
print(psa) # [1, 1, 6, 8, 4, 7]
print(list(itertools.accumulate(nums, min))) # [1, 0, 0, 0, -4, -4]
print(list(itertools.accumulate(nums, max))) # [1, 1, 5, 5, 5, 5]

# Iterate through the perms and combs below
# (1, 2) (1, 3) (2, 3)
print(*itertools.combinations([1, 2, 3], 2))
# (1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)
print(*itertools.combinations_with_replacement([1, 2, 3], 2))
# (1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)
print(*itertools.permutations([1, 2, 3], 2))
# (1, 1) (1, 2) (1, 3) (2, 1) (2, 2) (2, 3) (3, 1) (3, 2) (3, 3)
print(*itertools.product([1, 2, 3], repeat = 2))

# Iterate pairwise
print(*itertools.pairwise("ABCD")) # ('A', 'B') ('B', 'C') ('C', 'D')

# Iterate in an infinite cycle: "A", "B", "C", "A", "B", "C", "A", ...
for letter in itertools.cycle("ABC"): break
```

</PySection>
</LanguageSection>
Expand Down Expand Up @@ -240,10 +268,10 @@ You are not expected to know how to create an AVL tree in USACO.

</Warning>

Since some online judges such as LeetCode include additional libraries, there is also an
implementation of sorted sets included from the `sortedcontainers` library. All
operations shown below are $\mathcal{O}(\log N)$ time, except for its
$\mathcal{O}(N \log N)$ initialization.
Since some online judges such as LeetCode include additional libraries, there is
also an implementation of sorted sets included from the `sortedcontainers`
library. All operations shown below are $\mathcal{O}(\log N)$ time, except for
its $\mathcal{O}(N \log N)$ initialization.
CryoJS marked this conversation as resolved.
Show resolved Hide resolved

```py
from sortedcontainers import SortedSet
Expand Down Expand Up @@ -384,15 +412,9 @@ print(sorted_map[2]) # two
print(sorted_map[4]) # four

# Get all items (key value pairs), keys, or values below

# Output SortedItemsView(SortedDict({1: 'one', 2: 'two', 4: 'four', 5: 'five'}))
print(sorted_map.items())

# Output SortedKeysView(SortedDict({1: 'one', 2: 'two', 4: 'four', 5: 'five'}))
print(sorted_map.keys())

# Output SortedValuesView(SortedDict({1: 'one', 2: 'two', 4: 'four', 5: 'five'}))
print(sorted_map.values())
print(*sorted_map.items()) # (1, 'one') (2, 'two') (4, 'four') (5, 'five')
print(*sorted_map.keys()) # 1 2 4 5
print(*sorted_map.values()) # one two four five

# Find the index of an existing key
print(sorted_map.index(2)) # 1
Expand Down
Loading