From 048894d7d08c30df3ffce819deedb4d9a4d94b99 Mon Sep 17 00:00:00 2001 From: Nathan Wang Date: Tue, 14 Jul 2020 23:19:45 -0700 Subject: [PATCH] tuples --- content/3_Bronze/Pairs_Tuples.mdx | 56 ++++++++++++++++++++++++- content/3_Bronze/Unordered.mdx | 15 ++++--- src/components/markdown/MDXProvider.tsx | 2 +- src/context/UserSettingsContext.tsx | 24 +++++++---- 4 files changed, 78 insertions(+), 19 deletions(-) diff --git a/content/3_Bronze/Pairs_Tuples.mdx b/content/3_Bronze/Pairs_Tuples.mdx index 048b5a9..435c6da 100644 --- a/content/3_Bronze/Pairs_Tuples.mdx +++ b/content/3_Bronze/Pairs_Tuples.mdx @@ -1,18 +1,43 @@ --- id: pairs-tuples -title: Pairs & Tuples in C++ +title: Pairs & Tuples author: Benjamin Qi, Nathan Wang, Darren Yao, Abutalib Namazov description: "Introduction to pairs, which allow you to store two objects (possibly of different types) as a single unit, as well as tuples, which are a generalization of pairs." prerequisites: - Bronze - Introduction to Data Structures --- + + + + + + +**Pairs are not available in Python.** Just use tuples; no need for pairs! + + + + + + + + +**Pairs and tuples are not available in Java.** If you are using Java, just skip this module. + + + + + + Storing multiple elements as a single unit allows you to change the order of the elements (e.g. sorting) without mixing one component with another. **Example:** Given some points on a plane where the $i$-th point has coordinates $(x_i,y_i)$, sort these points in increasing order of $x$ coordinate and increasing order of $y$ in case there is a tie. If you sort one of $x_i$ and $y_i$ without considering the other, the points will not be in the correct order. We need to consider the pair $(x_i,y_i)$ as a single unit. + + + ## [Pairs](http://www.cplusplus.com/reference/utility/pair/pair/) - `pair p`: Creates a pair `p` with two elements with the first one being of `type1` and the second one being of `type2`. @@ -45,6 +70,7 @@ int main() { */ ``` + ## [Tuples](http://www.cplusplus.com/reference/tuple/) Of course, we can hold more than two values with something like `pair>`, but it gets messy when you need a lot of elements. In this case, using **tuples** might be more convenient. @@ -122,4 +148,30 @@ int main() { */ ``` -Tuples are sorted similarly. \ No newline at end of file +Tuples are sorted similarly. + + + + + +**Pairs and Tuples are not available in Java. Skip this module if you're a Java user!** + + + + + +## Python Tuples + +Use the link below (if you know of a better one, please let us know!) to learn about tuples. + +Python automatically sorts a list of tuples by the first element, then the second, and so on. This can save you time and keystrokes for certain problems like the one above. + + + + + + + + + + \ No newline at end of file diff --git a/content/3_Bronze/Unordered.mdx b/content/3_Bronze/Unordered.mdx index cda9a2e..d3a4217 100644 --- a/content/3_Bronze/Unordered.mdx +++ b/content/3_Bronze/Unordered.mdx @@ -38,7 +38,7 @@ Both Java and C++ contain two versions of sets and maps; one in which the keys a (more in-depth explanation?) -## HashSets +## Unordered Sets (HashSets) @@ -95,7 +95,7 @@ for(int element : set){ -## HashMaps +## Unordered Maps (HashMaps) @@ -140,7 +140,9 @@ System.out.println(map.containsKey(1)); // true -## Hacking +## Hacking (Optional) + +_You don't need to know this for USACO, so you can safely skip this section._ In USACO contests, unordered sets and maps generally fine, but the built-in hashing algorithm for C++ is vulnerable to pathological data sets causing abnormally slow runtimes. Apparently [Java](https://codeforces.com/blog/entry/62393?#comment-464875) is not vulnerable to this, however. @@ -174,11 +176,8 @@ unordered_map U; - - - - - + + diff --git a/src/components/markdown/MDXProvider.tsx b/src/components/markdown/MDXProvider.tsx index d62df2c..afec8b2 100644 --- a/src/components/markdown/MDXProvider.tsx +++ b/src/components/markdown/MDXProvider.tsx @@ -49,7 +49,7 @@ const components = { ), 'warning-block': ({ children, title }) => ( -
+
{ - const [primaryLang, setPrimaryLang] = useStickyState( - 'showAll', - 'user-primary-lang' - ); + const key = 'guide:userSettings:primaryLang'; + const [primaryLang, setPrimaryLang] = useState('showAll'); + const initialRender = useRef(true); + React.useEffect(() => { - if (primaryLang === 'showAll') setPrimaryLang('cpp'); - }, []); + if (initialRender.current) { + const stickyValue = window.localStorage.getItem(key); + if (stickyValue !== null) setPrimaryLang(JSON.parse(stickyValue)); + else setPrimaryLang('C++'); + + initialRender.current = false; + } else if (primaryLang !== 'showAll') { + window.localStorage.setItem(key, JSON.stringify(primaryLang)); + } + }, [key, primaryLang]); + return (