
- 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 all tasks can be executed using given server cores or not in Python
Suppose we have two lists, they are cores and tasks. The cores[i] indicates number of cores available in the ith server. And tasks[i] indicates the number of cores needed to execute that task. Each task must be run in only one server. And a server may have multiple tasks to run. We have to check whether it's possible to run all the tasks with the given cores or not.
So, if the input is like cores = [10, 7] tasks = [7, 3, 2, 2, 1], then the output will be True, because we can put tasks[0] and tasks[1] into first server with core 10, and remaining tasks on second server with cores 7.
To solve this, we will follow these steps −
- Define a function solve() . This will take cores, tasks
- if tasks set is empty, then
- return True
- for i in range 0 to size of cores - 1, do
- if cores[i] >= tasks[0], then
- cores[i] := cores[i] - tasks[0]
- if solve(cores, tasks list except the first task) is true, then
- return True
- cores[i] := cores[i] + tasks[0]
- if cores[i] >= tasks[0], then
- return False
Example
Let us see the following implementation to get better understanding −
def solve(cores, tasks): if not tasks: return True for i in range(len(cores)): if cores[i] >= tasks[0]: cores[i] -= tasks[0] if solve(cores, tasks[1:]): return True cores[i] += tasks[0] return False cores = [10, 7] tasks = [7, 3, 2, 2, 1] print(solve(cores, tasks))
Input
[10, 7], [7, 3, 2, 2, 1]
Output
True
- Related Articles
- C++ program to check whether it is possible to finish all the tasks or not from given dependencies
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether all can get a seat or not in Python
- Program to check a string can be broken into given list of words or not in python
- Program to find number of tasks can be finished with given conditions in Python
- Program to check whether final string can be formed using other two strings or not in Python
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- C++ Program to check given candies can be split with equal weights or not
- Program to check whether one point can be converted to another or not in Python
- Program to check words can be found in matrix character board or not in Python
- Program to check n can be shown as sum of k or not in Python
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check a string can be split into three palindromes or not in Python
- Program to check given string is pangram or not in Python

Advertisements