# RogerBW's Blog

 The Weekly Challenge 247: Most Frequent Santa 17 December 2023 I’ve been doing the Weekly Challenges. The latest involved gift allocation and letter pairs. (Note that this ends today.) Task 1: Secret Santa Secret Santa is a Christmas tradition in which members of a group are randomly assigned a person to whom they give a gift. You are given a list of names. Write a script that tries to team persons from different families. I suspect this is actually best approached as a flow network problem and solved with Ford-Fulkerson (or Edmonds-Karp). But I am lazy. So I take a two pass approach for each donor: (1) find a member of another family who isn't yet receiving a gift. Give it to them. Failing that: (2) find a member of your own family who isn't yourself. Give it to them. This is obviously subject to failure states depending on the input configuration. Rust: ``````fn secretsanta(name: Vec<&str>) -> bool { `````` We have full names in `name`. Build a parallel list of family names in `family`. `````` let mut family = Vec::new(); for n in &name { let surname = n.split(' ').last().unwrap(); family.push(surname); } `````` Set up a set of recipient indices. `````` let mut receivers: HashSet = HashSet::from_iter(0..name.len()); let mut gifting: Vec> = Vec::new(); `````` For each donor: `````` for giver in 0..name.len() { let mut done = false; let mut r = 0; `````` If we find a recipient who's not in the same family, use that. `````` for recipient in &receivers { if family[giver] != family[*recipient] { r = *recipient; done = true; break; } } `````` Otherwise, if we find a recipient who's not the donor, use that. `````` if !done { for recipient in &receivers { if recipient != &giver { r = *recipient; break; } } } `````` Remove that recipient and log the gift transfer. `````` receivers.remove(&r); gifting.push(vec![name[giver].to_string(), name[r].to_string()]); } for p in gifting { println!("{} -> {}", p[0], p[1]); } println!(""); true } `````` Task 2: Most Frequent Letter Pair You are given a string S of lower case letters 'a'..'z'. Write a script that finds the pair of consecutive letters in S that appears most frequently. If there is more than one such pair, chose the one that is the lexicographically first. This seems to fall very neatly into a set of operations: Generate all the letter pairs in the string Put them into a counted hash Find the highest count Filter the hash keys to just the ones with the highest count Sort those remaining hash keys and return the first. Raku: ``````sub mostfrequentletterpair(\$s) { my %f; `````` Generate the pairs and hash them: `````` for (0 .. chars(\$s) - 2) -> \$i { my \$pair = substr(\$s, \$i, 2); %f{\$pair}++; } `````` Find the highest: `````` my \$m = max(values %f); `````` Filter the hash keys and sort them. `````` my @l = %f.keys.grep({%f{\$_} == \$m}).sort({\$^a cmp \$^b}); `````` Return the first. `````` return @l[0]; } `````` Full code on github. Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one. Search 10 per page 20 per page 50 per page 100 per page Archive Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult Special All book reviews, All film reviews
Produced by aikakirja v0.1