Examples

Page 9 of 50

Browse code examples

pythonSnippet

ord And chr Map Code Points

ord returns the code point behind a character and chr inverts it, so char-level text math works on Unicode strings that iterate one code point at a time.

pythonSnippet

Flat Rows To Nested Tree

Index rows by id, attach each child under its parent reference, and collect roots separately, turning flat rows into a nested tree in one pass.

pythonSnippet

Palindrome Check Two Pointers

Walk two pointers inward from both ends, skipping non-alphanumeric characters and comparing lowercased letters, so punctuation-laden phrases still verify.

pythonSnippet

Prefix Sum Range Queries

Build one prefix array up front, then answer any range sum query with a single subtraction instead of re-summing the slice every time.

pythonSnippet

Two Heap Running Median

Split a value stream across two heapq heaps so every insert rebalances the halves and yields the exact running median without re-sorting.

rustSnippet

Fences Order Flag And Payload

A Release fence before publishing a flag pairs with an Acquire fence after observing it, so payload writes are visible without atomic ordering on each access.

rustSnippet

Clone Ends The Borrow Early

Turn a map lookup into an owned value with cloned so the borrow ends immediately, letting later inserts into the same HashMap borrow-check cleanly.

rustSnippet

Copy Files With Std Fs

Copy a file with fs::copy inside a fallible main, assert the byte count, and compare content line by line before cleaning up both temporary files.

rustSnippet

Expand Compact Range Notation

Expand compact range notation like 1-3,5,8-9 by splitting on commas, matching each part with split_once, and flat_mapping ranges into one vector.

rustSnippet

Fetch Update Folds The CAS Retry

fetch_update folds the compare-and-swap retry loop into one call: Ok carries the previous value when the closure wrote, Err carries it when the closure refused.

rustSnippet

Sync All Before Rename

Stage a write with write_all, force it to disk with sync_all, then publish with fs::rename so a crash never leaves a half-written live file.