10
\$\begingroup\$

Given two intervals A and B, decide if A can be translated and placed into B. You can assume both given interval has some measure(or say length).

aka. Exist d, for all x in A, x+d is in B.

Both extreme points are positive integers but can be either open or close(given in input, flexible form). Moved distance needn't be integer, though.

Test cases:

[1,2], [1,3] => true
[1,2], (1,3) => true  (d can't be integer but it's fine)
[1,2], [8,9] => true
[8,9), [1,2) => true
(1,9), (1,9) => true
[1,4], [3,5] => false
(8,9], [1,2) => false (rotating is not allowed)
(7,9), [1,2] => false

Now some simple challenges onto main, CMC isn't quite active

\$\endgroup\$
5
  • 2
    \$\begingroup\$ @emanresuA Both extreme points are positive integers. to avoid float error and simple some languages \$\endgroup\$
    – l4m2
    Commented 2 days ago
  • \$\begingroup\$ In that case, can "flexible input" include e.g. taking the range(1, 4] as the pair [1.1, 4] or similar? \$\endgroup\$
    – emanresu A
    Commented 2 days ago
  • \$\begingroup\$ @emanresuA iff meta allows strong a bool there \$\endgroup\$
    – l4m2
    Commented 2 days ago
  • \$\begingroup\$ With the flexible input, could we take an additional flag indicating it's an exclusive/inclusive range? So instead of [n or n] would be [n,0] and instead of (n or n) would be [n,1] be ok (e.g. (8,9], [1,2) would be input as [[8,1],[9,0]], [[1,0],[2,1]])? \$\endgroup\$ Commented yesterday
  • \$\begingroup\$ @KevinCruijssen Expected. \$\endgroup\$
    – l4m2
    Commented yesterday

7 Answers 7

6
\$\begingroup\$

Python 3 + SymPy, 80 bytes

lambda a,b:a.measure<b.measure+a.xreplace({a.inf:b.inf,a.sup:b.sup}).issubset(b)

Try it online!

Input two sympy.set.set.Interval, output bool.


Python 3, 41 bytes

lambda s,e,l,r,S,E,L,R:1>S-s-E+e<=L-l|R-r

Try it online!

Input 8 parameters as (start_a: int, end_a: int, left_open_a: bool, right_open_a: bool, start_b: int, end_b: int, left_open_b: bool, right_open_b: bool)...

-3 bytes by Albert.Lang

\$\endgroup\$
3
  • \$\begingroup\$ 41 bytes \$\endgroup\$ Commented yesterday
  • \$\begingroup\$ Do you need the 1> in your second solution? \$\endgroup\$
    – Shaggy
    Commented yesterday
  • \$\begingroup\$ @Shaggy yes, and one also needs to fix the order of the two last differences which I got wrong (sorry tsh) Fixed (I hope) version \$\endgroup\$ Commented yesterday
2
\$\begingroup\$

Retina 0.8.2, 50 bytes

\d+
$*
(1+),\1

^(.)(1+)(.),(.1\2|(\1|\[)\2(\3|]))

Try it online! Link includes test cases. Explanation:

\d+
$*

Convert to unary.

(1+),\1
 

Get just the lengths of each range.

^(.)(1+)(.),(.1\2|(\1|\[)\2(\3|]))

Match if the second range is longer than the first, or if it's at least as closed as the first.

\$\endgroup\$
2
  • \$\begingroup\$ Your explanation, particularly "Get just the lengths of each range.", isn't entirely accurate. Because the [1,2] becomes [1,11] after converting to unary, but then [1] in your second step (even though the range [1,2] is two items instead of 1). \$\endgroup\$ Commented yesterday
  • \$\begingroup\$ @KevinCruijssen The range [1, 2] isn't two items, it's all real numbers from 1 up to and including 2. All ranges over the same interval have the same length whether they're closed or open, but obviously only closed ranges contain the zero-width end points. \$\endgroup\$
    – Neil
    Commented yesterday
2
\$\begingroup\$

05AB1E, 17 bytes

ÆD`‹sËIËI`ßs_~~*~

Two separated inputs: the first is a pair of reversed pairs of integers. And the second is a pair of pairs of bits (in either order), where 1 indicates inclusive and 0 indicates exclusive (e.g. (8,9], [1,2) would have inputs [[9,8],[2,1]] and ["01","10"]/["10","01"]).

Try it online or verify all test cases.

Inspired by @Neil's Retina answer, so make sure to upvote that answer as well!

Explanation:

In pseudo-code:

  1. Get the amount of items in both ranges if we would parse it as a regular [a,b)-range (inclusive start, exclusive end).
  2. The result is now truthy if any of these four options is truthy:
    1. The amount of items of the second range is larger than the first
    2. OR the amount of items are the same, and the inclusiveness/exclusiveness of both ranges are matching (e.g. (a,b),(c,d) / (a,b],(c,d] / [a,b),[c,d) / [a,b],[c,d])
    3. OR the amount of items are the same, and the second range is inclusive on both ends (e.g. (a,b),[c,d] / (a,b],[c,d] / [a,b),[c,d] / [a,b],[c,d])
    4. OR the amount of items are the same, and the first range is exclusive on both ends (e.g. (a,b),(c,d) / (a,b),(c,d] / (a,b),[c,d) / (a,b),[c,d])
Æ          # Reduce the inner pairs of the first (implicit) input by subtracting
           #  e.g. [[9,8],[2,1]] → [1,1]
 D`‹       # Option 1:
 D         #  Duplicate it
  `        #  Pop and push both values separated to the stack
           #   → 1,1
   ‹       #  Check if the second is larger than the first
           #   → 0 (falsey)
 s         # Swap so the duplicated pair is at the top again
  Ë        # Check if both integers in this pair are the same
           #  → 1 (truthy)
   IË      # Option 2:
   I       #  Push the second input-pair of bit-strings
    Ë      #  Check if both are the same
           #   e.g. ["01","10"] → 0 (falsey)
   I`ß     # Option 3:
   I       #  Push the second input-pair of bit-strings again
    `      #  Pop and push both separated to the stack
           #   → "01","10"
     ß     #  Pop the second one, and push its minimum
           #  (1 if it was 11; 0 if it was 00/01/10)
           #   "10" → 0 (falsey)
    s_     # Option 4:
    s      #  Swap so the first one is at the top now
     _     #  Pop and check whether it is 0
           #  (1 if it was 00; 0 if it was 01/10/11)
           #   "01" → 0 (falsey)
      ~~*~ # Or,Or,And,Or to combine all checks:
      ~~   #  Options 2, 3, OR 4
           #   0,0,0 → 0 (falsey)
        *  #  AND both items were the same
           #   1,0 → 0 (falsey)
         ~ #  Or option 1
           #   0,0 → 0 (falsey)
           # (after which the result is output implicitly)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I feel as if this gives the wrong answer for [[[8,1],[9,0]],[[1,0],[3,1]]]. \$\endgroup\$
    – Neil
    Commented yesterday
  • \$\begingroup\$ @Neil Thanks for noticing. I completely revised my answer and now that test case is correct as well. \$\endgroup\$ Commented yesterday
1
\$\begingroup\$

Charcoal, 24 bytes

≔Eη⁻ι§θκυ‹⊖⊙()№υι⁻§υ²§υ¹

Try it online! Link is to verbose version of code. Takes input as a pair of lists [char_lower, int_lower, int_upper, char_upper]. Explanation:

≔Eη⁻ι§θκυ

Pairwise subtract the two inputs. The ints just subtract normally of course, while the chars turn into empty strings if they match.

‹⊖⊙()№υι⁻§υ²§υ¹

Check that the resulting range length is non-negative unless there was an open range matched with a closed range in which case the range length needs to be positive.

50 bytes for a more parsed version:

FE²S⊞υ⟦§ι⁰↨I⪪✂ι¹±¹¦¹,±¹§ι±¹⟧≔E⊟υ⁻ι§⌈υκυ‹⊖⊙()№υι§υ¹

Try it online! Link is to verbose version of code. Takes input as two newline-separated range strings. Explanation:

FE²S⊞υ⟦§ι⁰↨I⪪✂ι¹±¹¦¹,±¹§ι±¹⟧

Input and parse the two strings, calculating the length of the range.

≔E⊟υ⁻ι§⌈υκυ

Pairwise subtract the two inputs.

‹⊖⊙()№υι§υ¹

Check that the resulting range length is non-negative unless there was an open range matched with a closed range in which case the range length needs to be positive.

\$\endgroup\$
1
\$\begingroup\$

Japt, 16 bytes

+2 bytes because I think is misinterpreted the flexibility of the input.

cVon)r- §WínX r|

Takes input as 4 arrays, with B being the first (U), A the second (V), and the 3rd (W) & 4th (X) representing whether each end of B & A, respectively, is inclusive (1) or exclusive (0).

Try it

cVon)r- §WínX r|     :Implicit input of arrays U, V, W & X
c                    :Concatenate to U
 Vo                  :  Modify last element of V
   n                 :    Negate
    )                :End concat
     r-              :Reduce by subtraction
        §            :Less than or equal to
         Wí X        :  Interleave W & X
           n         :  Reducing each pair by inverse subtraction
              r|     :  Reduce by bitwise OR
\$\endgroup\$
1
\$\begingroup\$

R, 52 51 50 bytes

-2 by @pajonk:

  • Simplified logic: (¬A)&B = ¬(A|B)

  • Renamed diff to ?

\(x,i,`?`=diff,d=?x)d[2]>d[1]|!any(d[2]-d[1],0>?i)

Attempt This Online! Input:

  • x is a 2x2 matrix with one column for each interval
  • i is a 2x2 logical matrix for whether the corresponding bound in x is closed

R's operator precedence just happens to be such that I didn't need any extra parentheses (with weird things like - is before !). Also handy is that diff(t(x))[2]-diff(t(x))[1] == diff(x)[2]-diff(x)[1] for a 2x2 matrix.

\$\endgroup\$
2
  • \$\begingroup\$ -1 byte \$\endgroup\$
    – pajonk
    Commented 19 hours ago
  • \$\begingroup\$ Another -1 by renaming diff to ? (or any unary operator actually). \$\endgroup\$
    – pajonk
    Commented 12 hours ago
0
\$\begingroup\$

Python3, 78 bytes

lambda a,A,b,B,c,C,d,D:(A==B and C==D)+(A==C or B==D)and(b-a,-A-B)<=(d-c,-C-D)

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.