Skip to content

Commit facdf84

Browse files
committed
Initial commit
Signed-off-by: Savio Sena <[email protected]>
0 parents  commit facdf84

File tree

7 files changed

+175
-0
lines changed

7 files changed

+175
-0
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
debug/
4+
target/
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8+
# Cargo.lock
9+
10+
# These are backup files generated by rustfmt
11+
**/*.rs.bk
12+
13+
# MSVC Windows builds of rustc generate these, which store debugging information
14+
*.pdb

Cargo.lock

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Copyright (C) 2024, Savio Sena <[email protected]>
2+
3+
[package]
4+
name = "leetcode"
5+
version = "0.1.0"
6+
edition = "2021"
7+
license = "MIT"
8+
readme = "README.md"
9+
description = "My solutions to various Leetcode puzzles."
10+
homepage = "https://github.com/sav/leetcode"
11+
repository = "[email protected]:sav/leetcode.git"
12+
keywords = ["leetcode", "algorithm", "data-structure", "competitive"]
13+
14+
[lib]
15+
name = "leetcode"
16+
path = "src/lib/leetcode.rs"
17+
18+
[dependencies]
19+
alg = { git = "https://github.com/sav/alg.git", branch = "master" }

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Savio Sena <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Leetcode
2+
3+
My solutions to various Leetcode puzzles.

src/bin/example.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// example.rs, My solutions to various Leetcode puzzles.
2+
// Copyright (C) 2024, Savio Sena <[email protected]>
3+
4+
use leetcode::{*};
5+
6+
fn main() {
7+
for line in std::io::stdin().lock().trimmed_lines() {
8+
println!("-> {line}");
9+
}
10+
}

src/lib/leetcode.rs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// leetcode.rs, My solutions to various Leetcode puzzles.
2+
// Copyright (C) 2024, Savio Sena <[email protected]>
3+
4+
#[allow(unused_imports)]
5+
use std::io::{self, BufRead, Read, Stdin};
6+
use std::str::FromStr;
7+
8+
/// A trait for reading lines from a buffer, providing methods for various line operations.
9+
pub trait LineRead {
10+
fn trimmed_lines(&mut self) -> impl Iterator<Item = String>;
11+
12+
fn read_line_as<T: FromStr>(&mut self) -> Result<T, T::Err>;
13+
14+
fn skip_line(&mut self);
15+
}
16+
17+
impl<T: BufRead> LineRead for T {
18+
fn trimmed_lines(&mut self) -> impl Iterator<Item = String> {
19+
self.lines()
20+
.map(|l| l.expect("should read line from buffer").trim().to_string())
21+
}
22+
23+
fn read_line_as<S: FromStr>(&mut self) -> Result<S, S::Err> {
24+
let mut buffer = String::new();
25+
self.read_line(&mut buffer)
26+
.expect("should read line from buffer");
27+
buffer.trim().to_string().parse::<S>()
28+
}
29+
30+
fn skip_line(&mut self) {
31+
self.read_line(&mut "".to_string())
32+
.expect("should read line from buffer");
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
use std::io::Cursor;
40+
41+
#[test]
42+
fn test_iter_lines() {
43+
let mut cursor = Cursor::new(" 0 \n1 \n 2\n \n");
44+
let mut iter = cursor.trimmed_lines();
45+
assert_eq!(iter.next(), Some("0".to_string()));
46+
assert_eq!(iter.next(), Some("1".to_string()));
47+
assert_eq!(iter.next(), Some("2".to_string()));
48+
assert_eq!(iter.next(), Some("".to_string()));
49+
}
50+
51+
#[test]
52+
fn test_read_line_as() {
53+
let mut cursor = Cursor::new(" 0 \n1 \n 2\n");
54+
assert_eq!(cursor.read_line_as::<i32>(), Ok(0));
55+
assert_eq!(cursor.read_line_as::<i32>(), Ok(1));
56+
assert_eq!(cursor.read_line_as::<i32>(), Ok(2));
57+
58+
let mut cursor = Cursor::new(" 0 \n 3.1415 \n");
59+
assert_eq!(cursor.read_line_as::<f64>(), Ok(0.0));
60+
assert_eq!(cursor.read_line_as::<f64>(), Ok(3.1415));
61+
62+
let mut cursor = Cursor::new("0\n1\n 2 \n");
63+
let i: Result<i32, _> = cursor.read_line_as();
64+
assert_eq!(i, Ok(0));
65+
66+
let f: Result<f64, _> = cursor.read_line_as();
67+
assert_eq!(f, Ok(1.0_f64));
68+
69+
let s: Result<String, _> = cursor.read_line_as();
70+
assert_eq!(s, Ok("2".to_string()));
71+
}
72+
73+
#[test]
74+
#[should_panic]
75+
fn test_read_line_as_panic() {
76+
let mut cursor = Cursor::new(" \n \n");
77+
assert_eq!(cursor.read_line_as::<f64>(), Ok(0.0));
78+
}
79+
80+
#[test]
81+
fn test_skip_line() {
82+
let mut cursor = Cursor::new("\n\n");
83+
cursor.skip_line();
84+
cursor.skip_line();
85+
86+
let mut cursor = Cursor::new("\n");
87+
cursor.skip_line();
88+
cursor.skip_line();
89+
cursor.skip_line();
90+
cursor.skip_line();
91+
cursor.skip_line();
92+
}
93+
}

0 commit comments

Comments
 (0)