# RogerBW's Blog

The Weekly Challenge 233: Similar Frequency 10 September 2023

I’ve been doing the Weekly Challenges. The latest involved reducing lists in various ways. (Note that this ends today. And 232 was cancelled as the administrator was unwell)

You are given an array of words made up of alphabets only.

Write a script to find the number of pairs of similar words. Two words are similar if they consist of the same characters.

It turns out from the examples that "they consist of the same characters" means "the set of characters in the words is the same, ignoring count and order" - so "abc" and "cabcb" are similar by this definition.

Which means I will solve this with a custom hasher.

``````sub similarwords(\$a) {
my %ct;
my \$ac = ord('a');
``````

For each word:

``````  foreach my \$w (@{\$a}) {
``````

Make a set out of the letters (i.e. one key per letter, ignore the count).

``````    my %hs = map {\$_ => 1} split '', \$w;
``````

``````    my \$mask = 0;
``````

and encode based on ASCII value offset from `a`. So `a` produces 1, `b` 2, `c` 4, etc.

``````    foreach my \$c (keys %hs) {
\$mask |= 1 << (ord(\$c) - \$ac);
}
``````

Increment the counter for that mask.

``````    \$ct{\$mask}++;
}
``````

``````  my \$pairs = 0;
foreach my \$cv (values %ct) {
if (\$cv > 1) {
``````

No need to count the actual pairs; if there are 3 values, you can make 3 pairs out of them. 4 values, 6 pairs. 5 values, 10 pairs. Etc.

``````      \$pairs += \$cv * (\$cv - 1) / 2;
}
}
return \$pairs;
}
``````

Other languages are basically similar.

You are given an array of integers.

Write a script to sort the given array in increasing order based on the frequency of the values. If multiple values have the same frequency then sort them in decreasing order.

I've generally been solving these in Rust first, then porting to other languages. But Rust's `Counter` crate has a handy feature which I didn't immediately find in the other languages…

``````fn frequencysort(a: Vec<i32>) -> Vec<i32> {
let mut ct = a.into_iter().collect::<Counter<i32>>().most_common_ordered();
``````

Well, there's half my work done, thanks very much! (Given how rich Raku's collection types are, I was surprised not to find anything like this in its `Bag` functions, but apparently not…)

``````    ct.reverse();
let mut out = Vec::new();
for (k, v) in ct.iter() {
out.append(&mut vec![*k; *v]);
}
out
}
``````

And there's the other half. But let's also look at the code for other languages, specifically Python.

``````def frequencysort(a):
``````

Put the number counts into `ct`.

``````  ct = defaultdict(lambda: 0)
for x in a:
ct[x] += 1
``````

Build a "reversed `ct`", where the keys are the counts and the values are lists of the mumbers with that count.

``````  rct = defaultdict(lambda: [])
for k, v in ct.items():
rct[v].append(k)
``````

Then to build the output, and this is more or less as in the Rust code:

``````  out = []
``````

Iterate low to high counts:

``````  for k in sorted(rct):
``````

Then iterate high to low numbers with that count:

``````    for v in reversed(sorted(rct[k])):
``````

Then push on the correct count of that number:

``````      for i in range(k):
out.append(v)
return out
``````

Full code on github.