1 个不稳定版本
使用旧 Rust 2015
0.1.0 | 2017年10月22日 |
---|
#65 在 #cli-command
18KB
336 行
Rusty Santa
这是一个用于解决带有额外约束的 圣诞老人礼物分配 的小型 Rust 库(和命令行工具)。
可以将以下约束添加到随机分配中
- A 和 B 两人不应抽到对方(例如,对于夫妇而言)
- A 不应抽到 B(例如,如果 B 上一年已经从 A 那里收到礼物,或者 A 不喜欢 B)
虽然这是一个有趣的数学问题,可以用二分图和匈牙利算法解决,但这个库坚持更简单的方法,并试图模拟从篮子里抽取真实姓名的过程。算法会尝试最多1000次解决名字分配,直到失败。
库
用法
let mut group = Group::new();
group.add("Sheldon".into());
group.add("Amy".into());
group.add("Leonard".into());
group.add("Penny".into());
group.add("Rajesh".into());
group.add("Howard".into());
group.add("Bernadette".into());
// Exclude couples
group.exclude_pair("Sheldon".into(), "Amy".into());
group.exclude_pair("Leonard".into(), "Penny".into());
group.exclude_pair("Howard".into(), "Bernadette".into());
// Sheldon can't keep secrets from his roommates
group.exclude("Sheldon".into(), "Leonard".into());
match group.assign() {
Ok(assignments) => {
for (from, to) in assignments {
println!("{} => {}", from, to);
}
},
Err(e) => println!("Error: {:?}", e),
}
日志记录
Rusty Santa 在 TRACE 级别记录算法步骤
$ RUST_LOG=rusty_santa=trace cargo run -p rusty-santa --example cli
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target/debug/examples/cli`
TRACE:rusty_santa: Drawing recipient for Howard
TRACE:rusty_santa: Options: ["Sheldon", "Penny", "Amy", "Rajesh", "Leonard"]
TRACE:rusty_santa: Picked Sheldon!
TRACE:rusty_santa: Drawing recipient for Sheldon
TRACE:rusty_santa: Options: ["Howard", "Bernadette", "Penny", "Rajesh"]
TRACE:rusty_santa: Picked Howard!
TRACE:rusty_santa: Drawing recipient for Bernadette
TRACE:rusty_santa: Options: ["Penny", "Amy", "Rajesh", "Leonard"]
TRACE:rusty_santa: Picked Penny!
TRACE:rusty_santa: Drawing recipient for Penny
TRACE:rusty_santa: Options: ["Bernadette", "Amy", "Rajesh"]
TRACE:rusty_santa: Picked Amy!
TRACE:rusty_santa: Drawing recipient for Amy
TRACE:rusty_santa: Options: ["Bernadette", "Rajesh", "Leonard"]
TRACE:rusty_santa: Picked Rajesh!
TRACE:rusty_santa: Drawing recipient for Rajesh
TRACE:rusty_santa: Options: ["Bernadette", "Leonard"]
TRACE:rusty_santa: Picked Leonard!
TRACE:rusty_santa: Drawing recipient for Leonard
TRACE:rusty_santa: Options: ["Bernadette"]
TRACE:rusty_santa: Picked Bernadette!
Howard => Sheldon
Sheldon => Howard
Bernadette => Penny
Penny => Amy
Amy => Rajesh
Rajesh => Leonard
Leonard => Bernadette
命令行工具
存在一个概念验证命令行界面。您可以使用 cargo install rusty-santa-cli
安装它。
$ rusty-santa-cli
Rusty Santa v0.1.0
Who's in?
(List one name per line and press enter, end the list with an empty line.)
Name: A
Name: B
Name: C
Name: D
Name:
Alright. Are there any pairs that should not give each other gifts?
If you're done, just press enter.
Name 1: A
Name 2: B
OK, excluding the pair A <-> B
Someone else?
Name 1:
And now, are there any pairs where person 1 should not give person 2 a gift?
If you're done, just press enter.
Name 1: A
Name 2: C
OK, excluding the pair A -> C
Someone else?
Name 1:
Great! Now we'll draw the names.
I'll show a name, first. That person should come to the computer,
without other people seeing the screen.
Press enter to reveal the name, press enter again to hide it.
B, are you ready? Press enter to see the name.
******
A, are you ready? Press enter to see the name.
You'll give a gift to D! (Press enter to hide the name)
...等等。
许可协议
在以下任一协议下授权
- Apache License 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT),任选其一。
贡献
除非您明确说明,否则您提交的任何贡献,按照 Apache-2.0 许可证的定义,应按上述方式双许可,不得附加任何额外条款或条件。
依赖项
~4–11MB
~123K SLoC