aboutsummaryrefslogtreecommitdiff
path: root/challenge-207/lubos-kolouch/python
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-07 18:05:14 +0100
committerLubos Kolouch <lubos@kolouch.net>2023-03-07 18:05:14 +0100
commit3d41b13eee3184f3cd62707e1f662ef91f3a4c4f (patch)
tree4573b0c512674e251fa19536f52179189947c0d2 /challenge-207/lubos-kolouch/python
parentd75c83429332efc88f29eee14f988b199f2fa10c (diff)
downloadperlweeklychallenge-club-3d41b13eee3184f3cd62707e1f662ef91f3a4c4f.tar.gz
perlweeklychallenge-club-3d41b13eee3184f3cd62707e1f662ef91f3a4c4f.tar.bz2
perlweeklychallenge-club-3d41b13eee3184f3cd62707e1f662ef91f3a4c4f.zip
Challenge 207 LK Perl Python Bash
Diffstat (limited to 'challenge-207/lubos-kolouch/python')
-rw-r--r--challenge-207/lubos-kolouch/python/ch-1.py58
-rw-r--r--challenge-207/lubos-kolouch/python/ch-2.py47
2 files changed, 105 insertions, 0 deletions
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()