Compare commits
40 Commits
cat-town
...
efc1c8f029
Author | SHA1 | Date | |
---|---|---|---|
efc1c8f029
|
|||
43a2e51712
|
|||
88e4e319d3
|
|||
c274f59f58
|
|||
c8a4928ee8
|
|||
6d523ad7c1
|
|||
d9393152c5
|
|||
2f3ce704b2
|
|||
54a7a2340c
|
|||
8cc2e18965
|
|||
ffb16efcdc
|
|||
10ec594031
|
|||
cee338520e
|
|||
e92d117a41
|
|||
dc9c3fd54d
|
|||
a8d82b180a
|
|||
337a795ad0
|
|||
3f369e1e70
|
|||
a3819c9b26
|
|||
345464d0d4
|
|||
5b55118373
|
|||
accf0d0c2b
|
|||
b672131cfc
|
|||
d03225edce
|
|||
8434bf7025
|
|||
087355502d
|
|||
0e6448df23
|
|||
4da817996d
|
|||
7e0aa3b86b
|
|||
aa83db6f7a
|
|||
9d4f5b661b
|
|||
b94fb3b31e
|
|||
dacbddca6e
|
|||
6e50ec65f8
|
|||
ca812add16
|
|||
460741740b
|
|||
7204f08177
|
|||
505e04613f
|
|||
7f4f627769
|
|||
dc8045a20e
|
3
cat-town/.gitignore
vendored
3
cat-town/.gitignore
vendored
@@ -1,3 +0,0 @@
|
|||||||
/node_modules/
|
|
||||||
/src/*.js
|
|
||||||
/dist/
|
|
4838
cat-town/package-lock.json
generated
4838
cat-town/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,30 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "cat-town",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"private": true,
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"webpack": "webpack",
|
|
||||||
"dev": "webpack serve --config webpack.development.js",
|
|
||||||
"start": "npm run dev",
|
|
||||||
"build:dev": "webpack --config webpack.development.js",
|
|
||||||
"build:prod": "webpack --config webpack.production.js"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@excaliburjs/testing": "^0.25.1",
|
|
||||||
"clean-webpack-plugin": "^3.0.0",
|
|
||||||
"compression-webpack-plugin": "^7.1.2",
|
|
||||||
"html-webpack-plugin": "^5.5.0",
|
|
||||||
"source-map-loader": "^2.0.2",
|
|
||||||
"terser-webpack-plugin": "^5.3.6",
|
|
||||||
"ts-loader": "^9.4.2",
|
|
||||||
"typescript": "^4.9.4",
|
|
||||||
"webpack": "^5.75.0",
|
|
||||||
"webpack-cli": "^4.10.0",
|
|
||||||
"webpack-dev-server": "^4.11.1",
|
|
||||||
"webpack-merge": "^5.8.0"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"excalibur": "^0.27.0"
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,17 +0,0 @@
|
|||||||
import { Actor, Color, vec } from 'excalibur'
|
|
||||||
import { Resources } from '../resources'
|
|
||||||
|
|
||||||
export class Player extends Actor {
|
|
||||||
constructor() {
|
|
||||||
super({
|
|
||||||
pos: vec(150, 150),
|
|
||||||
width: 25,
|
|
||||||
height: 25,
|
|
||||||
color: new Color(255, 100, 100)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
onInitialize() {
|
|
||||||
this.graphics.use(Resources.Cat.toSprite())
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
// type Cat = {
|
|
||||||
// name string;
|
|
||||||
// years int;
|
|
||||||
// centimeters int;
|
|
||||||
// kilograms int;
|
|
||||||
// coloring Coloring;
|
|
||||||
// pattern Pattern;
|
|
||||||
// mood Mood;
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// type Coloring = 'orange' | 'black' | 'brown' | 'blue';
|
|
||||||
//
|
|
||||||
// type Pattern = 'plain' | 'striped' | 'spotted';
|
|
||||||
//
|
|
||||||
// type Mood = 'happy' | 'sad' | 'purring' |
|
|
||||||
// 'screaming' | 'mad' | 'scratchy' | 'curious' | 'concerned';
|
|
Binary file not shown.
Before Width: | Height: | Size: 8.8 KiB |
Binary file not shown.
@@ -1,30 +0,0 @@
|
|||||||
import { Engine, Loader, DisplayMode } from 'excalibur'
|
|
||||||
import { Beginning } from './scenes/beginning'
|
|
||||||
import { Player } from './actors/player'
|
|
||||||
import { Resources } from './resources'
|
|
||||||
|
|
||||||
class Game extends Engine {
|
|
||||||
private player: Player
|
|
||||||
private beginning: Beginning
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
super({ displayMode: DisplayMode.FitScreen })
|
|
||||||
}
|
|
||||||
|
|
||||||
public start() {
|
|
||||||
this.beginning = new Beginning()
|
|
||||||
this.player = new Player()
|
|
||||||
this.beginning.add(this.player)
|
|
||||||
|
|
||||||
game.add('beginning', this.beginning)
|
|
||||||
|
|
||||||
const loader = new Loader(Object.values(Resources))
|
|
||||||
|
|
||||||
return super.start(loader)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const game = new Game()
|
|
||||||
game.start().then(() => {
|
|
||||||
game.goToScene('beginning')
|
|
||||||
})
|
|
@@ -1,8 +0,0 @@
|
|||||||
import { ImageSource } from 'excalibur'
|
|
||||||
import catImage from './images/cat.png'
|
|
||||||
|
|
||||||
const Resources = {
|
|
||||||
Cat: new ImageSource(catImage)
|
|
||||||
}
|
|
||||||
|
|
||||||
export { Resources }
|
|
@@ -1,7 +0,0 @@
|
|||||||
import { Engine, Scene } from 'excalibur'
|
|
||||||
|
|
||||||
export class Beginning extends Scene {
|
|
||||||
public onInitialize(engine: Engine) {}
|
|
||||||
public onActivate() {}
|
|
||||||
public onDeactivate() {}
|
|
||||||
}
|
|
@@ -1,22 +0,0 @@
|
|||||||
// type Town = {
|
|
||||||
// grid Grid;
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// type Grid = {
|
|
||||||
// squares Map<Coords, Square>;
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// type Coords = {
|
|
||||||
// x int;
|
|
||||||
// y int;
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// type Square = {
|
|
||||||
// description string;
|
|
||||||
// things Map<string, Thing>;
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// type Thing = {
|
|
||||||
// name string;
|
|
||||||
// description string;
|
|
||||||
// };
|
|
@@ -1,14 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"allowSyntheticDefaultImports": true,
|
|
||||||
"moduleResolution": "node",
|
|
||||||
"sourceMap": true,
|
|
||||||
"target": "es2017",
|
|
||||||
"module": "es6",
|
|
||||||
"types": ["excalibur"],
|
|
||||||
"outDir": "./dist/",
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"forceConsistentCasingInFileNames": true,
|
|
||||||
"skipLibCheck": true
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,47 +0,0 @@
|
|||||||
const path = require("path");
|
|
||||||
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
|
|
||||||
const HtmlWebPackPlugin = require("html-webpack-plugin");
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
entry: "./src/index.ts",
|
|
||||||
target: "web",
|
|
||||||
output: {
|
|
||||||
filename: '[name].js',
|
|
||||||
sourceMapFilename: "[file].map",
|
|
||||||
path: path.resolve(__dirname, "dist"),
|
|
||||||
},
|
|
||||||
devtool: "source-map",
|
|
||||||
module: {
|
|
||||||
rules: [
|
|
||||||
{
|
|
||||||
test: /\.(png|svg|jpg|jpeg|gif)$/i,
|
|
||||||
type: "asset/resource"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
test: /\.js$/,
|
|
||||||
use: ["source-map-loader"],
|
|
||||||
exclude: [path.resolve(__dirname, "node_modules/excalibur")],
|
|
||||||
enforce: "pre",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
test: /\.tsx?$/,
|
|
||||||
use: "ts-loader",
|
|
||||||
exclude: /node_modules/,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
resolve: {
|
|
||||||
extensions: [".tsx", ".ts", ".js"],
|
|
||||||
},
|
|
||||||
optimization: {
|
|
||||||
splitChunks: {
|
|
||||||
chunks: "all",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
new CleanWebpackPlugin(),
|
|
||||||
new HtmlWebPackPlugin({
|
|
||||||
title: "Cat Town",
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
};
|
|
@@ -1,12 +0,0 @@
|
|||||||
const { merge } = require("webpack-merge");
|
|
||||||
const common = require("./webpack.common");
|
|
||||||
|
|
||||||
module.exports = merge(common, {
|
|
||||||
mode: "development",
|
|
||||||
devtool: "inline-source-map",
|
|
||||||
devServer: {
|
|
||||||
static: {
|
|
||||||
directory: "./dist",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
@@ -1,13 +0,0 @@
|
|||||||
const { merge } = require("webpack-merge");
|
|
||||||
const CompressionWebpackPlugin = require("compression-webpack-plugin");
|
|
||||||
const TerserPlugin = require("terser-webpack-plugin");
|
|
||||||
const common = require("./webpack.common");
|
|
||||||
|
|
||||||
module.exports = merge(common, {
|
|
||||||
mode: "production",
|
|
||||||
optimization: {
|
|
||||||
minimize: true,
|
|
||||||
minimizer: [new TerserPlugin()],
|
|
||||||
},
|
|
||||||
plugins: [new CompressionWebpackPlugin()],
|
|
||||||
});
|
|
1
leetcode/.gitignore
vendored
Normal file
1
leetcode/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.envrc
|
1
leetcode/.python-version
Normal file
1
leetcode/.python-version
Normal file
@@ -0,0 +1 @@
|
|||||||
|
3.12.0
|
160
leetcode/pyproject.toml
Normal file
160
leetcode/pyproject.toml
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["hatchling"]
|
||||||
|
build-backend = "hatchling.build"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "leetcode"
|
||||||
|
description = 'What A Mess'
|
||||||
|
readme = "README.md"
|
||||||
|
version = "0.1.0"
|
||||||
|
requires-python = ">=3.7"
|
||||||
|
license = "MIT"
|
||||||
|
keywords = []
|
||||||
|
authors = [
|
||||||
|
{ name = "Dan Buch", email = "dan@meatballhat.com" },
|
||||||
|
]
|
||||||
|
classifiers = [
|
||||||
|
"Development Status :: 4 - Beta",
|
||||||
|
"Programming Language :: Python",
|
||||||
|
"Programming Language :: Python :: 3.7",
|
||||||
|
"Programming Language :: Python :: 3.8",
|
||||||
|
"Programming Language :: Python :: 3.9",
|
||||||
|
"Programming Language :: Python :: 3.10",
|
||||||
|
"Programming Language :: Python :: 3.11",
|
||||||
|
"Programming Language :: Python :: Implementation :: CPython",
|
||||||
|
"Programming Language :: Python :: Implementation :: PyPy",
|
||||||
|
]
|
||||||
|
dependencies = [
|
||||||
|
"ipython",
|
||||||
|
"ipdb"
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.urls]
|
||||||
|
Documentation = "https://github.com/unknown/leetcode#readme"
|
||||||
|
Issues = "https://github.com/unknown/leetcode/issues"
|
||||||
|
Source = "https://github.com/unknown/leetcode"
|
||||||
|
|
||||||
|
[tool.hatch.envs.default]
|
||||||
|
dependencies = [
|
||||||
|
"coverage[toml]>=6.5",
|
||||||
|
"pytest",
|
||||||
|
]
|
||||||
|
[tool.hatch.envs.default.scripts]
|
||||||
|
test = "pytest {args:tests}"
|
||||||
|
test-cov = "coverage run -m pytest {args:tests}"
|
||||||
|
cov-report = [
|
||||||
|
"- coverage combine",
|
||||||
|
"coverage report",
|
||||||
|
]
|
||||||
|
cov = [
|
||||||
|
"test-cov",
|
||||||
|
"cov-report",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[tool.hatch.envs.all.matrix]]
|
||||||
|
python = ["3.7", "3.8", "3.9", "3.10", "3.11"]
|
||||||
|
|
||||||
|
[tool.hatch.envs.lint]
|
||||||
|
detached = true
|
||||||
|
dependencies = [
|
||||||
|
"black>=23.1.0",
|
||||||
|
"mypy>=1.0.0",
|
||||||
|
"ruff>=0.0.243",
|
||||||
|
]
|
||||||
|
[tool.hatch.envs.lint.scripts]
|
||||||
|
typing = "mypy --install-types --non-interactive {args:.}"
|
||||||
|
style = [
|
||||||
|
"ruff {args:.}",
|
||||||
|
"black --check --diff {args:.}",
|
||||||
|
]
|
||||||
|
fmt = [
|
||||||
|
"black {args:.}",
|
||||||
|
"ruff --fix {args:.}",
|
||||||
|
"style",
|
||||||
|
]
|
||||||
|
all = [
|
||||||
|
"style",
|
||||||
|
"typing",
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.black]
|
||||||
|
target-version = ["py37"]
|
||||||
|
line-length = 90
|
||||||
|
|
||||||
|
[tool.ruff]
|
||||||
|
target-version = "py37"
|
||||||
|
line-length = 90
|
||||||
|
select = [
|
||||||
|
"A",
|
||||||
|
"ARG",
|
||||||
|
"B",
|
||||||
|
"C",
|
||||||
|
"DTZ",
|
||||||
|
"E",
|
||||||
|
"EM",
|
||||||
|
"F",
|
||||||
|
"FBT",
|
||||||
|
"I",
|
||||||
|
"ICN",
|
||||||
|
"ISC",
|
||||||
|
"N",
|
||||||
|
"PLC",
|
||||||
|
"PLE",
|
||||||
|
"PLR",
|
||||||
|
"PLW",
|
||||||
|
"Q",
|
||||||
|
"RUF",
|
||||||
|
"S",
|
||||||
|
"T",
|
||||||
|
"TID",
|
||||||
|
"UP",
|
||||||
|
"W",
|
||||||
|
"YTT",
|
||||||
|
]
|
||||||
|
ignore = [
|
||||||
|
# Allow non-abstract empty methods in abstract base classes
|
||||||
|
"B027",
|
||||||
|
# Allow boolean positional values in function calls, like `dict.get(... True)`
|
||||||
|
"FBT003",
|
||||||
|
# Ignore checks for possible passwords
|
||||||
|
"S105", "S106", "S107",
|
||||||
|
# Ignore complexity
|
||||||
|
"C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915",
|
||||||
|
# Allow print func
|
||||||
|
"T201",
|
||||||
|
# Allow assert statements
|
||||||
|
"S101",
|
||||||
|
]
|
||||||
|
unfixable = [
|
||||||
|
# Don't touch unused imports
|
||||||
|
"F401",
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.ruff.isort]
|
||||||
|
known-first-party = ["leetcode"]
|
||||||
|
|
||||||
|
[tool.ruff.flake8-tidy-imports]
|
||||||
|
ban-relative-imports = "all"
|
||||||
|
|
||||||
|
[tool.ruff.per-file-ignores]
|
||||||
|
# Tests can use magic values, assertions, and relative imports
|
||||||
|
"tests/**/*" = ["PLR2004", "S101", "TID252"]
|
||||||
|
|
||||||
|
[tool.coverage.run]
|
||||||
|
source_pkgs = ["leetcode", "tests"]
|
||||||
|
branch = true
|
||||||
|
parallel = true
|
||||||
|
omit = [
|
||||||
|
"src/leetcode/__about__.py",
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.coverage.paths]
|
||||||
|
leetcode = ["src/leetcode", "*/leetcode/src/leetcode"]
|
||||||
|
tests = ["tests", "*/leetcode/tests"]
|
||||||
|
|
||||||
|
[tool.coverage.report]
|
||||||
|
exclude_lines = [
|
||||||
|
"no cov",
|
||||||
|
"if __name__ == .__main__.:",
|
||||||
|
"if TYPE_CHECKING:",
|
||||||
|
]
|
57
leetcode/roman.py
Normal file
57
leetcode/roman.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
SIMPLE = {
|
||||||
|
"I": 1,
|
||||||
|
"V": 5,
|
||||||
|
"X": 10,
|
||||||
|
"L": 50,
|
||||||
|
"C": 100,
|
||||||
|
"D": 500,
|
||||||
|
"M": 1000,
|
||||||
|
}
|
||||||
|
SIMPLE_REVERSE = {v: k for k, v in SIMPLE.items()}
|
||||||
|
COMPOUND = {
|
||||||
|
"IV": 4,
|
||||||
|
"IX": 9,
|
||||||
|
"XL": 40,
|
||||||
|
"XC": 90,
|
||||||
|
"CD": 400,
|
||||||
|
"CM": 900,
|
||||||
|
}
|
||||||
|
PREFIXES = {k[0] for k in COMPOUND.keys()}
|
||||||
|
COMPOUND_REVERSE = {v: k for k, v in COMPOUND.items()}
|
||||||
|
ALL = SIMPLE | COMPOUND
|
||||||
|
ALL_REVERSE = {v: k for k, v in ALL.items()}
|
||||||
|
|
||||||
|
|
||||||
|
def i2r(i: int) -> str:
|
||||||
|
if i > 100_000:
|
||||||
|
raise ValueError(f"{i} is too silly")
|
||||||
|
|
||||||
|
r: list[str] = []
|
||||||
|
|
||||||
|
for int_val, roman_val in sorted(ALL_REVERSE.items(), reverse=True):
|
||||||
|
remainder = i % int_val
|
||||||
|
|
||||||
|
r += [roman_val] * int((i - remainder) / int_val)
|
||||||
|
|
||||||
|
i = remainder
|
||||||
|
|
||||||
|
return "".join(r)
|
||||||
|
|
||||||
|
|
||||||
|
def r2i(r: str) -> int:
|
||||||
|
total = 0
|
||||||
|
offset = 0
|
||||||
|
|
||||||
|
for i in range(len(r)):
|
||||||
|
if i + offset > len(r) - 1:
|
||||||
|
break
|
||||||
|
|
||||||
|
c = r[i + offset]
|
||||||
|
if c in PREFIXES and (i + offset + 1) < len(r) and c + r[i + offset + 1] in ALL:
|
||||||
|
total += ALL[c + r[i + offset + 1]]
|
||||||
|
offset += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
total += ALL[c]
|
||||||
|
|
||||||
|
return total
|
57
leetcode/spiral_matrix.py
Normal file
57
leetcode/spiral_matrix.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import itertools
|
||||||
|
import typing
|
||||||
|
|
||||||
|
|
||||||
|
def matrix_spiral(matrix: list[list[typing.Any]]) -> list[typing.Any]:
|
||||||
|
return [matrix[y][x] for x, y in matrix_spiral_path(matrix)]
|
||||||
|
|
||||||
|
|
||||||
|
def matrix_spiral_path(matrix: list[list[int]]) -> list[tuple[int, int]]:
|
||||||
|
snek = SpinSnek(matrix)
|
||||||
|
|
||||||
|
while snek.step():
|
||||||
|
...
|
||||||
|
|
||||||
|
return snek.path
|
||||||
|
|
||||||
|
|
||||||
|
COMPASS = (
|
||||||
|
(1, 0), # east
|
||||||
|
(0, 1), # south
|
||||||
|
(-1, 0), # west
|
||||||
|
(0, -1), # north
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class SpinSnek:
|
||||||
|
def __init__(self, board: list[list[int]], loc: tuple[int, int] = (0, 0)):
|
||||||
|
self.max_loc: tuple[int, int] = (len(board[0]) - 1, len(board) - 1)
|
||||||
|
self.spinner: itertools.cycle[tuple[int, int]] = itertools.cycle(COMPASS)
|
||||||
|
self.direction = next(self.spinner)
|
||||||
|
self.path: list[tuple[int, int]] = [loc]
|
||||||
|
self.missteps: int = 0
|
||||||
|
|
||||||
|
def step(self) -> bool:
|
||||||
|
loc = self.path[-1]
|
||||||
|
next_loc: tuple[int, int] = (
|
||||||
|
loc[0] + self.direction[0],
|
||||||
|
loc[1] + self.direction[1],
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
next_loc[0] > self.max_loc[0]
|
||||||
|
or next_loc[1] > self.max_loc[1]
|
||||||
|
or next_loc[0] < 0
|
||||||
|
or next_loc[1] < 0
|
||||||
|
or next_loc in self.path
|
||||||
|
):
|
||||||
|
self.direction = next(self.spinner)
|
||||||
|
if self.missteps > len(COMPASS) - 1:
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.missteps += 1
|
||||||
|
return self.step()
|
||||||
|
|
||||||
|
self.missteps: int = 0
|
||||||
|
self.path.append(next_loc)
|
||||||
|
return True
|
124
leetcode/stdlib.py
Normal file
124
leetcode/stdlib.py
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
import typing
|
||||||
|
|
||||||
|
|
||||||
|
class LinkedListNode(typing.Protocol):
|
||||||
|
val: int
|
||||||
|
next: typing.Optional["LinkedListNode"]
|
||||||
|
|
||||||
|
|
||||||
|
class ListNode:
|
||||||
|
"""ListNode is the leetcode "standard library" type used in linked lists"""
|
||||||
|
|
||||||
|
def __init__(self, val: int = 0, next: typing.Optional["ListNode"] = None): # no qa
|
||||||
|
self.val = val
|
||||||
|
self.next = next
|
||||||
|
|
||||||
|
|
||||||
|
class BinaryTreeNode(typing.Protocol):
|
||||||
|
val: int
|
||||||
|
left: typing.Optional["BinaryTreeNode"]
|
||||||
|
right: typing.Optional["BinaryTreeNode"]
|
||||||
|
|
||||||
|
|
||||||
|
class TreeNode:
|
||||||
|
"""TreeNode is the leetcode "standard library" type used in binary trees"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
val: int = 0,
|
||||||
|
left: typing.Optional["TreeNode"] = None,
|
||||||
|
right: typing.Optional["TreeNode"] = None,
|
||||||
|
):
|
||||||
|
self.val = val
|
||||||
|
self.left = left
|
||||||
|
self.right = right
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_int(cls, val: int | None) -> typing.Optional["TreeNode"]:
|
||||||
|
if val is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return TreeNode(val)
|
||||||
|
|
||||||
|
# __repr__ was added by me
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
filtered_parts = []
|
||||||
|
|
||||||
|
for key, value in [
|
||||||
|
("val", self.val),
|
||||||
|
("left", self.left),
|
||||||
|
("right", self.right),
|
||||||
|
]:
|
||||||
|
if value is not None:
|
||||||
|
filtered_parts.append((key, value))
|
||||||
|
|
||||||
|
middle = ", ".join([f"{k}={v!r}" for k, v in filtered_parts])
|
||||||
|
|
||||||
|
return f"TreeNode({middle})"
|
||||||
|
|
||||||
|
# __eq__ was added by me
|
||||||
|
def __eq__(self, other: typing.Optional["TreeNode"]) -> bool:
|
||||||
|
return (
|
||||||
|
other is not None
|
||||||
|
and self.val == other.val
|
||||||
|
and self.left == other.left
|
||||||
|
and self.right == other.right
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ConnectableBinaryTreeNode(typing.Protocol):
|
||||||
|
val: int
|
||||||
|
left: typing.Optional["BinaryTreeNode"]
|
||||||
|
right: typing.Optional["BinaryTreeNode"]
|
||||||
|
next: typing.Optional["BinaryTreeNode"]
|
||||||
|
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
"""Node is the *other* leetcode "standard library" type used in binary trees"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
val: int = 0,
|
||||||
|
left: typing.Optional["Node"] = None,
|
||||||
|
right: typing.Optional["Node"] = None,
|
||||||
|
next: typing.Optional["Node"] = None, # no qa
|
||||||
|
):
|
||||||
|
self.val = val
|
||||||
|
self.left = left
|
||||||
|
self.right = right
|
||||||
|
self.next = next
|
||||||
|
|
||||||
|
# __repr__ was added by me
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
filtered_parts = []
|
||||||
|
|
||||||
|
for key, value in [
|
||||||
|
("val", self.val),
|
||||||
|
("right", self.right),
|
||||||
|
("left", self.left),
|
||||||
|
("next", self.next),
|
||||||
|
]:
|
||||||
|
if value is not None:
|
||||||
|
filtered_parts.append((key, value))
|
||||||
|
|
||||||
|
middle = ", ".join([f"{k}={v!r}" for k, v in filtered_parts])
|
||||||
|
|
||||||
|
return f"Node({middle})"
|
||||||
|
|
||||||
|
# __eq__ was added by me
|
||||||
|
def __eq__(self, other: "Node") -> bool:
|
||||||
|
return (
|
||||||
|
other is not None
|
||||||
|
and self.val == other.val
|
||||||
|
and self.left == other.left
|
||||||
|
and self.right == other.right
|
||||||
|
and self.next == other.next
|
||||||
|
)
|
||||||
|
|
||||||
|
# __list__ was added by me
|
||||||
|
def __list__(self) -> list[int | None]:
|
||||||
|
ret = [self.val]
|
||||||
|
ret += self.right.__list__() if self.right is not None else [None]
|
||||||
|
ret += self.left.__list__() if self.left is not None else [None]
|
||||||
|
ret += self.next.__list__() if self.next is not None else [None]
|
||||||
|
return ret
|
500
leetcode/stuff.py
Normal file
500
leetcode/stuff.py
Normal file
@@ -0,0 +1,500 @@
|
|||||||
|
import copy
|
||||||
|
import random
|
||||||
|
import typing
|
||||||
|
|
||||||
|
import stdlib
|
||||||
|
|
||||||
|
|
||||||
|
def yep(s: str) -> bool:
|
||||||
|
return s.strip().lower().startswith("y")
|
||||||
|
|
||||||
|
|
||||||
|
def guess_bisect_repl(lower: int, upper: int) -> int:
|
||||||
|
mid = lower + ((upper - lower) // 2)
|
||||||
|
|
||||||
|
if yep(input(f"is it {mid}? ")):
|
||||||
|
return mid
|
||||||
|
|
||||||
|
if yep(input(f"higher than {mid}? ")):
|
||||||
|
return guess_bisect_repl(mid, upper)
|
||||||
|
|
||||||
|
return guess_bisect_repl(lower, mid)
|
||||||
|
|
||||||
|
|
||||||
|
def find_sqrt_ish(n: int) -> int:
|
||||||
|
return int(find_bisect(0, n, gen_sqrt_check(n)))
|
||||||
|
|
||||||
|
|
||||||
|
def gen_sqrt_check(n: int) -> typing.Callable[[float], int]:
|
||||||
|
def check(mid: float) -> int:
|
||||||
|
mid_sq: float = mid * mid
|
||||||
|
|
||||||
|
if mid_sq == n:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if mid_sq < n:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
return check
|
||||||
|
|
||||||
|
|
||||||
|
def find_bisect(
|
||||||
|
lower: float, upper: float, check: typing.Callable[[float], int]
|
||||||
|
) -> float:
|
||||||
|
mid: float = lower + ((upper - lower) / 2)
|
||||||
|
|
||||||
|
print(f"lower={lower} mid={mid} upper={upper}")
|
||||||
|
|
||||||
|
if mid == lower or mid == upper or check(mid) == 0:
|
||||||
|
return mid
|
||||||
|
|
||||||
|
if check(mid) == 1:
|
||||||
|
return find_bisect(mid, upper, check)
|
||||||
|
|
||||||
|
return find_bisect(lower, mid, check)
|
||||||
|
|
||||||
|
|
||||||
|
def cartesian_path(p0: tuple[int, int], p1: tuple[int, int]) -> list[tuple[int, int]]:
|
||||||
|
path: list[tuple[int, int]] = []
|
||||||
|
|
||||||
|
if p0 < p1:
|
||||||
|
for i in range(p0[1], p1[1]):
|
||||||
|
path.append((i, p0[0]))
|
||||||
|
|
||||||
|
for i in range(p0[0], p1[0]):
|
||||||
|
path.append((p1[1], i))
|
||||||
|
|
||||||
|
else:
|
||||||
|
for i in range(p0[1], p1[1] - 1, -1):
|
||||||
|
path.append((i, p0[0]))
|
||||||
|
|
||||||
|
for i in range(p0[0] - 1, p1[0], -1):
|
||||||
|
path.append((p1[1], i))
|
||||||
|
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def gen_matrix(width: int, height: int) -> list[list[int]]:
|
||||||
|
return [list(range(width)) for _ in range(height)]
|
||||||
|
|
||||||
|
|
||||||
|
class MinStack:
|
||||||
|
def __init__(self):
|
||||||
|
self._v: list[int] = []
|
||||||
|
self._min: list[int] = []
|
||||||
|
|
||||||
|
def push(self, val: int) -> None:
|
||||||
|
self._v.append(val)
|
||||||
|
self._min.append(min(val, self._min[-1] if self._min else val))
|
||||||
|
|
||||||
|
def pop(self) -> None:
|
||||||
|
self._v.pop(-1)
|
||||||
|
self._min.pop(-1)
|
||||||
|
|
||||||
|
def top(self) -> int:
|
||||||
|
return self._v[-1]
|
||||||
|
|
||||||
|
def getMin(self) -> int: # no qa
|
||||||
|
return self._min[-1]
|
||||||
|
|
||||||
|
|
||||||
|
def linked_list_to_list(head: stdlib.LinkedListNode | None) -> list[int]:
|
||||||
|
seen: set[int] = set()
|
||||||
|
ret: list[int] = []
|
||||||
|
|
||||||
|
while head is not None:
|
||||||
|
if hash(head) in seen:
|
||||||
|
return ret
|
||||||
|
|
||||||
|
seen.add(hash(head))
|
||||||
|
ret.append(head.val)
|
||||||
|
head = head.next
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def sort_linked_list(head: stdlib.LinkedListNode | None) -> stdlib.LinkedListNode | None:
|
||||||
|
by_val: list[tuple[int, stdlib.LinkedListNode]] = []
|
||||||
|
ret: stdlib.LinkedListNode | None = None
|
||||||
|
|
||||||
|
while head is not None:
|
||||||
|
by_val.append((head.val, head))
|
||||||
|
head = head.next
|
||||||
|
|
||||||
|
cur = ret
|
||||||
|
|
||||||
|
for _, node in sorted(by_val, key=lambda v: v[0]):
|
||||||
|
if cur is None:
|
||||||
|
cur = ret = node
|
||||||
|
continue
|
||||||
|
|
||||||
|
cur.next = node
|
||||||
|
cur = cur.next
|
||||||
|
|
||||||
|
if cur is not None:
|
||||||
|
cur.next = None
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def connect_binary_tree_right(
|
||||||
|
root: stdlib.ConnectableBinaryTreeNode | None,
|
||||||
|
) -> tuple[stdlib.ConnectableBinaryTreeNode | None, list[int | None]]:
|
||||||
|
if root is None:
|
||||||
|
return None, []
|
||||||
|
|
||||||
|
by_level = binary_tree_by_level(copy.deepcopy(root))
|
||||||
|
by_level = typing.cast(dict[int, list[stdlib.ConnectableBinaryTreeNode]], by_level)
|
||||||
|
serialized: list[int | None] = []
|
||||||
|
|
||||||
|
print("")
|
||||||
|
|
||||||
|
if 0 not in by_level or len(by_level[0]) == 0:
|
||||||
|
return None, []
|
||||||
|
|
||||||
|
connected_root = by_level[0][0]
|
||||||
|
|
||||||
|
for level, nodes in sorted(by_level.items(), key=lambda p: p[0]):
|
||||||
|
for i in range(len(nodes)):
|
||||||
|
serialized.append(nodes[i].val)
|
||||||
|
|
||||||
|
if len(nodes) > i + 1:
|
||||||
|
print(f"{'-' * level}> connecting {nodes[i].val} -> {nodes[i + 1].val}")
|
||||||
|
nodes[i].next = nodes[i + 1]
|
||||||
|
|
||||||
|
serialized.append(None)
|
||||||
|
|
||||||
|
return connected_root, serialized
|
||||||
|
|
||||||
|
|
||||||
|
def binary_tree_by_level(
|
||||||
|
root: stdlib.BinaryTreeNode,
|
||||||
|
) -> dict[int, list[stdlib.BinaryTreeNode]]:
|
||||||
|
combined: dict[int, list[stdlib.BinaryTreeNode]] = {}
|
||||||
|
|
||||||
|
for path in collect_binary_tree_levels(0, root):
|
||||||
|
level, node = path
|
||||||
|
combined.setdefault(level, [])
|
||||||
|
combined[level].insert(0, node)
|
||||||
|
|
||||||
|
return combined
|
||||||
|
|
||||||
|
|
||||||
|
def collect_binary_tree_levels(
|
||||||
|
level: int, node: stdlib.BinaryTreeNode | None
|
||||||
|
) -> typing.Iterator[tuple[int, stdlib.BinaryTreeNode]]:
|
||||||
|
if node is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
yield (level, node)
|
||||||
|
yield from collect_binary_tree_levels(level + 1, node.right)
|
||||||
|
yield from collect_binary_tree_levels(level + 1, node.left)
|
||||||
|
|
||||||
|
|
||||||
|
def sum_binary_tree_path_ints(root: stdlib.BinaryTreeNode | None) -> int:
|
||||||
|
path_ints: list[int] = []
|
||||||
|
|
||||||
|
for path in collect_binary_tree_paths(root):
|
||||||
|
path_ints.append(int("".join([str(node.val) for node in path])))
|
||||||
|
|
||||||
|
return sum(path_ints)
|
||||||
|
|
||||||
|
|
||||||
|
def binary_tree_paths_as_lists(
|
||||||
|
paths: list[list[stdlib.BinaryTreeNode]],
|
||||||
|
) -> list[list[int]]:
|
||||||
|
paths_vals: list[list[int]] = []
|
||||||
|
|
||||||
|
for path in paths:
|
||||||
|
paths_vals.append([node.val for node in path])
|
||||||
|
|
||||||
|
return paths_vals
|
||||||
|
|
||||||
|
|
||||||
|
def collect_binary_tree_paths(
|
||||||
|
node: stdlib.BinaryTreeNode | None,
|
||||||
|
) -> typing.Iterator[list[stdlib.BinaryTreeNode]]:
|
||||||
|
if node is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
if node.right is None and node.left is None:
|
||||||
|
yield [node]
|
||||||
|
return
|
||||||
|
|
||||||
|
if node.right is not None:
|
||||||
|
for path in collect_binary_tree_paths(node.right):
|
||||||
|
yield [node] + path
|
||||||
|
|
||||||
|
if node.left is not None:
|
||||||
|
for path in collect_binary_tree_paths(node.left):
|
||||||
|
yield [node] + path
|
||||||
|
|
||||||
|
|
||||||
|
def binary_tree_from_list(inlist: list[int | None]) -> stdlib.BinaryTreeNode | None:
|
||||||
|
if len(inlist) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
nodes: list[stdlib.BinaryTreeNode | None] = [
|
||||||
|
typing.cast(stdlib.BinaryTreeNode | None, stdlib.TreeNode.from_int(i))
|
||||||
|
for i in inlist
|
||||||
|
]
|
||||||
|
nodes_copy = nodes[::-1]
|
||||||
|
root = nodes_copy.pop()
|
||||||
|
|
||||||
|
for node in nodes:
|
||||||
|
if node is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if len(nodes_copy) == 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
node.left = nodes_copy.pop()
|
||||||
|
|
||||||
|
if len(nodes_copy) > 0:
|
||||||
|
node.right = nodes_copy.pop()
|
||||||
|
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
|
def binary_tree_from_preorder_inorder(
|
||||||
|
preorder: list[int], inorder: list[int]
|
||||||
|
) -> stdlib.BinaryTreeNode | None:
|
||||||
|
preorder_reversed = preorder[::-1]
|
||||||
|
|
||||||
|
def subtree(left: list[int], right: list[int]) -> stdlib.BinaryTreeNode:
|
||||||
|
root: stdlib.BinaryTreeNode = typing.cast(
|
||||||
|
stdlib.BinaryTreeNode, stdlib.TreeNode(preorder_reversed.pop())
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(left) > 1:
|
||||||
|
split_pos = left.index(preorder_reversed[-1])
|
||||||
|
root.left = subtree(left[:split_pos], left[split_pos + 1 :])
|
||||||
|
elif len(left) == 1:
|
||||||
|
preorder_reversed.remove(left[0])
|
||||||
|
root.left = typing.cast(stdlib.BinaryTreeNode, stdlib.TreeNode(left[0]))
|
||||||
|
|
||||||
|
if len(right) > 1:
|
||||||
|
split_pos = right.index(preorder_reversed[-1])
|
||||||
|
root.right = subtree(right[:split_pos], right[split_pos + 1 :])
|
||||||
|
elif len(right) == 1:
|
||||||
|
preorder_reversed.remove(right[0])
|
||||||
|
root.right = typing.cast(stdlib.BinaryTreeNode, stdlib.TreeNode(right[0]))
|
||||||
|
|
||||||
|
return root
|
||||||
|
|
||||||
|
split_pos = inorder.index(preorder[0])
|
||||||
|
|
||||||
|
return subtree(inorder[:split_pos], inorder[split_pos + 1 :])
|
||||||
|
|
||||||
|
|
||||||
|
class JumpSpace(typing.NamedTuple):
|
||||||
|
pos: int
|
||||||
|
val: int
|
||||||
|
moves: list["JumpSpace"]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_board(
|
||||||
|
cls, pos: int = 0, board: typing.Iterable[int] = ()
|
||||||
|
) -> typing.Optional["JumpSpace"]:
|
||||||
|
board = list(board)
|
||||||
|
|
||||||
|
if len(board) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
space = cls(pos, board[pos], [])
|
||||||
|
space.collect(board)
|
||||||
|
return space
|
||||||
|
|
||||||
|
def collect(self, board: list[int]) -> None:
|
||||||
|
del self.moves[:]
|
||||||
|
|
||||||
|
if self.pos > len(board) or len(board) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
for n in range(self.pos + 1, self.pos + self.val + 1):
|
||||||
|
if n >= len(board):
|
||||||
|
break
|
||||||
|
|
||||||
|
self.moves.append(typing.cast(JumpSpace, JumpSpace.from_board(n, board)))
|
||||||
|
|
||||||
|
def jump_paths(self) -> list[list[int]]:
|
||||||
|
ret: list[list[int]] = [[self.pos]]
|
||||||
|
|
||||||
|
for next_space in self.moves:
|
||||||
|
for path in next_space.jump_paths():
|
||||||
|
ret.append([self.pos] + path)
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def collect_complete_jump_paths_from_board(board: list[int]) -> list[list[int]]:
|
||||||
|
return [
|
||||||
|
p
|
||||||
|
for p in collect_jump_paths_from_board(board)
|
||||||
|
if len(p) > 0 and p[-1] >= len(board) - 1
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def collect_jump_paths_from_board(board: list[int]) -> list[list[int]]:
|
||||||
|
space = JumpSpace.from_board(0, board)
|
||||||
|
if space is None:
|
||||||
|
return []
|
||||||
|
|
||||||
|
return space.jump_paths()
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE: the expensive way goes like this
|
||||||
|
# complete_paths = collect_complete_jump_paths_from_board(board)
|
||||||
|
|
||||||
|
# if len(complete_paths) == 0:
|
||||||
|
# return -1
|
||||||
|
|
||||||
|
# return min([len(p) - 1 for p in complete_paths])
|
||||||
|
|
||||||
|
|
||||||
|
def count_min_jumps_from_board(board: list[int]) -> int:
|
||||||
|
return len(collect_min_jumps_from_board(board))
|
||||||
|
|
||||||
|
|
||||||
|
def collect_min_jumps_from_board(board: list[int]) -> list[int]:
|
||||||
|
if len(board) < 3:
|
||||||
|
return list(range(1, len(board)))
|
||||||
|
|
||||||
|
jumps: list[int] = []
|
||||||
|
range_begin: int = 0
|
||||||
|
val = board[range_begin]
|
||||||
|
range_end: int = range_begin + val + 1
|
||||||
|
|
||||||
|
while range_end < len(board):
|
||||||
|
potential_jumps = board[range_begin:range_end]
|
||||||
|
|
||||||
|
scored_jumps = [
|
||||||
|
(val + range_begin + i, val, range_begin + i)
|
||||||
|
for i, val in enumerate(potential_jumps)
|
||||||
|
]
|
||||||
|
_, val, space = max(scored_jumps)
|
||||||
|
|
||||||
|
jumps.append(space)
|
||||||
|
|
||||||
|
range_begin = space
|
||||||
|
range_end = range_begin + val + 1
|
||||||
|
|
||||||
|
return jumps + [len(board) - 1]
|
||||||
|
|
||||||
|
|
||||||
|
def h_index(citations: list[int]) -> int:
|
||||||
|
last_qualified = None
|
||||||
|
|
||||||
|
for i, citation_count in enumerate(list(sorted(citations, reverse=True))):
|
||||||
|
if citation_count >= i + 1:
|
||||||
|
last_qualified = i + 1
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
return last_qualified or 0
|
||||||
|
|
||||||
|
|
||||||
|
class SlowRandomizedSet:
|
||||||
|
def __init__(self):
|
||||||
|
self._i: set[int] = set()
|
||||||
|
|
||||||
|
def insert(self, val: int) -> bool:
|
||||||
|
ok = val not in self._i
|
||||||
|
self._i.add(val)
|
||||||
|
return ok
|
||||||
|
|
||||||
|
def remove(self, val: int) -> bool:
|
||||||
|
if val in self._i:
|
||||||
|
self._i.remove(val)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def getRandom(self) -> int:
|
||||||
|
return random.choice(list(self._i))
|
||||||
|
|
||||||
|
|
||||||
|
class RandomizedSet:
|
||||||
|
def __init__(self):
|
||||||
|
self._l: list[int] = []
|
||||||
|
self._m: dict[int, int] = {}
|
||||||
|
|
||||||
|
def insert(self, val: int) -> bool:
|
||||||
|
if val in self._m:
|
||||||
|
return False
|
||||||
|
|
||||||
|
self._m[val] = len(self._l)
|
||||||
|
self._l.append(val)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def remove(self, val: int) -> bool:
|
||||||
|
if val not in self._m:
|
||||||
|
return False
|
||||||
|
|
||||||
|
val_loc = self._m[val]
|
||||||
|
last_val = self._l[-1]
|
||||||
|
self._l[val_loc] = last_val
|
||||||
|
self._m[last_val] = val_loc
|
||||||
|
|
||||||
|
self._l.pop()
|
||||||
|
self._m.pop(val)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def getRandom(self) -> int:
|
||||||
|
return random.choice(self._l)
|
||||||
|
|
||||||
|
|
||||||
|
class TrieNode(typing.NamedTuple):
|
||||||
|
value: str
|
||||||
|
kids: dict[str, "TrieNode"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_leaf(self) -> bool:
|
||||||
|
return "__self__" in self.kids
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def leaf(cls) -> "TrieNode":
|
||||||
|
return cls("__self__", {})
|
||||||
|
|
||||||
|
|
||||||
|
class Trie:
|
||||||
|
def __init__(self):
|
||||||
|
self._root_node = TrieNode("", {})
|
||||||
|
|
||||||
|
def insert(self, word: str) -> None:
|
||||||
|
if len(word) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
current_node = self._root_node
|
||||||
|
|
||||||
|
for prefix in [word[: i + 1] for i in range(len(word))]:
|
||||||
|
current_node.kids.setdefault(prefix, TrieNode(prefix, {}))
|
||||||
|
current_node = current_node.kids[prefix]
|
||||||
|
|
||||||
|
leaf = TrieNode.leaf()
|
||||||
|
current_node.kids[leaf.value] = leaf
|
||||||
|
|
||||||
|
def search(self, word: str) -> bool:
|
||||||
|
return self._has(word, prefix_ok=False)
|
||||||
|
|
||||||
|
def startsWith(self, prefix: str) -> bool:
|
||||||
|
return self._has(prefix, prefix_ok=True)
|
||||||
|
|
||||||
|
def _has(self, word: str, prefix_ok: bool) -> bool:
|
||||||
|
if len(word) == 0:
|
||||||
|
return True
|
||||||
|
|
||||||
|
reverse_path = [word[: i + 1] for i in range(len(word))][::-1]
|
||||||
|
current_node = self._root_node
|
||||||
|
|
||||||
|
while reverse_path and current_node is not None:
|
||||||
|
current_node = current_node.kids.get(reverse_path.pop())
|
||||||
|
|
||||||
|
return (
|
||||||
|
current_node is not None
|
||||||
|
and (current_node.is_leaf or prefix_ok)
|
||||||
|
and current_node.value == word
|
||||||
|
)
|
15
leetcode/test_roman.py
Normal file
15
leetcode/test_roman.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
import roman
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("n", "expected"),
|
||||||
|
[
|
||||||
|
(3, "III"),
|
||||||
|
(58, "LVIII"),
|
||||||
|
(1994, "MCMXCIV"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_int_to_roman(n: int, expected: str):
|
||||||
|
assert roman.i2r(n) == expected
|
46
leetcode/test_spiral_matrix.py
Normal file
46
leetcode/test_spiral_matrix.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
import spiral_matrix
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("matrix", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
[
|
||||||
|
["a", "b", "c"],
|
||||||
|
["d", "e", "f"],
|
||||||
|
["g", "h", "i"],
|
||||||
|
],
|
||||||
|
["a", "b", "c", "f", "i", "h", "g", "d", "e"],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[
|
||||||
|
[1, 2, 3, 4],
|
||||||
|
[5, 6, 7, 8],
|
||||||
|
[9, 10, 11, 12],
|
||||||
|
],
|
||||||
|
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
],
|
||||||
|
[] # noqa
|
||||||
|
+ [1, 2, 3, 4, 5, 6, 7, 8, 9] # right
|
||||||
|
+ [9, 9, 9] # down
|
||||||
|
+ [9, 8, 7, 6, 5, 4, 3, 2, 1] # left
|
||||||
|
+ [1, 1] # up
|
||||||
|
+ [1, 2, 3, 4, 5, 6, 7, 8] # right
|
||||||
|
+ [8] # down
|
||||||
|
+ [8, 7, 6, 5, 4, 3, 2] # left
|
||||||
|
+ [2, 3, 4, 5, 6, 7], # right
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_matrix_spiral(matrix, expected):
|
||||||
|
assert spiral_matrix.matrix_spiral(matrix) == expected
|
446
leetcode/test_stuff.py
Normal file
446
leetcode/test_stuff.py
Normal file
@@ -0,0 +1,446 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
import stuff
|
||||||
|
import stdlib
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("n", "expected"),
|
||||||
|
[
|
||||||
|
(0, 0),
|
||||||
|
(1, 1),
|
||||||
|
(5, 2),
|
||||||
|
(4, 2),
|
||||||
|
(8, 2),
|
||||||
|
(9, 3),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_find_sqrt_ish(n: int, expected: int):
|
||||||
|
assert stuff.find_sqrt_ish(n) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("ops", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
(
|
||||||
|
[
|
||||||
|
("new",),
|
||||||
|
("push", -2),
|
||||||
|
("push", 0),
|
||||||
|
("push", -3),
|
||||||
|
("getMin",),
|
||||||
|
("pop",),
|
||||||
|
("top",),
|
||||||
|
("getMin",),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
[
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
-3,
|
||||||
|
None,
|
||||||
|
0,
|
||||||
|
-2,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_min_stack(ops: list[tuple[str] | tuple[str, int]], expected: list[int | None]):
|
||||||
|
returned: list[int | None] = []
|
||||||
|
inst: stuff.MinStack | None = None
|
||||||
|
|
||||||
|
for op in ops:
|
||||||
|
if len(op) == 1:
|
||||||
|
if op[0] == "new":
|
||||||
|
inst = stuff.MinStack()
|
||||||
|
returned.append(None)
|
||||||
|
continue
|
||||||
|
|
||||||
|
returned.append(getattr(inst, op[0])())
|
||||||
|
continue
|
||||||
|
|
||||||
|
method, arg = op
|
||||||
|
returned.append(getattr(inst, method)(arg))
|
||||||
|
|
||||||
|
assert returned == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("head", "expected"),
|
||||||
|
[
|
||||||
|
(None, None),
|
||||||
|
(
|
||||||
|
stdlib.ListNode(
|
||||||
|
4, stdlib.ListNode(2, stdlib.ListNode(1, stdlib.ListNode(3)))
|
||||||
|
),
|
||||||
|
stdlib.ListNode(
|
||||||
|
1, stdlib.ListNode(2, stdlib.ListNode(3, stdlib.ListNode(4)))
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
stdlib.ListNode(
|
||||||
|
4,
|
||||||
|
stdlib.ListNode(
|
||||||
|
19,
|
||||||
|
stdlib.ListNode(
|
||||||
|
14,
|
||||||
|
stdlib.ListNode(
|
||||||
|
5,
|
||||||
|
stdlib.ListNode(
|
||||||
|
-3,
|
||||||
|
stdlib.ListNode(
|
||||||
|
1,
|
||||||
|
stdlib.ListNode(
|
||||||
|
8,
|
||||||
|
stdlib.ListNode(
|
||||||
|
5, stdlib.ListNode(11, stdlib.ListNode(15))
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
stdlib.ListNode(
|
||||||
|
-3,
|
||||||
|
stdlib.ListNode(
|
||||||
|
1,
|
||||||
|
stdlib.ListNode(
|
||||||
|
4,
|
||||||
|
stdlib.ListNode(
|
||||||
|
5,
|
||||||
|
stdlib.ListNode(
|
||||||
|
5,
|
||||||
|
stdlib.ListNode(
|
||||||
|
8,
|
||||||
|
stdlib.ListNode(
|
||||||
|
11,
|
||||||
|
stdlib.ListNode(
|
||||||
|
14, stdlib.ListNode(15, stdlib.ListNode(19))
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_sort_linked_list(
|
||||||
|
head: stdlib.LinkedListNode | None, expected: stdlib.LinkedListNode | None
|
||||||
|
):
|
||||||
|
if head is None:
|
||||||
|
assert stuff.sort_linked_list(head) == expected
|
||||||
|
return
|
||||||
|
|
||||||
|
assert stuff.linked_list_to_list(
|
||||||
|
stuff.sort_linked_list(head)
|
||||||
|
) == stuff.linked_list_to_list(expected)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("root", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
stdlib.Node(
|
||||||
|
1,
|
||||||
|
left=stdlib.Node(2, left=stdlib.Node(4), right=stdlib.Node(5)),
|
||||||
|
right=stdlib.Node(3, right=stdlib.Node(7)),
|
||||||
|
),
|
||||||
|
[1, None, 2, 3, None, 4, 5, 7, None],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_connect_binary_tree_right(
|
||||||
|
root: stdlib.ConnectableBinaryTreeNode | None, expected: list[int | None] | None
|
||||||
|
):
|
||||||
|
if expected is None:
|
||||||
|
assert root is None
|
||||||
|
return
|
||||||
|
|
||||||
|
connected, serialized = stuff.connect_binary_tree_right(root)
|
||||||
|
assert connected is not None
|
||||||
|
assert serialized == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("root", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
stdlib.Node(
|
||||||
|
4,
|
||||||
|
right=stdlib.Node(0),
|
||||||
|
left=stdlib.Node(9, right=stdlib.Node(1), left=stdlib.Node(5)),
|
||||||
|
),
|
||||||
|
1026,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_connect_binary_tree_sum_numbers(
|
||||||
|
root: stdlib.BinaryTreeNode | None, expected: int
|
||||||
|
):
|
||||||
|
assert stuff.sum_binary_tree_path_ints(root) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("inlist", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
[3, 5, 1, 6, 2, 0, 8, None, None, 7, 4],
|
||||||
|
stdlib.TreeNode(
|
||||||
|
3,
|
||||||
|
left=stdlib.TreeNode(
|
||||||
|
5,
|
||||||
|
left=stdlib.TreeNode(6),
|
||||||
|
right=stdlib.TreeNode(
|
||||||
|
2,
|
||||||
|
left=stdlib.TreeNode(7),
|
||||||
|
right=stdlib.TreeNode(4),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
right=stdlib.TreeNode(
|
||||||
|
1,
|
||||||
|
left=stdlib.TreeNode(0),
|
||||||
|
right=stdlib.TreeNode(8),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_binary_tree_from_list(
|
||||||
|
inlist: list[int | None], expected: stdlib.BinaryTreeNode | None
|
||||||
|
):
|
||||||
|
assert stuff.binary_tree_from_list(inlist) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("preorder", "inorder", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
[3, 9, 20, 15, 7],
|
||||||
|
[9, 3, 15, 20, 7],
|
||||||
|
stdlib.TreeNode(
|
||||||
|
3,
|
||||||
|
left=stdlib.TreeNode(9),
|
||||||
|
right=stdlib.TreeNode(
|
||||||
|
20, left=stdlib.TreeNode(15), right=stdlib.TreeNode(7)
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[-1],
|
||||||
|
[-1],
|
||||||
|
stdlib.TreeNode(-1),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[1, 2],
|
||||||
|
[1, 2],
|
||||||
|
stdlib.TreeNode(1, right=stdlib.TreeNode(2)),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_binary_tree_from_preorder_inorder(
|
||||||
|
preorder: list[int], inorder: list[int], expected: stdlib.BinaryTreeNode
|
||||||
|
):
|
||||||
|
assert stuff.binary_tree_from_preorder_inorder(preorder, inorder) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("board", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
[2, 3, 1, 1, 4],
|
||||||
|
[
|
||||||
|
[0, 1, 2, 3, 4],
|
||||||
|
[0, 1, 3, 4],
|
||||||
|
[0, 1, 4],
|
||||||
|
[0, 2, 3, 4],
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_collect_complete_jump_paths_from_board(
|
||||||
|
board: list[int], expected: list[list[int]]
|
||||||
|
):
|
||||||
|
assert list(sorted(stuff.collect_complete_jump_paths_from_board(board))) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("board", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
[2, 3, 1, 1, 4],
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[2, 3, 0, 1, 4],
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[1],
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[1, 2],
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[6, 2, 6, 1, 7, 9, 3, 5, 3, 7, 2, 8, 9, 4, 7, 7, 2, 2, 8, 4, 6, 6, 1, 3],
|
||||||
|
4,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[3, 4, 3, 2, 5, 4, 3],
|
||||||
|
3,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[3, 2, 1],
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[1, 2, 3],
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_count_min_jumps_from_board(board: list[int], expected: int):
|
||||||
|
assert stuff.count_min_jumps_from_board(board) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("citations", "expected"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
[3, 0, 6, 1, 5],
|
||||||
|
3,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[1, 3, 1],
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[100],
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_h_index(citations: list[int], expected: int):
|
||||||
|
assert stuff.h_index(citations) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("cls",),
|
||||||
|
[
|
||||||
|
(stuff.SlowRandomizedSet,),
|
||||||
|
(stuff.RandomizedSet,),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_randomized_set(cls: type[stuff.RandomizedSet] | type[stuff.SlowRandomizedSet]):
|
||||||
|
inst = cls()
|
||||||
|
|
||||||
|
assert inst.insert(1) is True
|
||||||
|
assert inst.remove(2) is False
|
||||||
|
assert inst.insert(2) is True
|
||||||
|
assert inst.getRandom() in (1, 2)
|
||||||
|
assert inst.remove(1) is True
|
||||||
|
assert inst.insert(2) is False
|
||||||
|
assert inst.getRandom() == 2
|
||||||
|
|
||||||
|
inst = cls()
|
||||||
|
|
||||||
|
assert inst.insert(1) is True
|
||||||
|
assert inst.insert(10) is True
|
||||||
|
assert inst.insert(20) is True
|
||||||
|
assert inst.insert(30) is True
|
||||||
|
|
||||||
|
seen: set[int] = set()
|
||||||
|
|
||||||
|
for _ in range(10_000):
|
||||||
|
seen.add(inst.getRandom())
|
||||||
|
|
||||||
|
assert seen == {1, 10, 20, 30}
|
||||||
|
|
||||||
|
# ["remove","remove","insert","getRandom","remove","insert"]
|
||||||
|
# [[0],[0],[0],[],[0],[0]]
|
||||||
|
|
||||||
|
inst = cls()
|
||||||
|
|
||||||
|
assert inst.remove(0) is False
|
||||||
|
assert inst.remove(0) is False
|
||||||
|
assert inst.insert(0) is True
|
||||||
|
assert inst.getRandom() == 0
|
||||||
|
assert inst.remove(0) is True
|
||||||
|
assert inst.insert(0) is True
|
||||||
|
|
||||||
|
# ["RandomizedSet","insert","insert","remove","insert","remove","getRandom"]
|
||||||
|
# [[],[0],[1],[0],[2],[1],[]]
|
||||||
|
|
||||||
|
inst = cls()
|
||||||
|
|
||||||
|
assert inst.insert(0) is True
|
||||||
|
assert inst.insert(1) is True
|
||||||
|
assert inst.remove(0) is True
|
||||||
|
assert inst.insert(2) is True
|
||||||
|
assert inst.remove(1) is True
|
||||||
|
assert inst.getRandom() == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_trie_single_letter():
|
||||||
|
trie = stuff.Trie()
|
||||||
|
|
||||||
|
assert trie.insert("a") is None
|
||||||
|
assert trie.search("a") is True
|
||||||
|
assert trie.startsWith("a") is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_trie_prefix_leaf():
|
||||||
|
trie = stuff.Trie()
|
||||||
|
|
||||||
|
assert trie.insert("apple") is None
|
||||||
|
assert trie.search("apple") is True
|
||||||
|
assert trie.search("app") is False
|
||||||
|
assert trie.startsWith("app") is True
|
||||||
|
assert trie.insert("app") is None
|
||||||
|
assert trie.search("app") is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_trie_two_letter():
|
||||||
|
trie = stuff.Trie()
|
||||||
|
|
||||||
|
assert trie.insert("ab") is None
|
||||||
|
assert trie.search("a") is False
|
||||||
|
assert trie.startsWith("a") is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_trie_busy():
|
||||||
|
trie = stuff.Trie()
|
||||||
|
|
||||||
|
assert trie.insert("app") is None
|
||||||
|
assert trie.insert("apple") is None
|
||||||
|
assert trie.insert("beer") is None
|
||||||
|
assert trie.insert("add") is None
|
||||||
|
assert trie.insert("jam") is None
|
||||||
|
assert trie.insert("rental") is None
|
||||||
|
assert trie.search("apps") is False
|
||||||
|
assert trie.search("app") is True
|
||||||
|
assert trie.search("ad") is False
|
||||||
|
assert trie.search("applepie") is False
|
||||||
|
assert trie.search("rest") is False
|
||||||
|
assert trie.search("jan") is False
|
||||||
|
assert trie.search("rent") is False
|
||||||
|
assert trie.search("beer") is True
|
||||||
|
assert trie.search("jam") is True
|
||||||
|
assert trie.startsWith("apps") is False
|
||||||
|
assert trie.startsWith("app") is True
|
||||||
|
assert trie.startsWith("ad") is True
|
||||||
|
assert trie.startsWith("applepie") is False
|
||||||
|
assert trie.startsWith("rest") is False
|
||||||
|
assert trie.startsWith("jan") is False
|
||||||
|
assert trie.startsWith("rent") is True
|
||||||
|
assert trie.startsWith("beer") is True
|
||||||
|
assert trie.startsWith("jam") is True
|
14
rustbyexample/.gitignore
vendored
14
rustbyexample/.gitignore
vendored
@@ -1,11 +1,3 @@
|
|||||||
/custom_types/*
|
/out/
|
||||||
/hello/*
|
/*.d/out
|
||||||
/primitives/*
|
/*.d/**/out
|
||||||
/variable_bindings/*
|
|
||||||
/types/*
|
|
||||||
|
|
||||||
!/custom_types/*.rs
|
|
||||||
!/hello/*.rs
|
|
||||||
!/primitives/*.rs
|
|
||||||
!/variable_bindings/*.rs
|
|
||||||
!/types/*.rs
|
|
||||||
|
18
rustbyexample/conversion.d/from_into.rs
Normal file
18
rustbyexample/conversion.d/from_into.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
use std::convert::From;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Number {
|
||||||
|
value: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<i32> for Number {
|
||||||
|
fn from(item: i32) -> Self {
|
||||||
|
Number { value: item }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let num = Number::from(30);
|
||||||
|
println!("My number is {:?}", num);
|
||||||
|
}
|
19
rustbyexample/conversion.d/from_into2.rs
Normal file
19
rustbyexample/conversion.d/from_into2.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use std::convert::Into;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Number {
|
||||||
|
value: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<Number> for i32 {
|
||||||
|
fn into(self) -> Number {
|
||||||
|
Number { value: self }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let int = 5;
|
||||||
|
let num: Number = int.into();
|
||||||
|
println!("My number is {:?}", num);
|
||||||
|
}
|
16
rustbyexample/conversion.d/string.rs
Normal file
16
rustbyexample/conversion.d/string.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
struct Circle {
|
||||||
|
radius: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Circle {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Circle of radius {}", self.radius)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let circle = Circle { radius: 6 };
|
||||||
|
println!("{}", circle.to_string());
|
||||||
|
}
|
7
rustbyexample/conversion.d/string2.rs
Normal file
7
rustbyexample/conversion.d/string2.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
fn main() {
|
||||||
|
let parsed: i32 = "5".parse().unwrap();
|
||||||
|
let turbo_parsed = "10".parse::<i32>().unwrap();
|
||||||
|
|
||||||
|
let sum = parsed + turbo_parsed;
|
||||||
|
println!("Sum: {:?}", sum);
|
||||||
|
}
|
28
rustbyexample/conversion.d/try_from_try_into.rs
Normal file
28
rustbyexample/conversion.d/try_from_try_into.rs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
use std::convert::TryFrom;
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct EvenNumber(i32);
|
||||||
|
|
||||||
|
impl TryFrom<i32> for EvenNumber {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
||||||
|
if value % 2 == 0 {
|
||||||
|
Ok(EvenNumber(value))
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8)));
|
||||||
|
assert_eq!(EvenNumber::try_from(5), Err(()));
|
||||||
|
|
||||||
|
let result: Result<EvenNumber, ()> = 8i32.try_into();
|
||||||
|
assert_eq!(result, Ok(EvenNumber(8)));
|
||||||
|
|
||||||
|
let result: Result<EvenNumber, ()> = 5i32.try_into();
|
||||||
|
assert_eq!(result, Err(()));
|
||||||
|
}
|
@@ -1,3 +1,4 @@
|
|||||||
|
#[allow(dead_code)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Person {
|
struct Person {
|
||||||
name: String,
|
name: String,
|
25
rustbyexample/expression.rs
Normal file
25
rustbyexample/expression.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#[allow(path_statements)]
|
||||||
|
#[allow(unused_must_use)]
|
||||||
|
fn main() {
|
||||||
|
let x = 5;
|
||||||
|
x;
|
||||||
|
x + 1;
|
||||||
|
15;
|
||||||
|
|
||||||
|
let x = 5u32;
|
||||||
|
|
||||||
|
let y = {
|
||||||
|
let x_squared = x * x;
|
||||||
|
let x_cube = x_squared * x;
|
||||||
|
|
||||||
|
x_cube + x_squared + x
|
||||||
|
};
|
||||||
|
|
||||||
|
let z = {
|
||||||
|
2 * x;
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("x is {:?}", x);
|
||||||
|
println!("y is {:?}", y);
|
||||||
|
println!("z is {:?}", z);
|
||||||
|
}
|
13
rustbyexample/flow_control.d/for.rs
Normal file
13
rustbyexample/flow_control.d/for.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
fn main() {
|
||||||
|
for n in 1..101 {
|
||||||
|
if n % 15 == 0 {
|
||||||
|
println!("fizzbuzz");
|
||||||
|
} else if n % 3 == 0 {
|
||||||
|
println!("fizz");
|
||||||
|
} else if n % 5 == 0 {
|
||||||
|
println!("buzz");
|
||||||
|
} else {
|
||||||
|
println!("{}", n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
rustbyexample/flow_control.d/for2.rs
Normal file
13
rustbyexample/flow_control.d/for2.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
fn main() {
|
||||||
|
for n in 1..=100 {
|
||||||
|
if n % 15 == 0 {
|
||||||
|
println!("fizzbuzz");
|
||||||
|
} else if n % 3 == 0 {
|
||||||
|
println!("fizz");
|
||||||
|
} else if n % 5 == 0 {
|
||||||
|
println!("buzz");
|
||||||
|
} else {
|
||||||
|
println!("{}", n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
rustbyexample/flow_control.d/for3.rs
Normal file
12
rustbyexample/flow_control.d/for3.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
fn main() {
|
||||||
|
let names = vec!["Bob", "Frank", "Ferris"];
|
||||||
|
|
||||||
|
for name in names.iter() {
|
||||||
|
match name {
|
||||||
|
&"Ferris" => println!("There is a rustacean among us!"),
|
||||||
|
_ => println!("Hello {}", name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("names: {:?}", names);
|
||||||
|
}
|
12
rustbyexample/flow_control.d/for4.rs
Normal file
12
rustbyexample/flow_control.d/for4.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
fn main() {
|
||||||
|
let names = vec!["Bob", "Frank", "Ferris"];
|
||||||
|
|
||||||
|
for name in names.into_iter() {
|
||||||
|
match name {
|
||||||
|
"Ferris" => println!("There is a rustacean among us!"),
|
||||||
|
_ => println!("Hello {}", name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//println!("names: {:?}", names);
|
||||||
|
}
|
12
rustbyexample/flow_control.d/for5.rs
Normal file
12
rustbyexample/flow_control.d/for5.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut names = vec!["Bob", "Frank", "Ferris"];
|
||||||
|
|
||||||
|
for name in names.iter_mut() {
|
||||||
|
*name = match name {
|
||||||
|
&mut "Ferris" => "There is a rustacean among us!",
|
||||||
|
_ => "Hello",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("names: {:?}", names);
|
||||||
|
}
|
23
rustbyexample/flow_control.d/if_else.rs
Normal file
23
rustbyexample/flow_control.d/if_else.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
fn main() {
|
||||||
|
let n = 5;
|
||||||
|
|
||||||
|
if n < 0 {
|
||||||
|
print!("{} is negative", n);
|
||||||
|
} else if n > 0 {
|
||||||
|
print!("{} is positive", n);
|
||||||
|
} else {
|
||||||
|
print!("{} is zero", n);
|
||||||
|
}
|
||||||
|
|
||||||
|
let big_n = if n < 10 && n > -10 {
|
||||||
|
println!(", and is a small number, increase net-fold");
|
||||||
|
|
||||||
|
10 * n
|
||||||
|
} else {
|
||||||
|
println!(", and is a big number, halve the number");
|
||||||
|
|
||||||
|
n / 2
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{} -> {}", n, big_n);
|
||||||
|
}
|
25
rustbyexample/flow_control.d/if_let.rs
Normal file
25
rustbyexample/flow_control.d/if_let.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
fn main() {
|
||||||
|
let number = Some(7);
|
||||||
|
let letter: Option<i32> = None;
|
||||||
|
let emoticon: Option<i32> = None;
|
||||||
|
|
||||||
|
if let Some(i) = number {
|
||||||
|
println!("Matched {:?}!", i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(i) = letter {
|
||||||
|
println!("Matched {:?}!", i);
|
||||||
|
} else {
|
||||||
|
println!("Didn't match a number. Let's go with a letter!");
|
||||||
|
}
|
||||||
|
|
||||||
|
let i_like_letters = false;
|
||||||
|
|
||||||
|
if let Some(i) = emoticon {
|
||||||
|
println!("Matched {:?}!", i);
|
||||||
|
} else if i_like_letters {
|
||||||
|
println!("Didn't match a number. Let's go with a letter!");
|
||||||
|
} else {
|
||||||
|
println!("I don't like letters. Let's go with an emoticon :)!");
|
||||||
|
}
|
||||||
|
}
|
27
rustbyexample/flow_control.d/if_let2.rs
Normal file
27
rustbyexample/flow_control.d/if_let2.rs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
enum Foo {
|
||||||
|
Bar,
|
||||||
|
Baz,
|
||||||
|
Qux(u32),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = Foo::Bar;
|
||||||
|
let b = Foo::Baz;
|
||||||
|
let c = Foo::Qux(100);
|
||||||
|
|
||||||
|
if let Foo::Bar = a {
|
||||||
|
println!("a is a foobar");
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Foo::Bar = b {
|
||||||
|
println!("b is foobar");
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Foo::Qux(value) = c {
|
||||||
|
println!("c is {}", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Foo::Qux(value @ 100) = c {
|
||||||
|
println!("c is one hundred (value: {:?})", value);
|
||||||
|
}
|
||||||
|
}
|
11
rustbyexample/flow_control.d/if_let3.rs
Normal file
11
rustbyexample/flow_control.d/if_let3.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
enum Foo {
|
||||||
|
Bar,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = Foo::Bar;
|
||||||
|
|
||||||
|
if let Foo::Bar = a {
|
||||||
|
println!("a is foobar");
|
||||||
|
}
|
||||||
|
}
|
16
rustbyexample/flow_control.d/let_else.rs
Normal file
16
rustbyexample/flow_control.d/let_else.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
fn get_count_item(s: &str) -> (u64, &str) {
|
||||||
|
let mut it = s.split(' ');
|
||||||
|
let (Some(count_str), Some(item)) = (it.next(), it.next()) else {
|
||||||
|
panic!("Can't segment count item pair: '{s}'");
|
||||||
|
};
|
||||||
|
let Ok(count) = u64::from_str(count_str) else {
|
||||||
|
panic!("Can't parse integer: '{count_str}'");
|
||||||
|
};
|
||||||
|
(count, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!(get_count_item("3 chairs"), (3, "chairs"));
|
||||||
|
}
|
18
rustbyexample/flow_control.d/loop.d/nested.rs
Normal file
18
rustbyexample/flow_control.d/loop.d/nested.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#![allow(unreachable_code)]
|
||||||
|
#![allow(unused_labels)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
'outer: loop {
|
||||||
|
println!("Entered the outer loop");
|
||||||
|
|
||||||
|
'inner: loop {
|
||||||
|
println!("Entered the inner loop");
|
||||||
|
|
||||||
|
break 'outer;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("This point will never be reached");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Exited the outer loop");
|
||||||
|
}
|
13
rustbyexample/flow_control.d/loop.d/return.rs
Normal file
13
rustbyexample/flow_control.d/loop.d/return.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut counter = 0;
|
||||||
|
|
||||||
|
let result = loop {
|
||||||
|
counter += 1;
|
||||||
|
|
||||||
|
if counter == 10 {
|
||||||
|
break counter * 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(result, 20);
|
||||||
|
}
|
23
rustbyexample/flow_control.d/loop.rs
Normal file
23
rustbyexample/flow_control.d/loop.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut count = 0u32;
|
||||||
|
|
||||||
|
println!("Let's count until infinity!");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
if count == 3 {
|
||||||
|
println!("three");
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", count);
|
||||||
|
|
||||||
|
if count == 5 {
|
||||||
|
println!("OK, that's enough");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
rustbyexample/flow_control.d/match.d/binding.rs
Normal file
14
rustbyexample/flow_control.d/match.d/binding.rs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
fn age() -> u32 {
|
||||||
|
15
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Tell me what type of person you are");
|
||||||
|
|
||||||
|
match age() {
|
||||||
|
0 => println!("I haven't celebrated my first birthday yet"),
|
||||||
|
n @ 1..=12 => println!("I'm a child of age {:?}", n),
|
||||||
|
n @ 13..=19 => println!("I'm a teen of age {:?}", n),
|
||||||
|
n => println!("I'm an old person of age {:?}", n),
|
||||||
|
}
|
||||||
|
}
|
11
rustbyexample/flow_control.d/match.d/binding2.rs
Normal file
11
rustbyexample/flow_control.d/match.d/binding2.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fn some_number() -> Option<u32> {
|
||||||
|
Some(42)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
match some_number() {
|
||||||
|
Some(n @ 42) => println!("The Answer: {}!", n),
|
||||||
|
Some(n) => println!("Not interesting... {}", n),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,30 @@
|
|||||||
|
#[allow(dead_code)]
|
||||||
|
enum Color {
|
||||||
|
Red,
|
||||||
|
Blue,
|
||||||
|
Green,
|
||||||
|
RGB(u32, u32, u32),
|
||||||
|
HSV(u32, u32, u32),
|
||||||
|
HSL(u32, u32, u32),
|
||||||
|
CMY(u32, u32, u32),
|
||||||
|
CMYK(u32, u32, u32, u32),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let color = Color::RGB(122, 17, 40);
|
||||||
|
|
||||||
|
println!("What color is it?");
|
||||||
|
match color {
|
||||||
|
Color::Red => println!("The color is Red!"),
|
||||||
|
Color::Blue => println!("The color is Blue!"),
|
||||||
|
Color::Green => println!("The color is Green!"),
|
||||||
|
Color::RGB(r, g, b) => println!("Red: {}, green: {}, and blue: {}!", r, g, b),
|
||||||
|
Color::HSV(h, s, v) => println!("Hue: {}, saturation: {}, value: {}!", h, s, v),
|
||||||
|
Color::HSL(h, s, l) => println!("Hue: {}, saturation: {}, lightness: {}!", h, s, l),
|
||||||
|
Color::CMY(c, m, y) => println!("Cyan: {}, magenta: {}, yellow: {}!", c, m, y),
|
||||||
|
Color::CMYK(c, m, y, k) => println!(
|
||||||
|
"Cyan: {}, magenta: {}, yellow: {}, key (black): {}!",
|
||||||
|
c, m, y, k
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,29 @@
|
|||||||
|
fn main() {
|
||||||
|
let reference = &4;
|
||||||
|
|
||||||
|
match reference {
|
||||||
|
&val => println!("Got a value via destructuring: {:?}", val),
|
||||||
|
}
|
||||||
|
|
||||||
|
match *reference {
|
||||||
|
val => println!("Got a value via dereferencing: {:?}", val),
|
||||||
|
}
|
||||||
|
|
||||||
|
let _not_a_reference = 3;
|
||||||
|
|
||||||
|
let ref _is_a_reference = 3;
|
||||||
|
|
||||||
|
let value = 5;
|
||||||
|
let mut mut_value = 6;
|
||||||
|
|
||||||
|
match value {
|
||||||
|
ref r => println!("Got a reference to a value: {:?}", r),
|
||||||
|
}
|
||||||
|
|
||||||
|
match mut_value {
|
||||||
|
ref mut m => {
|
||||||
|
*m += 10;
|
||||||
|
println!("We added 10. `mut_value`: {:?}", m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,27 @@
|
|||||||
|
fn main() {
|
||||||
|
let array = [1, -2, 6];
|
||||||
|
|
||||||
|
match array {
|
||||||
|
[0, second, third] => println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third),
|
||||||
|
|
||||||
|
[1, _, third] => println!(
|
||||||
|
"array[0] = 1, array[2] = {} and array[1] was ignored",
|
||||||
|
third
|
||||||
|
),
|
||||||
|
|
||||||
|
[-1, second, ..] => println!(
|
||||||
|
"array[0] = -1, array[1] = {} and all the other ones were ignored",
|
||||||
|
second
|
||||||
|
),
|
||||||
|
|
||||||
|
[3, second, tail @ ..] => println!(
|
||||||
|
"array[0] = 3, array[1] = {} and the other elements were {:?}",
|
||||||
|
second, tail
|
||||||
|
),
|
||||||
|
|
||||||
|
[first, middle @ .., last] => println!(
|
||||||
|
"array[0] = {}, middle = {:?}, array[2] = {}",
|
||||||
|
first, middle, last
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,19 @@
|
|||||||
|
fn main() {
|
||||||
|
struct Foo {
|
||||||
|
x: (u32, u32),
|
||||||
|
y: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
let foo = Foo { x: (1, 2), y: 3 };
|
||||||
|
|
||||||
|
match foo {
|
||||||
|
Foo { x: (1, b), y } => println!("First of x is 1, b = {}, y = {}", b, y),
|
||||||
|
Foo { y: 2, x: i } => println!("y is 2, i = {:?}", i),
|
||||||
|
Foo { y, .. } => println!("y = {}, we don't care about x", y),
|
||||||
|
}
|
||||||
|
|
||||||
|
let faa = Foo { x: (1, 2), y: 3 };
|
||||||
|
|
||||||
|
let Foo { x: x0, y: y0 } = faa;
|
||||||
|
println!("Outside: x0 = {x0:?}, y0 = {y0}");
|
||||||
|
}
|
@@ -0,0 +1,12 @@
|
|||||||
|
fn main() {
|
||||||
|
let triple = (0, -2, 3);
|
||||||
|
|
||||||
|
println!("Tell me about {:?}", triple);
|
||||||
|
match triple {
|
||||||
|
(0, y, z) => println!("First is `0`, `y` is {:?}, and `z` is {:?}", y, z),
|
||||||
|
(1, ..) => println!("First is `1` and the rest doesn't matter"),
|
||||||
|
(.., 2) => println!("last is `2` and the rest doesn't matter"),
|
||||||
|
(3, .., 4) => println!("First is `3`, last is `4`, and the rest doesn't matter"),
|
||||||
|
_ => println!("It doesn't matter what they are"),
|
||||||
|
}
|
||||||
|
}
|
16
rustbyexample/flow_control.d/match.d/guard.rs
Normal file
16
rustbyexample/flow_control.d/match.d/guard.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#[allow(dead_code)]
|
||||||
|
enum Temperature {
|
||||||
|
Celsius(i32),
|
||||||
|
Fahrenheit(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let temperature = Temperature::Celsius(35);
|
||||||
|
|
||||||
|
match temperature {
|
||||||
|
Temperature::Celsius(t) if t > 30 => println!("{}C is above 30 Celsius", t),
|
||||||
|
Temperature::Celsius(t) => println!("{}C is below 30 Celsius", t),
|
||||||
|
Temperature::Fahrenheit(t) if t > 86 => println!("{}F is above 86 Fahrenheit", t),
|
||||||
|
Temperature::Fahrenheit(t) => println!("{}F is below 86 Fahrenheit", t),
|
||||||
|
}
|
||||||
|
}
|
9
rustbyexample/flow_control.d/match.d/guard2.rs
Normal file
9
rustbyexample/flow_control.d/match.d/guard2.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
fn main() {
|
||||||
|
let number: u8 = 4;
|
||||||
|
|
||||||
|
match number {
|
||||||
|
i if i == 0 => println!("Zero"),
|
||||||
|
i if i > 0 => println!("Greater than zero"),
|
||||||
|
_ => unreachable!("Should never happen."),
|
||||||
|
}
|
||||||
|
}
|
19
rustbyexample/flow_control.d/match.rs
Normal file
19
rustbyexample/flow_control.d/match.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
fn main() {
|
||||||
|
let number = 13;
|
||||||
|
|
||||||
|
println!("Tell me about {}", number);
|
||||||
|
match number {
|
||||||
|
1 => println!("One!"),
|
||||||
|
2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
|
||||||
|
13..=19 => println!("A teen"),
|
||||||
|
_ => println!("Ain't special"),
|
||||||
|
}
|
||||||
|
|
||||||
|
let boolean = true;
|
||||||
|
let binary = match boolean {
|
||||||
|
false => 0,
|
||||||
|
true => 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{} -> {}", boolean, binary);
|
||||||
|
}
|
17
rustbyexample/flow_control.d/while.rs
Normal file
17
rustbyexample/flow_control.d/while.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut n = 1;
|
||||||
|
|
||||||
|
while n < 101 {
|
||||||
|
if n % 15 == 0 {
|
||||||
|
println!("fizzbuzz");
|
||||||
|
} else if n % 3 == 0 {
|
||||||
|
println!("fizz");
|
||||||
|
} else if n % 5 == 0 {
|
||||||
|
println!("buzz");
|
||||||
|
} else {
|
||||||
|
println!("{}", n);
|
||||||
|
}
|
||||||
|
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
}
|
13
rustbyexample/flow_control.d/while_let.rs
Normal file
13
rustbyexample/flow_control.d/while_let.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut optional = Some(0);
|
||||||
|
|
||||||
|
while let Some(i) = optional {
|
||||||
|
if i > 9 {
|
||||||
|
println!("Greater than 9, quit!");
|
||||||
|
optional = None;
|
||||||
|
} else {
|
||||||
|
println!("`i` is `{:?}`. Try again.", i);
|
||||||
|
optional = Some(i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
rustbyexample/fn.d/closures.d/anonymity.rs
Normal file
14
rustbyexample/fn.d/closures.d/anonymity.rs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
fn apply<F>(f: F)
|
||||||
|
where
|
||||||
|
F: Fn(),
|
||||||
|
{
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = 7;
|
||||||
|
|
||||||
|
let print = || println!("{}", x);
|
||||||
|
|
||||||
|
apply(print);
|
||||||
|
}
|
36
rustbyexample/fn.d/closures.d/capture.rs
Normal file
36
rustbyexample/fn.d/closures.d/capture.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
fn main() {
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
|
let color = String::from("green");
|
||||||
|
|
||||||
|
let print = || println!("`color`: {}", color);
|
||||||
|
|
||||||
|
print();
|
||||||
|
|
||||||
|
let _reborrow = &color;
|
||||||
|
print();
|
||||||
|
|
||||||
|
let _color_moved = color;
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
let mut inc = || {
|
||||||
|
count += 1;
|
||||||
|
println!("`count`: {}", count);
|
||||||
|
};
|
||||||
|
|
||||||
|
inc();
|
||||||
|
|
||||||
|
inc();
|
||||||
|
|
||||||
|
let _count_reborrowed = &mut count;
|
||||||
|
|
||||||
|
let movable = Box::new(3);
|
||||||
|
|
||||||
|
let consume = || {
|
||||||
|
println!("`movable`: {:?}", movable);
|
||||||
|
mem::drop(movable);
|
||||||
|
};
|
||||||
|
|
||||||
|
consume();
|
||||||
|
}
|
8
rustbyexample/fn.d/closures.d/capture2.rs
Normal file
8
rustbyexample/fn.d/closures.d/capture2.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fn main() {
|
||||||
|
let haystack = vec![1, 2, 3];
|
||||||
|
|
||||||
|
let contains = move |needle| haystack.contains(needle);
|
||||||
|
|
||||||
|
println!("{}", contains(&1));
|
||||||
|
println!("{}", contains(&4));
|
||||||
|
}
|
36
rustbyexample/fn.d/closures.d/input_parameters.rs
Normal file
36
rustbyexample/fn.d/closures.d/input_parameters.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
fn apply<F>(f: F)
|
||||||
|
where
|
||||||
|
F: FnOnce(),
|
||||||
|
{
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_to_3<F>(f: F) -> i32
|
||||||
|
where
|
||||||
|
F: Fn(i32) -> i32,
|
||||||
|
{
|
||||||
|
f(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
|
let greeting = "hello";
|
||||||
|
let mut farewell = "goodbye".to_owned();
|
||||||
|
|
||||||
|
let diary = || {
|
||||||
|
println!("I said {}.", greeting);
|
||||||
|
|
||||||
|
farewell.push_str("!!!");
|
||||||
|
println!("Then I screamed {}.", farewell);
|
||||||
|
println!("Now I can sleep. zzzzz");
|
||||||
|
|
||||||
|
mem::drop(farewell);
|
||||||
|
};
|
||||||
|
|
||||||
|
apply(diary);
|
||||||
|
|
||||||
|
let double = |x| 2 * x;
|
||||||
|
|
||||||
|
println!("3 doubled: {}", apply_to_3(double));
|
||||||
|
}
|
12
rustbyexample/fn.d/closures.rs
Normal file
12
rustbyexample/fn.d/closures.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
fn main() {
|
||||||
|
let outer_var = 42;
|
||||||
|
|
||||||
|
let closure_annotated = |i: i32| -> i32 { i + outer_var };
|
||||||
|
let closure_inferred = |i| i + outer_var;
|
||||||
|
|
||||||
|
println!("closure_annotated: {}", closure_annotated(1));
|
||||||
|
println!("closure_inferred: {}", closure_inferred(1));
|
||||||
|
|
||||||
|
let one = || 1;
|
||||||
|
println!("closure returning one: {}", one());
|
||||||
|
}
|
74
rustbyexample/fn.d/methods.rs
Normal file
74
rustbyexample/fn.d/methods.rs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
struct Point {
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Point {
|
||||||
|
fn origin() -> Point {
|
||||||
|
Point { x: 0.0, y: 0.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new(x: f64, y: f64) -> Point {
|
||||||
|
Point { x: x, y: y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Rectangle {
|
||||||
|
p1: Point,
|
||||||
|
p2: Point,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Rectangle {
|
||||||
|
fn area(&self) -> f64 {
|
||||||
|
let Point { x: x1, y: y1 } = self.p1;
|
||||||
|
let Point { x: x2, y: y2 } = self.p2;
|
||||||
|
|
||||||
|
((x1 - x2) * (y1 - y2)).abs()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn perimeter(&self) -> f64 {
|
||||||
|
let Point { x: x1, y: y1 } = self.p1;
|
||||||
|
let Point { x: x2, y: y2 } = self.p2;
|
||||||
|
|
||||||
|
2.0 * ((x1 - x2).abs() + (y1 - y2).abs())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate(&mut self, x: f64, y: f64) {
|
||||||
|
self.p1.x += x;
|
||||||
|
self.p2.x += x;
|
||||||
|
|
||||||
|
self.p1.y += y;
|
||||||
|
self.p2.y += y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Pair(Box<i32>, Box<i32>);
|
||||||
|
|
||||||
|
impl Pair {
|
||||||
|
fn destroy(self) {
|
||||||
|
let Pair(first, second) = self;
|
||||||
|
|
||||||
|
println!("Destroying Pair({}, {})", first, second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let rectangle = Rectangle {
|
||||||
|
p1: Point::origin(),
|
||||||
|
p2: Point::new(3.0, 4.0),
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Rectangle perimeter: {}", rectangle.perimeter());
|
||||||
|
println!("Rectangle area: {}", rectangle.area());
|
||||||
|
|
||||||
|
let mut square = Rectangle {
|
||||||
|
p1: Point::origin(),
|
||||||
|
p2: Point::new(1.0, 1.0),
|
||||||
|
};
|
||||||
|
|
||||||
|
square.translate(1.0, 1.0);
|
||||||
|
|
||||||
|
let pair = Pair(Box::new(1), Box::new(2));
|
||||||
|
|
||||||
|
pair.destroy();
|
||||||
|
}
|
29
rustbyexample/fn.rs
Normal file
29
rustbyexample/fn.rs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
fn main() {
|
||||||
|
fizzbuzz_to(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
|
||||||
|
if rhs == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
lhs % rhs == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fizzbuzz(n: u32) -> () {
|
||||||
|
if is_divisible_by(n, 15) {
|
||||||
|
println!("fizzbuzz");
|
||||||
|
} else if is_divisible_by(n, 3) {
|
||||||
|
println!("fizz");
|
||||||
|
} else if is_divisible_by(n, 5) {
|
||||||
|
println!("buzz");
|
||||||
|
} else {
|
||||||
|
println!("{}", n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fizzbuzz_to(n: u32) {
|
||||||
|
for n in 1..=n {
|
||||||
|
fizzbuzz(n);
|
||||||
|
}
|
||||||
|
}
|
@@ -1,3 +1,4 @@
|
|||||||
|
#[allow(dead_code)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Person<'a> {
|
struct Person<'a> {
|
||||||
name: &'a str,
|
name: &'a str,
|
@@ -20,6 +20,6 @@ impl fmt::Display for List {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let v = List(vec![1, 2, 3]);
|
let v = List(vec![6, 0, 6, 8, 0, 8, 9, 1, 1, 1, 3, 1, 2]);
|
||||||
println!("{}", v);
|
println!("{}", v);
|
||||||
}
|
}
|
@@ -22,5 +22,14 @@ fn main() {
|
|||||||
println!("borrow a section of the array as a slice");
|
println!("borrow a section of the array as a slice");
|
||||||
analyze_slice(&ys[1..4]);
|
analyze_slice(&ys[1..4]);
|
||||||
|
|
||||||
// println!("{}", xs[5]);
|
let empty_array: [u32; 0] = [];
|
||||||
|
assert_eq!(&empty_array, &[]);
|
||||||
|
assert_eq!(&empty_array, &[][..]);
|
||||||
|
|
||||||
|
for i in 0..xs.len() + 1 {
|
||||||
|
match xs.get(i) {
|
||||||
|
Some(xval) => println!("{}: {}", i, xval),
|
||||||
|
None => println!("Slow down! {} is too far!", i),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -3,6 +3,8 @@ fn main() {
|
|||||||
|
|
||||||
println!("1 - 2 = {}", 1i32 - 2);
|
println!("1 - 2 = {}", 1i32 - 2);
|
||||||
|
|
||||||
|
println!("1e4 is {}, -2.5e-3 is {}", 1e4, -2.5e-3);
|
||||||
|
|
||||||
println!("true AND false is {}", true && false);
|
println!("true AND false is {}", true && false);
|
||||||
println!("true OR false is {}", true || false);
|
println!("true OR false is {}", true || false);
|
||||||
println!("NOT true is {}", !true);
|
println!("NOT true is {}", !true);
|
@@ -1,3 +1,5 @@
|
|||||||
|
#[allow(unused_variables)]
|
||||||
|
#[allow(unused_assignments)]
|
||||||
fn main() {
|
fn main() {
|
||||||
let logical: bool = true;
|
let logical: bool = true;
|
||||||
|
|
15
rustbyexample/types.d/alias.rs
Normal file
15
rustbyexample/types.d/alias.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
type NanoSecond = u64;
|
||||||
|
type Inch = u64;
|
||||||
|
type U64 = u64;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let nanoseconds: NanoSecond = 5 as U64;
|
||||||
|
let inches: Inch = 2 as U64;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"{} nanoseconds + {} inches = {} unit?",
|
||||||
|
nanoseconds,
|
||||||
|
inches,
|
||||||
|
nanoseconds + inches
|
||||||
|
);
|
||||||
|
}
|
@@ -1,15 +0,0 @@
|
|||||||
type NanoSecond = u64;
|
|
||||||
type Inch = u64;
|
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
type u64_t = u64;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let nanoseconds: NanoSecond = 5 as u64_t;
|
|
||||||
let inches: Inch = 2 as u64_t;
|
|
||||||
|
|
||||||
println!("{} nanoseconds + {} inches = {} unit?",
|
|
||||||
nanoseconds,
|
|
||||||
inches,
|
|
||||||
nanoseconds + inches);
|
|
||||||
}
|
|
@@ -13,4 +13,3 @@ fn main() {
|
|||||||
let shadowed_binding = 2;
|
let shadowed_binding = 2;
|
||||||
println!("shadowed in outer block: {}", shadowed_binding);
|
println!("shadowed in outer block: {}", shadowed_binding);
|
||||||
}
|
}
|
||||||
|
|
@@ -11,5 +11,5 @@ fn main() {
|
|||||||
|
|
||||||
let _unused_variable = 3u32;
|
let _unused_variable = 3u32;
|
||||||
|
|
||||||
let noisy_unused_variable = 2u32;
|
let _noisy_unused_variable = 2u32;
|
||||||
}
|
}
|
Reference in New Issue
Block a user