
- 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 check we can form array from pieces or not in Python
Suppose we have an array nums where all elements are unique and have another array with different smaller arrays called pieces. We have to check whether we can get the main array nums by concatenating the arrays in pieces in any order or not. But we are not allowed to reorder the elements present in each array pieces[i].
So, if the input is like nums = [5,1,12,36,2,47,6] pieces = [[2,47,6],[12,36],[1],[5]], then the output will be True because we can concatenate them in this order [[5], [1], [12,36], [2,47,6]] to get the main array.
To solve this, we will follow these steps −
temp := a new list
for each p in pieces, do
if p[0] not present in nums, then
return False
l := size of p
indx := index(p[0]) in nums
if subarray of nums from index indx to indx+l-1 is not same as p, then
return False
otherwise,
append p after temp
if size of nums is same as size of temp, then
return True
otherwise,
return False
Example (Python)
Let us see the following implementation to get better understanding −
def solve(nums, pieces): temp = [] for p in pieces: if p[0] not in nums: return False l = len(p) indx = nums.index(p[0]) if nums[indx:indx+l] != p: return False else: temp.extend(p) if len(nums) == len(temp): return True else: return False nums = [5,1,12,36,2,47,6] pieces = [[2,47,6],[12,36],[1],[5]] print(solve(nums, pieces))
Input
[5,1,12,36,2,47,6], [[2,47,6],[12,36],[1],[5]]
Output
True
- Related Articles
- Program to check we can visit any city from any city or not in Python
- Program to check we can reach leftmost or rightmost position or not in Python
- Program to check whether we can take all courses or not in Python
- Program to check we can cross river by stones or not in Python
- Program to check whether we can unlock all rooms or not in python
- Python program to check whether we can pile up cubes or not
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to check whether we can get N queens solution or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check we can reach at position n by jumping or not in Python
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- Program to check if we reverse sublist of one list to form second list or not in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check whether we can form 24 by placing operators in python
- Program to check robot can reach target position or not in Python
