From c3b957318aa6332e441d07dd526c1839b2718ddd Mon Sep 17 00:00:00 2001 From: chenwj113 Date: Mon, 29 Jan 2024 15:07:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 13 +++++++++++- src/pls/mod.rs | 36 ++++++++++++++++++++++++++++++++- src/rand_mod.rs | 8 ++++++++ 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/rand_mod.rs diff --git a/Cargo.lock b/Cargo.lock index 170c546..2b041cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "indoc" version = "1.0.9" @@ -47,6 +58,7 @@ name = "lottery" version = "1.0.0" dependencies = [ "pyo3", + "rand", ] [[package]] @@ -87,6 +99,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.78" @@ -165,6 +183,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -215,6 +263,12 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "windows-targets" version = "0.48.5" diff --git a/Cargo.toml b/Cargo.toml index 362d674..e33a2e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ crate-type = ["cdylib"] [dependencies] pyo3 = "0.19.0" +rand = "0.8.5" diff --git a/src/lib.rs b/src/lib.rs index 6649d1a..157d560 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,21 @@ mod pls; +mod rand_mod; -use pyo3::prelude::*; +use pyo3::{prelude::*}; +use pyo3::types::PyList; + + +#[pyfunction] +fn rand_choice(obj: &PyList, amount: usize) -> PyResult> { + let sample = Vec::from_iter(obj.iter()); + let result = rand_mod::rand_choice(sample, amount); + Ok(result) +} /// A Python module implemented in Rust. #[pymodule] fn lottery(py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(rand_choice, m)?)?; pls::register_pls_mod(py, m)?; Ok(()) } diff --git a/src/pls/mod.rs b/src/pls/mod.rs index f2d6a65..df05243 100644 --- a/src/pls/mod.rs +++ b/src/pls/mod.rs @@ -1,6 +1,6 @@ use std::collections::{HashMap, HashSet}; -use pyo3::prelude::*; +use pyo3::{prelude::*}; /// Formats the sum of two numbers as string. #[pyfunction] @@ -130,10 +130,44 @@ pub fn get_numbers_by_span(span: u32, group_type: Option) -> PyResult) -> PyResult> { + let mut list: Vec = Vec::new(); + for i in 0..1000 { + let s = format!("{:03}", i); + match group_type { + Some(g) => { + let set :HashSet = HashSet::from_iter(s.chars().into_iter()); + if g == 1 && set.len() == 1 { + list.push(s); + }else if g == 3 && set.len() == 2 { + list.push(s); + } else if g == 6 && set.len() == 3 { + list.push(s); + } + }, + None => { + list.push(s); + } + } + + } + let mut result = Vec::new(); + for item in &list { + if item.contains(num.to_string().as_str()) { + result.push(item.clone()); + } + } + result.sort_by(|x, y| x.cmp(y)); + Ok(result) + +} + pub fn register_pls_mod(py: Python <'_>, parent_mod: &PyModule) -> PyResult<()> { let child_mod = PyModule::new(py, "pls")?; child_mod.add_function(wrap_pyfunction!(get_numbers_by_span, child_mod)?)?; child_mod.add_function(wrap_pyfunction!(get_numbers_by_sum, child_mod)?)?; + child_mod.add_function(wrap_pyfunction!(get_numbers_by_num, child_mod)?)?; parent_mod.add_submodule(child_mod)?; Ok(()) diff --git a/src/rand_mod.rs b/src/rand_mod.rs new file mode 100644 index 0000000..5a87335 --- /dev/null +++ b/src/rand_mod.rs @@ -0,0 +1,8 @@ +use rand::seq::SliceRandom; + +pub fn rand_choice(sample: Vec, amount: usize) -> Vec where T: Copy + Clone { + let mut rng = &mut rand::thread_rng(); + let result = sample.choose_multiple(&mut rng, amount).cloned().collect(); + + return result; +} \ No newline at end of file