Given dimensions of a 2D array, except 1xn, that results in an odd number of elements, find the midpoint index.
Example: Input is 3x5, representing the 2D array
[0,0] [0,1] [0,2] [0,3] [0,4]
[1,0] [1,1] [1,2] [1,3] [1,4]
[2,0] [2,1] [2,2] [2,3] [2,4]
The midpoint index is [1,2]
Input
3
5
Output
1 , 2
Rules
- Standard code-golf rules, fewest bytes wins
- Either full program or function is permitted
- When input is either 1xn or the corresponding array would have an even number of elements, output must be [0,0]
- nx1 should be treated like any other array
16 Answers 16
APL (Dyalog Classic), (削除) 20 (削除ここまで) (削除) 19 (削除ここまで) (削除) 15 (削除ここまで) 13 bytes
×ばつ1≠⊃
Japt, 11 bytes
My first (somewhat) serious Japt submission! Any help is appreciated, I think ~10 bytes or even less may be possible. Saved 4 bytes thanks to Shaggy!
£ÎÉ©Ueu)*Xz
How it works
Slightly outdated.
£eu *UÎ>1 *Xz – Full program.
£ – For each X of the input array.
*Xz – Multiply floor(X/2) by the truth value of:
eu – Are both numbers odd? And...
*UÎ>1 – Is the initial element greater than 1?
– Implicit output to STDOUT.
APL (Dyalog Unicode), 23 bytes SBCS
Anonymous prefix function. Takes rows,columns as right argument.
{0::0 0⋄÷∘÷@0⌽ ×ばつ⍵-1}
{
...}
"dfn"; ⍵
is right argument (rightmost letter of Greek alphabet).
0::
if any error happens:
0 0
return [0,0]
⋄
now try:
⍵-1
right argument minus 1
×ばつ
multiply by a half
⌽ ̈⍨
rotate each by itself (this errors on non-integers)
÷∘÷@0
reciprocal of reciprocal at position 0 (this errors when the first coordinate is 0)
05AB1E, (削除) 9 (削除ここまで) 8 bytes
Saved 1 byte thanks to Mr. Xcoder (remove D
)
¬≠*ÉP*;ï
Try it online! or as a Test Suite
Explanation
­* # multiply the input by its first value falsified
ÉP* # multiply the input by the product of the results values' oddness
; # divide by 2
ï # convert to int
-
\$\begingroup\$ Nice! I had
¬≠sPÉ*s2÷*
for 10 bytes. Taking some inspiration from you though, would¬≠*PÉ*2÷
work for 8 bytes? Alternatively,¬≠*PÉ*;ï
, of course. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2018年07月04日 11:34:42 +00:00Commented Jul 4, 2018 at 11:34 -
\$\begingroup\$ @Mr.Xcoder: Of course. I don't need the
D
here :/ Thanks! \$\endgroup\$Emigna– Emigna2018年07月04日 11:45:47 +00:00Commented Jul 4, 2018 at 11:45
-
1
-
Perl 6, (削除) 42 (削除ここまで) 39 bytes
{(^$^a X ^$^b)[$b-1&&$a*$b%2&&$a*$b/2]}
Anonymous code block that returns a list of two integers.
Explanation:
{ } # Anonymous code block
(^$^a X ^$^b) # Cross product of the two ranges, basically the flattened 2D array
[ ] # Get the element at
&&$a*$b/2 # Midpoint of the array
$a*$b%2 # If the numbers are odd
$b-1&& # And the second element is not 1
# Else the 0th element, which is 0,0
-
\$\begingroup\$ Seems to fail on 7×1: Try it online! \$\endgroup\$Adám– Adám2018年07月04日 09:51:24 +00:00Commented Jul 4, 2018 at 9:51
-
\$\begingroup\$ I'm confused. Why is 1xn expected to fail, but nx1 is fine? \$\endgroup\$Jo King– Jo King2018年07月04日 10:13:01 +00:00Commented Jul 4, 2018 at 10:13
-
2\$\begingroup\$ Because OP says so. \$\endgroup\$Adám– Adám2018年07月04日 10:19:26 +00:00Commented Jul 4, 2018 at 10:19
JavaScript (ES6), 31 bytes
Saved 1 byte thanks to @Neil
Takes the dimensions in currying syntax (w)(h)
.
w=>h=>w&h&w>1?[h>>1,w>>1]:[0,0]
Original version, 32 bytes
Takes the dimensions in currying syntax (w)(h)
.
w=>h=>w*h&!!--w?[h>>1,w/2]:[0,0]
How?
We want to test whether both \$w\$ and \$h\$ are odd, which is true if and only if \$w\times h\$ is odd. We also want to make sure that \$w\$ is not equal to \1ドル\,ドル which means that \$w-1\$ is not equal to \0ドル\$. We can merge both tests into:
w * h & !!--w
If this test passes, we know that \$w\$ was odd and is now even (since it was decremented), so we can divide it by \2ドル\$ with a standard division. For \$h\,ドル we use a bitwise shift instead because it's still odd.
Jelly, 10 bytes
This confused me :p
×ばつḂ_ỊḢ$PƲ
A monadic link accepting a list of the dimensions returning a list of the middle-indices or [0,0]
in the special-cases.
How?
×ばつḂ_ỊḢ$PƲ - Link: list of dimension lengths e.g. [19,12] or [19,13] or [1,19] or [19,1]
:2 - integer divide by two (gets middle indices) [9,6] [9,6] [0,9] [9,0]
Ʋ - last four links as a monad:
Ḃ - bit (n%2) (of the input) [1,0] [1,1] [1,1] [1,1]
$ - last two links as a monad (of the input):
Ị - insignificant? (abs(n)<=1) [0,0] [0,0] [1,0] [0,1]
Ḣ - head 0 0 1 0
_ - subtract (vectorises) [1,0] [1,1] [0,0] [1,1]
P - product 0 1 0 1
×ばつ - multiply (vectorises) [0,0] [9,6] [0,0] [9,0]
Julia 0.6, (削除) 28 (削除ここまで) 27 bytes
x\y=x&y&1&(x>1)*[x÷2,y÷2]
(-1 byte thanks to Jo King.)
Explanation:
x\y
- define an operator instead of a function, to save bytes
x&y&1
- check that both inputs are odd.
&(x>1)
- and that the first input is not 1
*
- the result of the above checks is 0 or 1, multiply that with:
[x÷2,y÷2]
- ÷
is integer division, so here for odd numbers gives (n-1)/2
i.e. the middle index. So this forms a vector of the two middle indices (which might be made [0, 0]
if any of the previous checks fail).
-
-
2\$\begingroup\$ Short answer: Yes thank you! Long answer: Your comment finally prompted me to check why those work on TIO but not on my system (I'd assumed it was just version differences). Turns out I can assign to those if I haven't used them yet in that session. Makes some confusing behaviour around this make sense now. \$\endgroup\$Sundar R– Sundar R2018年07月04日 13:44:29 +00:00Commented Jul 4, 2018 at 13:44
Lua, 77 bytes
x,y=...f=math.floor print((x%2==1 and y%2==1)and f(x/2)..","..f(y/2)or "0,0")
not the smallest language but submission for practicing :)
-
\$\begingroup\$ I don't know much Lua (although I wish I did!), but I believe you can replace the oddness check
x%2==1 and y%2==1
withx%2+y%2>1
Try it online! \$\endgroup\$Sundar R– Sundar R2018年07月04日 14:30:11 +00:00Commented Jul 4, 2018 at 14:30 -
2\$\begingroup\$ Also, this doesn't return
0,0
whenx
is 1, which is one of the question's requirements \$\endgroup\$Sundar R– Sundar R2018年07月04日 14:32:34 +00:00Commented Jul 4, 2018 at 14:32 -
\$\begingroup\$ You can extend the condition to check that too, here's my attempt: Try it online! (also changed
x%2+y%2
tox*y%2
after seeing Arnauld's answer.) \$\endgroup\$Sundar R– Sundar R2018年07月04日 14:45:19 +00:00Commented Jul 4, 2018 at 14:45 -
\$\begingroup\$ It's been a while since I've done anything in Lua, but it has the
//
operator yes? So you don't need the floor function \$\endgroup\$Jo King– Jo King2018年07月04日 14:49:42 +00:00Commented Jul 4, 2018 at 14:49
R, (削除) 38 (削除ここまで) (削除) 29 (削除ここまで) 28 bytes
An extra byte removed thanks to @Guiseppe
(i=scan()-1)/2*!any(i%%2,!i)
Get the input, subtract 1 store in i
and divide by 2 and multiply by not any evens or 0's in i
Python 2, (削除) 43 (削除ここまで) 41 bytes
lambda a,b:((0,0),(a/2,b/2))[a*b%2*(a>1)]
No rocket science here.
-2 with thanks to @MrXcoder and @Emigna
-
1\$\begingroup\$
[a*b%2*(a!=1)]
should work for 42 bytes. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2018年07月04日 13:01:04 +00:00Commented Jul 4, 2018 at 13:01 -
2\$\begingroup\$ @Mr.Xcoder: or even
a>1
. \$\endgroup\$Emigna– Emigna2018年07月04日 13:13:03 +00:00Commented Jul 4, 2018 at 13:13
output must be [0,0]
any reason behind that? How does that part add anything to computing the midpoint? Obviously, 1xn arrays or arrays with an even dimension don't have[0,0]
as their midpoints. \$\endgroup\$