Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Last active June 13, 2020 01:25
Show Gist options
  • Save ZechCodes/e4f2f0864819056c2ab3686be5b59b10 to your computer and use it in GitHub Desktop.
Save ZechCodes/e4f2f0864819056c2ab3686be5b59b10 to your computer and use it in GitHub Desktop.

Challenge # 26 - Do All Bigrams Exist?

You are given an input array of bigrams, and an list of words.

Write a function that returns True if every single bigram from this list can be found at least once in a list of words.

Examples

can_find(["at", "be", "th", "au"], ["beautiful", "the", "hat"]) ➞ True

can_find(["ay", "be", "ta", "cu"], ["maybe", "beta", "abet", "course"]) ➞ False
# "cu" does not exist in any of the words

can_find(["th", "fo", "ma", "or"], ["the", "many", "for", "forest"]) ➞ True

can_find(["oo", "mi", "ki", "la"], ["milk", "chocolate", "cooks"]) ➞ False

Notes

  • A bigram is string of two consecutive characters.
import unittest
from typing import List
def can_find(bigrams: List[str], words: List[str]) -> bool:
return False # Replace with your code!
class TestBigrams(unittest.TestCase):
def test_1(self):
self.assertEqual(can_find(["at", "be", "th", "au"], ["beautiful", "the", "hat"]), True)
def test_2(self):
self.assertEqual(can_find(["bo", "ta", "el", "st", "ca"], ["books", "table", "cap", "hostel"]), True)
def test_3(self):
self.assertEqual(can_find(["la", "te"], ["latte"]), True)
def test_4(self):
self.assertEqual(can_find(["th", "fo", "ma", "or"], ["the", "many", "for", "forest"]), True)
def test_5(self):
self.assertEqual(can_find(["ay", "be", "ta", "cu"], ["maybe", "beta", "abet", "course"]), False)
def test_6(self):
self.assertEqual(can_find(["oo", "mi", "ki", "la"], ["milk", "chocolate", "cooks"]), False)
def test_7(self):
self.assertEqual(can_find(["la"], []), False)
def test_8(self):
self.assertEqual(can_find(["la", "at", "te", "ea"], ["latte"]), False)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment