
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to Find Out if There is a Short Circuit in Input Words in Python
Suppose we have a list of words. We have to check the given words can be chained to form a circle. A word A can be placed in front of another word B in a chained circle if only the last character of A is identical to the first character of B. Every word has to be used and can be used only once (the first/last word will not be considered).
So, if the input is like words = ["ant","dog","tamarind","nausea","gun"], then the output will be True.
To solve this, we will follow these steps −
graph := a new key-value pair list
seen := a new set
inDegree := a new key-value pair list
outDegree := a new key-value pair list
for each word in words, do
start := word[0]
end := word[-1]
insert end at the end of graph[start]
outDegree[start] := outDegree[start] + 1
inDegree[end] := inDegree[end] + 1
for each node in outDegree, do
if outDegree[node] is not same as inDegree[node], then
return False
dfs(words[0,0])
return size of seen if it is same as size of graph
Define a function dfs() . This will take node.
add(node) in seen
for each child in graph[node], do
if child is not present in seen, then
dfs(child)
Example
Let us see the following implementation to get better understanding −
import collections class Solution: def solve(self, words): self.graph = collections.defaultdict(list) self.seen = set() inDegree = collections.Counter() outDegree = collections.Counter() for word in words: start = word[0] end = word[-1] self.graph[start].append(end) outDegree[start] += 1 inDegree[end] += 1 for node in outDegree: if outDegree[node] != inDegree[node]: return False self.dfs(words[0][0]) return len(self.seen) == len(self.graph) def dfs(self, node): self.seen.add(node) for child in self.graph[node]: if child not in self.seen: self.dfs(child) ob = Solution() print(ob.solve(["ant","dog","tamarind","nausea","gun"]))
Input
["ant","dog","tamarind","nausea","gun"]
Output
True
- Related Articles
- C++ Program to find out if there is a pattern in a grid
- Program to find out if we win in a game in Python
- Program to find out if a BST is present in a given binary tree in Python
- Program to find how many distinct rotation groups are there for a list of words in Python
- Program to find out if the graph is traversable by everybody in Python
- Program to find out if a linked list is present in a given binary tree in Python
- Short-circuit evaluation in JavaScript
- Short Circuit Assignment in JavaScript
- Program to Find Out if an Edge is a Part of a Minimum Spanning Tree in Python
- Python program to sort out words of the sentence in ascending order
- Program to check whether there is any pair of words which are almost same in Python
- Short Circuit Transient in Synchronous Machine
- Program to find out the data type of user input in C++
- How to find out if a Python object is a string?
- Program to Find Out Currency Arbitrage in Python
