26 July 2024 | Challenge 279 |

# Neither Sort Nor Split

## Task 1: Sort Letters

**Submitted by: Mohammad Sajid Anwar**

You are given two arrays, `@letters`

and `@weights`

.

Write a script to sort the given array `@letters`

based on the `@weights`

.

### Example 1

```
Input: @letters = ('R', 'E', 'P', 'L')
@weights = (3, 2, 1, 4)
Output: PERL
```

### Example 2

```
Input: @letters = ('A', 'U', 'R', 'K')
@weights = (2, 4, 1, 3)
Output: RAKU
```

### Example 3

```
Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T')
@weights = (5, 4, 2, 6, 1, 3)
Output: PYTHON
```

### Solution

This task is very similar to task #1 from challenge 278 with one significant difference: Now I’ve seen solutions to last week’s challenge that are much better than mine.

I’ll assume the weights to be in the range \(1 \ldots n\) without gaps or repetitions. Then the weights can be used as (one-based) indices in a target array where the source characters are to be placed.

```
use v5.24;
use warnings;
use experimental 'signatures';
sub sort_letters ($letters, $weights) {
(\my @word)->@[map $_ - 1, @$weights] = @$letters;
join '', @word;
}
```

See the full solution to task 1.

## Task 2: Split String

**Submitted by: Mohammad Sajid Anwar**

You are given a string, `$str`

.

Write a script to split the given string into two containing exactly same number of vowels and return true if you can otherwise false.

### Example 1

```
Input: $str = "perl"
Ouput: false
```

### Example 2

```
Input: $str = "book"
Ouput: true
Two possible strings "bo" and "ok" containing exactly one vowel each.
```

### Example 3

```
Input: $str = "good morning"
Ouput: true
Two possible strings "good " and "morning" containing two vowels each or "good m" and "orning" containing two vowels each.
```

### Solution

Here we need to check if the given string contains an even number of vowels.
Using the `tr///`

operator to count them.

```
use strict;
use warnings;
sub split_string {
local $_ = shift;
!(tr/aeiouAEIOU// % 2);
}
```

See the full solution to task 2.

If you have a question about this post or if you like to comment on it, feel free to open an issue in my github repository.