Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Add pararhyme #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/rhymes_with/rhymes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
def does_rhyme(word1: str, word2: str, length: int = 3) -> bool:
"""
:returns: True if word1 and word2 rhymes.
"""
return does_perfectly_rhyme(word1, word2) or does_pararhyme(word1, word2)


def does_perfectly_rhyme(word1: str, word2: str, length: int = 3) -> bool:
"""
:returns: True iff word1 and word2 has the
same ending up to length.
Expand All @@ -7,6 +14,26 @@ def does_rhyme(word1: str, word2: str, length: int = 3) -> bool:
return word1[-length:] == word2[-length:]


def does_pararhyme(word1: str, word2: str) -> bool:
"""
:returns: True if word1 and word2 has mathcing
consonants.
"""

if not (len(word1) == len(word2)):
return False

for letter1, letter2 in zip(word1, word2):
Copy link
Owner

@eivindjahren eivindjahren Nov 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all(letter1 == letter2 or is_vowel(letter1, letter2) for letter1, letter2 in zip(word1,word2))

if not letter1 == letter2 and not is_vowels(letter1, letter2):
return False
return True


def is_vowels(letter1, letter2):
vovels = ["a", "e", "i", "o", "u"]
return letter1 in vovels and letter2 in vovels


def filter_rhymes(word: str, dictionary: [str]) -> [str]:
"""
:returns: the strings in dictionary that does_rhyme
Expand Down
8 changes: 8 additions & 0 deletions tests/test_rhymes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@

def test_cook_rhymes_with_book():
assert does_rhyme("book", "cook")


def test_tick_pararhymes_with_tock():
assert does_rhyme("tick", "tock")


def test_ticks_does_not_pararhymes_with_tock():
assert not does_rhyme("ticks", "tock")