
- 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 whether one point can be converted to another or not in Python
Suppose we have a starting points (sx, sy), and target point (tx, ty), we have to check whether a sequence of moves exists from the start point to the end point. Here move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).
So, if the input is like (sx, sy) = (1,1) (tx, ty) = (4,5), then the output will be True, this is because move (1,1) to (2,1), then (3,1), then (4,1), then (4,5).
To solve this, we will follow these steps −
Define a function solve() . This will take sx, sy, tx, ty
if sx > tx or sy > ty, then
return False
if sx is same as tx, then
return(ty-sy) mod sx is same as 0
if sy is same as ty, then
return(tx - sx) mod sy is same as 0
return solve(sx, sy, tx-ty, ty) or solve(sx, sy, tx, ty-tx)
Example
Let us see the following implementation to get better understanding
def solve(sx, sy, tx, ty): if sx > tx or sy > ty: return False if sx == tx: return (ty-sy)%sx == 0 if sy == ty: return (tx - sx)%sy == 0 return solve(sx, sy, tx-ty, ty) or solve(sx, sy, tx, ty-tx) (sx, sy) = (1,1) (tx, ty) = (4,5) print(solve(sx, sy, tx, ty))
Input
(1,1), (4,5)
Output
True
- Related Articles
- Program to check one string can be converted to another by removing one element in Python
- Program to check whether one string can be 1-to-1 mapped into another string in Python
- Program to check whether one string swap can make strings equal or not using Python
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Program to check one string can be converted to other by shifting characters clockwise in Python
- Python program to check whether we can pile up cubes or not
- 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 whether Amal can win stone game or not in Python
- Program to check whether one value is present in BST or not in Python
- Program to check whether final string can be formed using other two strings or not in Python
- Program to check all 1s are present one after another or not in Python
- Program to check whether one tree is subtree of other or not in Python
- Program to check whether we can get N queens solution or not in Python
