Recursively print all sentences that can be formed from list of word lists
Given a list of word lists How to print all sentences possible taking one word from a list at a time via recursion?
Example:
Input: {{"you", "we"}, {"have", "are"}, {"sleep", "eat", "drink"}} Output: you have sleep you have eat you have drink you are sleep you are eat you are drink we have sleep we have eat we have drink we are sleep we are eat we are drink
We strongly recommend to minimize your browser and try this yourself first.
The idea is based on simple depth first traversal. We start from every word of first list as first word of an output sentence, then recur for the remaining lists.
Below is C++ implementation of above idea. In the below implementation, input list of list is considered as a 2D array. If we take a closer look, we can observe that the code is very close to DFS of graph.
C++
// C++ program to print all possible sentences from a list of word list #include <iostream> #include <string> #define R 3 #define C 3 using namespace std; // A recursive function to print all possible sentences that can be formed // from a list of word list void printUtil(string arr[R][C], int m, int n, string output[R]) { // Add current word to output array output[m] = arr[m][n]; // If this is last word of current output sentence, then print // the output sentence if (m==R-1) { for ( int i=0; i<R; i++) cout << output[i] << " " ; cout << endl; return ; } // Recur for next row for ( int i=0; i<C; i++) if (arr[m+1][i] != "" ) printUtil(arr, m+1, i, output); } // A wrapper over printUtil() void print(string arr[R][C]) { // Create an array to store sentence string output[R]; // Consider all words for first row as starting points and // print all sentences for ( int i=0; i<C; i++) if (arr[0][i] != "" ) printUtil(arr, 0, i, output); } // Driver program to test above functions int main() { string arr[R][C] = {{ "you" , "we" }, { "have" , "are" }, { "sleep" , "eat" , "drink" }}; print(arr); return 0; } |
chevron_right
filter_none
Python
# Python program recursively print all sentences that can be # formed from list of word lists R = 3 C = 3 # A recursive function to print all possible sentences that can # be formed from a list of word list def printUtil(arr, m, n, output): # Add current word to output array output[m] = arr[m][n] # If this is last word of current output sentence, then print # the output sentence if m = = R - 1 : for i in xrange (R): print output[i] + " " , print "\n" , return # Recur for next row for i in xrange (C): if arr[m + 1 ][i] ! = "": printUtil(arr, m + 1 , i, output) # A wrapper over printUtil def printf(arr): # Create an array to store sentence output = [""] * R # Consider all words for first row as starting # points and print all sentences for i in xrange (C): if arr[ 0 ][i] ! = "": printUtil(arr, 0 , i, output) # Driver program arr = [ [ "you" , "we" ,""], [ "have" , "are" ,""], [ "sleep" , "eat" , "drink" ]] printf(arr) # This code is contributed by Bhavya Jain |
chevron_right
filter_none
Output:
you have sleep you have eat you have drink you are sleep you are eat you are drink we have sleep we have eat we have drink we are sleep we are eat we are drink
This article is contributed by Kartik. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Print list items containing all characters of a given word
- Count of words that are present in all the given sentences
- Print the first and last character of each word in a String
- Print last character of each word in a string
- Print longest palindrome word in a sentence
- Print each word in a sentence with their corresponding average of ASCII values
- Print all n digit patterns formed by mobile Keypad
- Reverse each word in a linked list node
- Find shortest unique prefix for every word in a given list | Set 2 (Using Sorting)
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- Print anagrams together in Python using List and Dictionary
- Count occurrences of a substring recursively
- Recursively remove all adjacent duplicates
- Decode a string recursively encoded as count followed by substring