23
3
Similar figures
Two rectangles are similar if the ratios of their sides are the same.
Consider these two rectangles; a rectangle 5 lines tall and 11 chars wide:
===========
===========
===========
===========
===========
and a rectangle 10 lines tall and 22 chars wide:
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
These shapes are similar because the ratios of their sides are the same. To put it formally (with \$h\$ being the shortest side and \$w\$ being the longest side):
$$ \frac{h_1}{w_1} = \frac{h_2}{w_2} $$
You can also do:
$$ \frac{h_1}{h_2} = \frac{w_1}{w_2} $$
The challenge
Write a program or function that takes a "main" rectangle and some "other" rectangles and prints which of "others" are similar to "main".
The input
A shape and a list of shapes. Each shape consists of 2 non-zero positive integers, which denote the width and height of the rectangle. For instance, this:
(4,2), (3,9)
denotes two rectangles, a 4x2 and a 3x9. The exact format of the input may be however you desire.
The output
The indices of the "other" shapes that are similar to "main". You may choose whether the indices are 0- or 1-based, as well as the exact format and order of the output.
Sample program
In Python:
main = eval(raw_input()) # The main rectangle.
rects = eval(raw_input()) # The list of rectangles.
similar = set()
for i, rect in enumerate(rects):
if max(main)*min(rect) == min(main)*max(rect): # Cross-multiply
# They are similar.
similar.add(i)
print similar
Sample input and output
Input:
(1, 2)
[(1, 2), (2, 4)]
Output:
set([0, 1])
Input:
(1, 2)
[(1, 9), (2, 5), (16, 8)]
Output:
set([2])
Winning
This is code-golf, so the shortest submission wins.
Notes
- This should go without saying, but standard loopholes are banned.
- No builtins for locating similar figures may be used. (I don't even know if that exists, but I would't be surprised!)
Is using floating point division allowed? Would
[1.0 2.0]
be an acceptable input format? – Dennis – 2015-09-04T00:08:28.590@Dennis Provided your selected language doesn't have oddly low floating point precision and therefore the test cases fail, it should be fine. ;) – kirbyfan64sos – 2015-09-04T00:20:07.847
Instead of indices may we also output the actual similar shapes themselves? – orlp – 2015-09-04T01:36:24.077
@orlp Nope!!! :D – kirbyfan64sos – 2015-09-04T02:06:10.097
@Dennis The trouble with floating point is that you run into trouble in some cases. e.g.,
[1.0 5.0]
is not the same as[0.2 1.0]
because 0.2 is not representable in binary floating point. (That is,5 * 0.2 != 1.0
.) – Chris Jester-Young – 2015-09-04T02:21:23.670@ChrisJester-Young I know, that's why I asked.
0.2
won't be an issue here though, since the input will always be an integer (in the mathematical sense). – Dennis – 2015-09-04T02:23:28.360What is (0,0) similar to? Also your sample program fails parsing
(3+i, 4-2i)
– user253751 – 2015-09-04T16:05:34.957@immibis You only need to handle non-zero integers, not imaginary numbers. I'll edit the post. – kirbyfan64sos – 2015-09-04T17:25:32.267
So they can be negative? – user253751 – 2015-09-05T09:35:56.980
@immibis Uhh...nope. Editing... – kirbyfan64sos – 2015-09-05T15:29:56.580
3Is the output format of outputting the indices mandatory? For a test case like
[(1,2), (2,4), (1,9), (2,5), (16,8)]
, is only[0,1,4]
and[1,2,5]
allowed, or could we also output[1,1,0,0,1]
or[(1,2), (2,4), (16,8)]
? – Kevin Cruijssen – 2019-03-15T14:38:48.793