mirror of
https://github.com/osmarks/random-stuff
synced 2025-09-12 07:16:00 +00:00
declassify some projects
This commit is contained in:
85
code-guessing/entry-rs/src/entry_impl.rs
Normal file
85
code-guessing/entry-rs/src/entry_impl.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
pub fn entry(s: &str) -> i32 {
|
||||
let digit = |c: u8| (c as i32) - 48;
|
||||
let mut acc = 0;
|
||||
let mut pos = 0;
|
||||
let b = s.as_bytes();
|
||||
loop {
|
||||
match b[pos] {
|
||||
b'+' => {
|
||||
acc += {
|
||||
pos += 1;
|
||||
let mut acc = 0;
|
||||
while (pos + 1) < b.len() && (b[pos + 1] == b'/' || b[pos + 1] == b'*') {
|
||||
println!("DIV or MUL {} {} {}", b[pos], b[pos + 1], b[pos + 2]);
|
||||
if acc == 0 {
|
||||
acc = digit(b[pos])
|
||||
}
|
||||
if b[pos + 1] == b'/' {
|
||||
acc /= digit(b[pos + 2])
|
||||
} else {
|
||||
acc *= digit(b[pos + 2])
|
||||
}
|
||||
pos += 2;
|
||||
}
|
||||
if acc == 0 {
|
||||
digit(b[pos])
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
};
|
||||
pos += 1;
|
||||
},
|
||||
b'-' => {
|
||||
acc -= {
|
||||
pos += 1;
|
||||
let mut acc = -1;
|
||||
while (pos + 1) < b.len() && (b[pos + 1] == b'/' || b[pos + 1] == b'*') {
|
||||
println!("DIV or MUL {} {} {}", b[pos], b[pos + 1], b[pos + 2]);
|
||||
if acc == -1 {
|
||||
acc = digit(b[pos])
|
||||
}
|
||||
if b[pos + 1] == b'/' {
|
||||
acc /= digit(b[pos + 2])
|
||||
} else {
|
||||
acc *= digit(b[pos + 2])
|
||||
}
|
||||
pos += 2;
|
||||
}
|
||||
if acc == -1 {
|
||||
digit(b[pos])
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
};
|
||||
pos += 1;
|
||||
},
|
||||
x => {
|
||||
acc += {
|
||||
let mut acc = 0;
|
||||
while (pos + 1) < b.len() && (b[pos + 1] == b'/' || b[pos + 1] == b'*') {
|
||||
println!("DIV or MUL {} {} {}", b[pos], b[pos + 1], b[pos + 2]);
|
||||
if acc == 0 {
|
||||
acc = digit(b[pos])
|
||||
}
|
||||
if b[pos + 1] == b'/' {
|
||||
acc /= digit(b[pos + 2])
|
||||
} else {
|
||||
acc *= digit(b[pos + 2])
|
||||
}
|
||||
pos += 2;
|
||||
}
|
||||
if acc == 0 {
|
||||
digit(b[pos])
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
};
|
||||
pos += 1
|
||||
}
|
||||
}
|
||||
if pos >= b.len() {
|
||||
break
|
||||
}
|
||||
}
|
||||
acc
|
||||
}
|
16
code-guessing/entry-rs/src/lib.rs
Normal file
16
code-guessing/entry-rs/src/lib.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
mod entry_impl;
|
||||
use entry_impl::entry;
|
||||
|
||||
#[pyfunction]
|
||||
fn wrapped_entry(s: &str) -> PyResult<i32> {
|
||||
Ok(entry(s))
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
fn entry_rs(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(wrapped_entry, m)?)?;
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user