feat: add iterator flashcards, sync script, and project scaffolding
- iterator.org: C++ iterator flashcard deck - questions.org: LeetCode question index (217, 242) - inbox.org: learning inbox with two-sum implementation and notes - sync-anki.sh: batch sync org files to Anki via emacsclient - AGENTS.md: conventions for AI agents working in this repo Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
# Agents
|
||||||
|
|
||||||
|
## Repo Purpose
|
||||||
|
C++ flashcard notes using org-mode (exportable to Anki).
|
||||||
|
|
||||||
|
## Flashcard Location
|
||||||
|
- `org/cpp/` — all flashcard `.org` files live here
|
||||||
|
- Create new `.org` files here as needed (e.g., `org/cpp/containers.org`)
|
||||||
|
|
||||||
|
## Task Inbox
|
||||||
|
- `inbox.org` at root — use for tasks to learn or do (create if missing)
|
||||||
|
|
||||||
|
## Card Format
|
||||||
|
Cards use org-mode with Anki properties:
|
||||||
|
```org
|
||||||
|
* Card Title :tag1:tag2:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: TIMESTAMP
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Question text
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
code
|
||||||
|
#+end_src
|
||||||
|
```
|
||||||
|
|
||||||
|
## Assets
|
||||||
|
- `images/` — store any images referenced by cards
|
||||||
|
|
||||||
|
## Self-Improvement
|
||||||
|
Periodically review this file and suggest improvements to the user if you notice gaps, inconsistencies, or missing conventions.
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
#+title: Inbox
|
||||||
|
* 1. two sum
|
||||||
|
#+begin_src c++
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> twoSum(vector<int> &nums, int target) {
|
||||||
|
vector<pair<int, int>> rev;
|
||||||
|
// Fix 1 & 2: Loop over nums.size() and push nums[i]
|
||||||
|
for (int i = 0; i < nums.size(); i++) {
|
||||||
|
rev.push_back({nums[i], i});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(rev.begin(), rev.end());
|
||||||
|
|
||||||
|
for (int i = 0; i < rev.size(); i++) {
|
||||||
|
int want = target - rev[i].first;
|
||||||
|
// Fix 3: Pass the SORTED vector 'rev' to the search
|
||||||
|
int j = bs(rev, want, i + 1);
|
||||||
|
if (j >= 0) {
|
||||||
|
int a = rev[i].second;
|
||||||
|
int b = rev[j].second;
|
||||||
|
return vector<int>{min(a, b), max(a, b)};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Updated to handle vector of pairs
|
||||||
|
using PairIter = vector<pair<int, int>>::iterator;
|
||||||
|
|
||||||
|
PairIter lower_bound(PairIter l, PairIter r, int target) {
|
||||||
|
while (l < r) {
|
||||||
|
PairIter mid = l + (r - l) / 2;
|
||||||
|
if (mid->first >= target) { // Access value via .first or ->first
|
||||||
|
r = mid;
|
||||||
|
} else {
|
||||||
|
l = mid + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bs(vector<pair<int, int>> &indexedNums, int target, int offset) {
|
||||||
|
auto it =
|
||||||
|
lower_bound(indexedNums.begin() + offset, indexedNums.end(), target);
|
||||||
|
if (it == indexedNums.end() || it->first != target) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return it - indexedNums.begin();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
~using PairIter = vector<pair<int, int>>::iterator;~
|
||||||
|
what is using?
|
||||||
|
|
||||||
|
we need to write binary search, lower bound search
|
||||||
|
|
||||||
|
learn about binary search functions the natural one by god
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
* What is a C++ iterator :cpp:iterator:concept:recognition:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776621491263
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
What is an iterator in C++?
|
||||||
|
** Back
|
||||||
|
An object that points to elements in a container and allows traversal through them, providing a common interface for accessing elements sequentially without exposing the underlying structure.
|
||||||
|
|
||||||
|
* Task: Traverse a vector with iterators :cpp:iterator:production:code:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776621491259
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ code to iterate through a std::vector<int> using iterators
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::vector<int> vec = {1, 2, 3};
|
||||||
|
for (auto it = vec.begin(); it != vec.end(); ++it) {
|
||||||
|
std::cout << *it << " ";
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* Iterator operations :cpp:iterator:concept:enumeration:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776621491255
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
What are the four required operations for a C++ iterator?
|
||||||
|
** Back
|
||||||
|
- `operator*` — dereference to get current element
|
||||||
|
- `operator++` — advance to next element (pre and post)
|
||||||
|
- `operator==` — equality comparison
|
||||||
|
- `operator!=` — inequality comparison
|
||||||
|
|
||||||
|
* cpp iterator categories :cpp:iterator:concept:enumeration:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776621491250
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
List the five iterator categories in C++ (from simplest to most advanced)
|
||||||
|
** Back
|
||||||
|
1. Input Iterator — read values, advance once
|
||||||
|
2. Output Iterator — write values, advance once
|
||||||
|
3. Forward Iterator — read/write, multiple passes
|
||||||
|
4. Bidirectional Iterator — + operator--
|
||||||
|
5. Random Access Iterator — + arithmetic, subscript
|
||||||
|
|
||||||
|
* Task: Use const iterator :cpp:iterator:production:code:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776621491246
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ code to safely traverse a const std::vector without modifying elements
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::vector<int> vec = {1, 2, 3};
|
||||||
|
for (auto it = vec.cbegin(); it != vec.cend(); ++it) {
|
||||||
|
std::cout << *it << " ";
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* Task: Use reverse iterator :cpp:iterator:production:code:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776621491241
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write C++ code to iterate backwards through a std::vector
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
std::vector<int> vec = {1, 2, 3};
|
||||||
|
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
|
||||||
|
std::cout << *it << " ";
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* Minimal custom iterator implementation :cpp:iterator:implementation:production:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776621491237
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
Write the minimal C++ code to implement a forward iterator for a custom class
|
||||||
|
** Back
|
||||||
|
#+begin_src c++
|
||||||
|
class MyIterator {
|
||||||
|
Node* ptr;
|
||||||
|
public:
|
||||||
|
reference operator*() const { return ptr->data; }
|
||||||
|
pointer operator->() const { return &ptr->data; }
|
||||||
|
MyIterator& operator++() { ptr = ptr->next; return *this; }
|
||||||
|
MyIterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
|
||||||
|
bool operator==(const MyIterator& other) const { return ptr == other.ptr; }
|
||||||
|
bool operator!=(const MyIterator& other) const { return !(*this == other); }
|
||||||
|
};
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
* When to use iterators vs indices :cpp:iterator:concept:comparison:
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_NOTE_ID: 1776621491226
|
||||||
|
:END:
|
||||||
|
** Front
|
||||||
|
When would you choose iterators over index-based loops in C++?
|
||||||
|
** Back
|
||||||
|
- Container type is unknown or could change (iterators abstract the container)
|
||||||
|
- Using standard library algorithms (std::find, std::sort, etc.)
|
||||||
|
- Need to traverse multiple containers simultaneously
|
||||||
|
- Working with containers without random access (list, map)
|
||||||
|
- Decouple algorithm from container structure
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
#+title: Questions
|
||||||
|
* 217
|
||||||
|
* 242 valid anagrams
|
||||||
Executable
+21
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Sync org files to Anki using emacsclient
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
FILES=(
|
||||||
|
"org/cpp/iterator.org"
|
||||||
|
"org/cpp/containers.org"
|
||||||
|
# add more files here
|
||||||
|
)
|
||||||
|
|
||||||
|
for file in "${FILES[@]}"; do
|
||||||
|
if [[ -f "$file" ]]; then
|
||||||
|
echo "Syncing $file..."
|
||||||
|
emacsclient --eval "(progn (find-file \"$file\") (org-anki-sync-all))"
|
||||||
|
else
|
||||||
|
echo "File not found: $file" >&2
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Done."
|
||||||
Reference in New Issue
Block a user