Last active
August 1, 2019 14:51
-
-
Save vitasiku/e797ccb877aa04f96c775a94159b47aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_cyclic(input_list): | |
""" | |
The intuition is very simple and can be thought of as traversing a double-linked list or tree-traversals. | |
- For the given_list to be cyclic, the first and last chars in words that form the list should match. | |
- Which means, these chars should form even pairs. | |
Thus, this function, | |
1. Creates a new list consisting of only the first and last character of every word in the list. | |
2. Convert the new list into a string. | |
3. Counts the number of occurences of every character in the new string in step 2. | |
4. Checks, if all the character occurences are even. If true, then the list is cyclic else it will not be cyclic. | |
""" | |
from collections import Counter | |
return all(v%2 == 0 for v in Counter("".join([f"{c[0]}{c[-1]}" for c in input_list])).values()) | |
given_list = ['chair', 'height', 'racket', 'touch', 'tunic'] | |
is_cyclic(given_list) # True or False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment