DEV Community

Simon Green
Simon Green

Posted on

The H Word

Weekly Challenge 207

Challenge, My solutions

Task 1: Keyboard Word

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 per the standard QWERTY keyboard.

My solution

This is pretty straight forward. Go through the list and find any values that match the regular expression ^(?:[qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$. In Python, I use the for if method to filter this list. In Perl, I use the grep method.

Examples

$ ./ch-1.py Hello Alaska Dad Peace
("Alaska","Dad")

$ ./ch-1.py OMG Bye
()
Enter fullscreen mode Exit fullscreen mode

Task 2: 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.

My solution

There are probably better ways of doing this, but I'm just brute forcing this. Like with many of these challenges, the sample size is so small that any improvements aren't going to provide any major gain.

Go from 1 to the length of the list (array in Perl), and check if there are at least that many items that are greater than or equal to that number.

Examples

$ ./ch-2.py 10 8 5 4 3
4

$ ./ch-2.py 25 8 5 3 3
3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)