Keyboard Words
Task
You are given an array of words.
Write a script to print all the words in the given array that can be types using alphabet on only one row of the keyboard.
Let us assume the keys are arranged as below:
- Row 1:
qwertyuiop
- Row 2:
asdfghjkl
- Row 3:
zxcvbnm
Solution
|
|
Discussion
Matching strings against regular expressions is naturally a strong point in Raku.
Creating a named regex for each row and comparing them using alternation is done in keyboard-word
.
The ternary operator then returns whether a match has been found.
This function is then used to filter the word list.
H-Index
Task
You are given an array of integers containing citations a researcher has received for each paper.
Write a script to compute the researcher’s H-Index. For more information please checkout the Wikipedia page.
The H-Index is the largest number h such that h articles have at least h citations each. For example, if an author has five publications, with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), then the author’s h-index is 3, because the author has three publications with 3 or more citations. However, the author does not have four publications with 4 or more citations.
Solution
|
|
Discussion
After sorting the list just iterate the list from the back.
The first value which is smaller than it’s inverted index aborts the while
loop.
As we went one iteration too far, subtract one from the index and return it.