選挙投票のカウントを想定したもの
import typing as T
import random
Summary = T.Mapping[int, int]
def process_votes(pile: T.List[int]) -> Summary:
summary = {}
for vote in pile:
if vote in summary:
summary[vote] += 1
else:
summary[vote] = 1
return summary
if __name__ == "__main__":
num_candidates = 3
num_voters = 100
pile = [random.randint(1, num_candidates) for _ in range(num_voters)]
print(pile)
counts = process_votes(pile)
print(f"Total number of votes: {counts}")
$ python3 count_votes_sequential.py
[1, 1, 2, 1, 2, 3, 2, 2, 1, 2, 2, 3, 3, 2, 3, 1, 2, 2, 2, 3, 1, 1, 2, 3, 1, 2, 3, 3, 2, 2, 3, 2, 1, 3, 2, 2, 3, 3, 2, 3, 3, 3, 2, 3, 2, 1, 1, 3, 2, 3, 3, 2, 2, 1, 1, 1, 1, 2, 3, 3, 1, 2, 2, 3, 2, 1, 2, 2, 1, 3, 1, 1, 2, 1, 1, 2, 3, 3, 1, 2, 1, 2, 3, 3, 3, 1, 2, 2, 2, 1, 1, 3, 2, 1, 2, 2, 3, 2, 3, 1]
Total number of votes: {1: 29, 2: 40, 3: 31}
Rustで書き直す
範囲指定のランダムは rnd.gen_range となる。
use std::time::Instant;
use threadpool::ThreadPool;
use rand::Rng;
use std::collections::HashMap;
fn process_votes(pile: Vec<u32>) -> HashMap<u32, u32> {
let mut summary = HashMap::new();
for vote in pile {
if summary.get(&vote) != None {
let count = summary.get(&vote).unwrap();
summary.insert(vote, count + 1);
} else {
summary.insert(vote, 1);
}
}
summary
}
fn main(){
let num_voters = 100;
let num_candidate = 3;
let mut pile: Vec<u32> = Vec::new();
let mut rnd = rand::thread_rng();
for _ in 0..num_voters {
pile.push(rnd.gen_range(1..(num_candidate + 1)))
}
let summary = process_votes(pile);
println!("{:?}", summary);
}
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.29s
Running `target/debug/parallel`
{3: 42, 2: 33, 1: 25}
ここまではOK