RogerBW's Blog

The Weekly Challenge 284: Lucky Relative 01 September 2024

I’ve been doing the Weekly Challenges. The latest involved a list search and an unusual sort. (Note that this ends today.)

Task 1: Lucky Integer

You are given an array of integers, @ints.

Write a script to find the lucky integer if found otherwise return -1. If there are more than one then return the largest.

A lucky integer is an integer that has a frequency in the array equal to its value.

I got lazy with this. In Perl:

sub luckyinteger($a) {

Build a freqency count.

  my %c;
  map {$c{$_}++} @{$a};

Get descending-sorted lists of keys and values.

  my @c1 = reverse sort values %c;
  my @c2 = reverse sort keys %c;

For each value starting with the largest,

  foreach my $v1 (@c1) {

For each key starting with the largest,

    foreach my $v2 (@c2) {

If the key's lookup equals the value, and the key equals the value, this is the one we want.

      if ($c{$v2} == $v1 && $v1 == $v2) {
        return $v2;
      }
    }
  }
  return -1;
}

Would have been better, in retrospect, to pull out only those values where k == v and take the largest, but I didn't think of it. Never mind. Rust's counter crate is easiest:

use counter::Counter;

fn luckyinteger(a: Vec<usize>) -> i32 {
    let c = a.into_iter().collect::<Counter<_>>();
    for pair in c.most_common_tiebreaker(|&a, &b| b.cmp(&a)) {
        if pair.0 == pair.1 {
            return pair.0 as i32;
        }
    }
    -1
}

Task 2: Relative Sort

You are given two list of integers, @list1 and @list2. The elements in the @list2 are distinct and also in the @list1.

Write a script to sort the elements in the @list1 such that the relative order of items in @list1 is same as in the @list2. Elements that is missing in @list2 should be placed at the end of @list1 in ascending order.

I used a counter for this one too. Python:

from collections import defaultdict

def relativesort(list1, list2):

Build a frequency count of list1.

  c = defaultdict(lambda: 0)
  for n in list1:
    c[n] += 1

Set up the output list.

  out = []

Push into it a number of copies of the item in list2 equal to its count in list1. Remove that item from the counter. (Which is a reason to use a defaultdict, other than simplifying the counter code; if the item doesn't appear in list1, the counter value will be zero and nothing will be added.)

  for i in list2:
    out.extend([i] * c[i])
    del c[i]

What's left is the counts of items in list1 that weren't in list2. Sort them and do the same thing.

  d = sorted(c.keys())
  for i in d:
    out.extend([i] * c[i])
  return out

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
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 2300ad 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech bayern 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 essen 2024 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