diff options
| -rwxr-xr-x | challenge-207/lubos-kolouch/bash/ch-1.sh | 20 | ||||
| -rwxr-xr-x | challenge-207/lubos-kolouch/bash/ch-2.sh | 24 | ||||
| -rw-r--r-- | challenge-207/lubos-kolouch/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-207/lubos-kolouch/perl/ch-2.pl | 69 | ||||
| -rw-r--r-- | challenge-207/lubos-kolouch/python/ch-1.py | 58 | ||||
| -rw-r--r-- | challenge-207/lubos-kolouch/python/ch-2.py | 47 |
6 files changed, 254 insertions, 0 deletions
diff --git a/challenge-207/lubos-kolouch/bash/ch-1.sh b/challenge-207/lubos-kolouch/bash/ch-1.sh new file mode 100755 index 0000000000..813495ebfe --- /dev/null +++ b/challenge-207/lubos-kolouch/bash/ch-1.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Define keyboard rows +row1="qwertyuiop" +row2="asdfghjkl" +row3="zxcvbnm" + +# Read words from input array +words=("Hello" "Alaska" "Dad" "Peace") + +# Iterate over each word +for word in "${words[@]}" +do + # Check if all letters of the word belong to one keyboard row + if grep -qi "^[${row1}]*\$\|^[${row2}]*\$\|^[${row3}]*\$" <<< "$word"; then + # Print the word if it can be typed using only one row + echo "$word" + fi +done + diff --git a/challenge-207/lubos-kolouch/bash/ch-2.sh b/challenge-207/lubos-kolouch/bash/ch-2.sh new file mode 100755 index 0000000000..b81ba15704 --- /dev/null +++ b/challenge-207/lubos-kolouch/bash/ch-2.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Read the input citations from command line arguments +citations=( "$@" ) + +# Function to compute the H-Index for a given array of citations +function compute_h_index { + # Sort the array of citations in descending order + sorted_citations=( $(echo "${citations[@]}" | tr ' ' '\n' | sort -rn) ) + # Loop through the sorted array and find the largest number h such that h articles have at least h citations each + h_index=0 + for i in "${!sorted_citations[@]}"; do + if (( sorted_citations[i] >= i+1 )); then + h_index=$(( i+1 )) + else + break + fi + done + echo "$h_index" +} + +# Compute the H-Index for the input citations +compute_h_index "${citations[@]}" + diff --git a/challenge-207/lubos-kolouch/perl/ch-1.pl b/challenge-207/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..a061a9a038 --- /dev/null +++ b/challenge-207/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +my @keyboard_rows = qw(qwertyuiop asdfghjkl zxcvbnm); +my %keyboard_map; + +# Create a hash map to store the keyboard layout +for my $i ( 0 .. 2 ) { + my $row = $keyboard_rows[$i]; + $keyboard_map{$_} = $i + 1 for split( '', $row ); +} + +# Function to check if a word can be typed using only one row of the keyboard +sub is_single_row_word { + my ($word) = @_; + my $row = $keyboard_map{ lc( substr( $word, 0, 1 ) ) }; + for my $c ( split( '', lc($word) ) ) { + return 0 if $keyboard_map{$c} != $row; + } + return 1; +} + +# Test example 1 +my @words1 = ( "Hello", "Alaska", "Dad", "Peace" ); +my @single_row_words1 = grep { is_single_row_word($_) } @words1; +is_deeply( \@single_row_words1, [ "Alaska", "Dad" ], "Example 1" ); + +# Test example 2 +my @words2 = ( "OMG", "Bye" ); +my @single_row_words2 = grep { is_single_row_word($_) } @words2; +is_deeply( \@single_row_words2, [], "Example 2" ); + +done_testing(); diff --git a/challenge-207/lubos-kolouch/perl/ch-2.pl b/challenge-207/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..6974c64a78 --- /dev/null +++ b/challenge-207/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature 'say'; +use Test::More; + +=head1 NAME + +h_index - computes the researcher’s H-Index + +=head1 SYNOPSIS + + use strict; + use warnings; + use feature 'say'; + + my @citations = (10, 8, 5, 4, 3); + my $h_index = h_index(@citations); + say "H-Index: $h_index"; + +=head1 DESCRIPTION + +The H-Index is the largest number h such that h articles have at least h citations each. +This function takes an array of integers containing citations a researcher has received +for each paper and computes the researcher’s H-Index. + +=head1 EXAMPLES + + # Example 1 + my @citations_1 = (10, 8, 5, 4, 3); + my $h_index_1 = h_index(@citations_1); + say "Example 1: expected 4, got $h_index_1"; + is($h_index_1, 4, "Test Example 1"); + + # Example 2 + my @citations_2 = (25, 8, 5, 3, 3); + my $h_index_2 = h_index(@citations_2); + say "Example 2: expected 3, got $h_index_2"; + is($h_index_2, 3, "Test Example 2"); + +=cut + +sub h_index { + my @citations = @_; + my $n = scalar @citations; + my $h = 0; + my @sorted_citations = sort { $b <=> $a } @citations; + + for ( my $i = 0 ; $i < $n ; $i++ ) { + if ( $sorted_citations[$i] >= $i + 1 ) { + $h = $i + 1; + } + else { + last; + } + } + return $h; +} + +# Run the tests +my @citations_1 = ( 10, 8, 5, 4, 3 ); +my $h_index_1 = h_index(@citations_1); +is( $h_index_1, 4, "Test Example 1" ); + +my @citations_2 = ( 25, 8, 5, 3, 3 ); +my $h_index_2 = h_index(@citations_2); +is( $h_index_2, 3, "Test Example 2" ); + +done_testing(); diff --git a/challenge-207/lubos-kolouch/python/ch-1.py b/challenge-207/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..4410928b53 --- /dev/null +++ b/challenge-207/lubos-kolouch/python/ch-1.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import unittest +from typing import List + + +def is_single_row_word(word: str) -> bool: + """Check if a word can be typed using only one row of the keyboard. + + Args: + word (str): The word to check. + + Returns: + bool: True if the word can be typed using only one row of the keyboard, + False otherwise. + + """ + keyboard_rows = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'] + keyboard_map = {} + for i in range(len(keyboard_rows)): + for c in keyboard_rows[i]: + keyboard_map[c] = i+1 + row = keyboard_map.get(word[0].lower(), 0) + for c in word: + if keyboard_map.get(c.lower(), 0) != row: + return False + return True + + +def filter_single_row_words(words: List[str]) -> List[str]: + """Filter out the words that can be typed using only one row of the keyboard. + + Args: + words (List[str]): The list of words to filter. + + Returns: + List[str]: A new list containing only the words that can be typed using + only one row of the keyboard. + + """ + return [w for w in words if is_single_row_word(w)] + +class TestSingleRowWords(unittest.TestCase): + def test_example1(self): + """Test the first example from the task description.""" + words = ["Hello", "Alaska", "Dad", "Peace"] + single_row_words = filter_single_row_words(words) + self.assertCountEqual(single_row_words, ["Alaska", "Dad"]) + + def test_example2(self): + """Test the second example from the task description.""" + words = ["OMG", "Bye"] + single_row_words = filter_single_row_words(words) + self.assertCountEqual(single_row_words, []) + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-207/lubos-kolouch/python/ch-2.py b/challenge-207/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..a9041d5e67 --- /dev/null +++ b/challenge-207/lubos-kolouch/python/ch-2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def compute_h_index(citations: List[int]) -> int: + """ + Computes the H-Index for a researcher given an array of their citations. + + The H-Index is the largest number h such that h articles have at least h citations each. + + Args: + citations: A list of integers representing the citations of a researcher's publications. + + Returns: + The H-Index of the researcher. + + Examples: + >>> compute_h_index([10, 8, 5, 4, 3]) + 4 + >>> compute_h_index([25, 8, 5, 3, 3]) + 3 + """ + # Sort the citations in descending order + citations = sorted(citations, reverse=True) + + # Find the largest h such that h articles have at least h citations each + h_index = 0 + for i, citation in enumerate(citations): + h = i + 1 + if citation >= h: + h_index = h + else: + break + + return h_index + + +# Tests +def test_compute_h_index(): + assert compute_h_index([10, 8, 5, 4, 3]) == 4 + assert compute_h_index([25, 8, 5, 3, 3]) == 3 + + +if __name__ == '__main__': + test_compute_h_index() |
