A playground has a rectangular sandbox whose four sides are parallel to the horizontal or vertical axis.
The first line gives the coordinates x1, y1, x2, y2 of two opposite corners of the sandbox, separated by single spaces.
It is not fixed which of the two corners comes first, so x1 may be greater than x2 and y1 may be greater than y2.
The second line gives the number of balls n, and each of the next n lines gives the coordinates x, y where a ball landed, separated by one space.
For each ball print "yes" on its own line if it landed inside the sandbox or on its border, and "no" if it landed outside.
A ball is inside or on the border when its x is between x1 and x2 and its y is between y1 and y2. Here between includes both end values.
Write a function that returns true or false for whether a number v is between two numbers a and b (ends included), and use it once for the horizontal direction and once for the vertical.
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
A predicate function can return the condition itself. There is no need to spell it out as if (condition) return true; else return false;. Return type: Python bool, JS/TS boolean, Java boolean, C# bool, C++ bool, C uses bool from <stdbool.h>, and in Craykon you write the condition after return. Call the same function once for the horizontal and once for the vertical direction and join them with && (and in Python). Four traps: (1) Only Python lets you write the mathematical a ≤ v ≤ b directly (low <= v <= high). JS accepts low <= v <= high without an error but reads it as (low <= v) <= high, comparing the result of the first comparison (0 or 1) with high. So it is always true when high is 1 or more. C and C++ read it the same way: gcc compiles it as is (a warning appears only with -Wall), while recent clang rejects it with an error. Java and C# refuse to compile it because a boolean cannot be compared with a number, TS blocks it with error TS2365, and Craykon also blocks it, saying comparisons cannot be chained. In these languages split it in two: low <= v && v <= high. (2) The corners can come in a mixed-up order, so first settle the smaller and larger ends with min(a, b) and max(a, b) (Craykon Mathf.Min, Mathf.Max). In the example x1 = 8 is greater than x2 = 2. (3) The border counts as inside, so use <=, not <. (4) Both directions must match, so use &&, not ||.
Pythonmain.py · 13 lines
def between(v, a, b): """v가 a와 b 사이(양 끝 포함)에 있으면 True. a와 b는 어느 쪽이 커도 된다.""" return min(a, b) <= v <= max(a, b)x1, y1, x2, y2 = map(int, input().split())n = int(input())for _ in range(n): x, y = map(int, input().split()) if between(x, x1, x2) and between(y, y1, y2): print("yes") else: print("no")
JavaScriptmain.js · 14 lines
// v가 a와 b 사이(양 끝 포함)에 있으면 true. a와 b는 어느 쪽이 커도 된다function between(v, a, b) { return Math.min(a, b) <= v && v <= Math.max(a, b);}const lines = require("fs").readFileSync(0, "utf8").split("\n");const [x1, y1, x2, y2] = lines[0].trim().split(/\s+/).map(Number);const n = Number(lines[1]);const out = [];for (let i = 0; i < n; i++) { const [x, y] = lines[2 + i].trim().split(/\s+/).map(Number); out.push(between(x, x1, x2) && between(y, y1, y2) ? "yes" : "no");}console.log(out.join("\n"));
TypeScriptmain.ts · 16 lines
import { readFileSync } from "node:fs";// v가 a와 b 사이(양 끝 포함)에 있으면 true. a와 b는 어느 쪽이 커도 된다function between(v: number, a: number, b: number): boolean { return Math.min(a, b) <= v && v <= Math.max(a, b);}const lines: string[] = readFileSync(0, "utf8").split("\n");const [x1, y1, x2, y2]: number[] = lines[0].trim().split(/\s+/).map(Number);const n: number = Number(lines[1]);const out: string[] = [];for (let i = 0; i < n; i++) { const [x, y]: number[] = lines[2 + i].trim().split(/\s+/).map(Number); out.push(between(x, x1, x2) && between(y, y1, y2) ? "yes" : "no");}console.log(out.join("\n"));
JavaMain.java · 20 lines
import java.util.Scanner;public class Main { // v가 a와 b 사이(양 끝 포함)에 있으면 true. a와 b는 어느 쪽이 커도 된다 static boolean between(int v, int a, int b) { return Math.min(a, b) <= v && v <= Math.max(a, b); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x1 = sc.nextInt(), y1 = sc.nextInt(), x2 = sc.nextInt(), y2 = sc.nextInt(); int n = sc.nextInt(); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { int x = sc.nextInt(), y = sc.nextInt(); out.append(between(x, x1, x2) && between(y, y1, y2) ? "yes" : "no").append('\n'); } System.out.print(out); }}
C#Main.cs · 27 lines
using System;class Program{ // v가 a와 b 사이(양 끝 포함)에 있으면 true. a와 b는 어느 쪽이 커도 된다 static bool Between(int v, int a, int b) { return Math.Min(a, b) <= v && v <= Math.Max(a, b); } static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); } static void Main() { int[] r = ReadInts(); int n = ReadInts()[0]; for (int i = 0; i < n; i++) { int[] ball = ReadInts(); bool inside = Between(ball[0], r[0], r[2]) && Between(ball[1], r[1], r[3]); Console.WriteLine(inside ? "yes" : "no"); } }}
C++main.cpp · 17 lines
#include <algorithm>#include <iostream>// v가 a와 b 사이(양 끝 포함)에 있으면 true. a와 b는 어느 쪽이 커도 된다bool between(int v, int a, int b) { return std::min(a, b) <= v && v <= std::max(a, b);}int main() { int x1, y1, x2, y2, n; std::cin >> x1 >> y1 >> x2 >> y2 >> n; for (int i = 0; i < n; i++) { int x, y; std::cin >> x >> y; std::cout << (between(x, x1, x2) && between(y, y1, y2) ? "yes" : "no") << '\n'; }}
Cmain.c · 20 lines
#include <stdbool.h>#include <stdio.h>// v가 a와 b 사이(양 끝 포함)에 있으면 true. a와 b는 어느 쪽이 커도 된다bool between(int v, int a, int b) { int low = a < b ? a : b; int high = a < b ? b : a; return low <= v && v <= high;}int main(void) { int x1, y1, x2, y2, n; if (scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &n) != 5) return 0; for (int i = 0; i < n; i++) { int x, y; if (scanf("%d %d", &x, &y) != 2) return 0; puts(between(x, x1, x2) && between(y, y1, y2) ? "yes" : "no"); } return 0;}
Craykonmain.cray · 20 lines
// v가 a와 b 사이(양 끝 포함)에 있으면 참. a와 b는 어느 쪽이 커도 된다사이인가(v, a, b){ return Mathf.Min(a, b) <= v && v <= Mathf.Max(a, b);}r = Console.ReadNumbers();n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){ ball = Console.ReadNumbers(); if (사이인가(ball[0], r[0], r[2]) && 사이인가(ball[1], r[1], r[3])) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); }}