fix: 增加随机选择的函数
This commit is contained in:
parent
dc5a7e3c26
commit
c3b957318a
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -10,3 +10,4 @@ crate-type = ["cdylib"]
|
|||
|
||||
[dependencies]
|
||||
pyo3 = "0.19.0"
|
||||
rand = "0.8.5"
|
||||
|
|
|
|||
13
src/lib.rs
13
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<Vec<&PyAny>> {
|
||||
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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<u32>) -> PyResult<Vec<S
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn get_numbers_by_num(num: u32, group_type: Option<u32>) -> PyResult<Vec<String>> {
|
||||
let mut list: Vec<String> = Vec::new();
|
||||
for i in 0..1000 {
|
||||
let s = format!("{:03}", i);
|
||||
match group_type {
|
||||
Some(g) => {
|
||||
let set :HashSet<char> = 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(())
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
use rand::seq::SliceRandom;
|
||||
|
||||
pub fn rand_choice<T>(sample: Vec<T>, amount: usize) -> Vec<T> where T: Copy + Clone {
|
||||
let mut rng = &mut rand::thread_rng();
|
||||
let result = sample.choose_multiple(&mut rng, amount).cloned().collect();
|
||||
|
||||
return result;
|
||||
}
|
||||
Loading…
Reference in New Issue