Chop off the matrix to get the desired sum

Clash Royale CLAN TAG#URR8PPP
up vote
21
down vote
favorite
Definition
Given a matrix $M$ of non-negative integers and a non-negative integer $k$, we define $F_k$ as the "chop-off" function that removes all rows and all columns in $M$ that contain $k$.
Example:
$$beginalignM=pmatrixcolorred6&colorred1&colorwhitebbox[red,1pt]5\1&2&colorred8\colorred9&colorred8&colorwhitebbox[red,1pt]5\6&0&colorred4\\F_5(M)=pmatrix1&2\6&0endalign$$
Your task
Given $M$ and a target sum $S$, your task is to find all possible values of $k$ such that the sum of the remaining elements in $F_k(M)$ is equal to $S$.
Example:
Given the above matrix $M$ and $S=9$:
- $k=5$ is a solution, because $F_5(M)=pmatrix1&2\6&0$ and $1+2+6+0=9$
- $k=1$ is the only other possible solution: $F_1(M)=pmatrix5\4$ and $5+4=9$
So the expected output would be $1,5$.
Clarifications and rules
- The input is guaranteed to admit at least one solution.
- The sum of the elements in the original matrix is guaranteed to be greater than $S$.
- You may assume $S>0$. It means that an empty matrix will never lead to a solution.
- The values of $k$ may be printed or returned in any order and in any reasonable, unambiguous format.
- You are allowed not to deduplicate the output (e.g. $[1,1,5,5]$ or $[1,5,1,5]$ are considered valid answers for the above example).
- This is code-golf.
Test cases
M = [[6,1,5],[1,2,8],[9,8,5],[6,0,4]]
S = 9
Solution = 1,5
M = [[7,2],[1,4]]
S = 7
Solution = 4
M = [[12,5,2,3],[17,11,18,8]]
S = 43
Solution = 5
M = [[7,12],[10,5],[0,13]]
S = 17
Solution = 0,13
M = [[1,1,0,1],[2,0,0,2],[2,0,1,0]]
S = 1
Solution = 2
M = [[57,8,33,84],[84,78,19,14],[43,14,81,30]]
S = 236
Solution = 19,43,57
M = [[2,5,8],[3,5,8],[10,8,5],[10,6,7],[10,6,4]]
S = 49
Solution = 2,3,4,7
M = [[5,4,0],[3,0,4],[8,2,2]]
S = 8
Solution = 0,2,3,4,5,8
code-golf array-manipulation matrix
add a comment |Â
up vote
21
down vote
favorite
Definition
Given a matrix $M$ of non-negative integers and a non-negative integer $k$, we define $F_k$ as the "chop-off" function that removes all rows and all columns in $M$ that contain $k$.
Example:
$$beginalignM=pmatrixcolorred6&colorred1&colorwhitebbox[red,1pt]5\1&2&colorred8\colorred9&colorred8&colorwhitebbox[red,1pt]5\6&0&colorred4\\F_5(M)=pmatrix1&2\6&0endalign$$
Your task
Given $M$ and a target sum $S$, your task is to find all possible values of $k$ such that the sum of the remaining elements in $F_k(M)$ is equal to $S$.
Example:
Given the above matrix $M$ and $S=9$:
- $k=5$ is a solution, because $F_5(M)=pmatrix1&2\6&0$ and $1+2+6+0=9$
- $k=1$ is the only other possible solution: $F_1(M)=pmatrix5\4$ and $5+4=9$
So the expected output would be $1,5$.
Clarifications and rules
- The input is guaranteed to admit at least one solution.
- The sum of the elements in the original matrix is guaranteed to be greater than $S$.
- You may assume $S>0$. It means that an empty matrix will never lead to a solution.
- The values of $k$ may be printed or returned in any order and in any reasonable, unambiguous format.
- You are allowed not to deduplicate the output (e.g. $[1,1,5,5]$ or $[1,5,1,5]$ are considered valid answers for the above example).
- This is code-golf.
Test cases
M = [[6,1,5],[1,2,8],[9,8,5],[6,0,4]]
S = 9
Solution = 1,5
M = [[7,2],[1,4]]
S = 7
Solution = 4
M = [[12,5,2,3],[17,11,18,8]]
S = 43
Solution = 5
M = [[7,12],[10,5],[0,13]]
S = 17
Solution = 0,13
M = [[1,1,0,1],[2,0,0,2],[2,0,1,0]]
S = 1
Solution = 2
M = [[57,8,33,84],[84,78,19,14],[43,14,81,30]]
S = 236
Solution = 19,43,57
M = [[2,5,8],[3,5,8],[10,8,5],[10,6,7],[10,6,4]]
S = 49
Solution = 2,3,4,7
M = [[5,4,0],[3,0,4],[8,2,2]]
S = 8
Solution = 0,2,3,4,5,8
code-golf array-manipulation matrix
Would retaining the original structure of the input array (e.g.,[[1,5],[1],[5],]for the first test case) be a valid means of output?
â Shaggy
Sep 5 at 16:28
@Shaggy Yes. That looks reasonable.
â Arnauld
Sep 5 at 16:33
add a comment |Â
up vote
21
down vote
favorite
up vote
21
down vote
favorite
Definition
Given a matrix $M$ of non-negative integers and a non-negative integer $k$, we define $F_k$ as the "chop-off" function that removes all rows and all columns in $M$ that contain $k$.
Example:
$$beginalignM=pmatrixcolorred6&colorred1&colorwhitebbox[red,1pt]5\1&2&colorred8\colorred9&colorred8&colorwhitebbox[red,1pt]5\6&0&colorred4\\F_5(M)=pmatrix1&2\6&0endalign$$
Your task
Given $M$ and a target sum $S$, your task is to find all possible values of $k$ such that the sum of the remaining elements in $F_k(M)$ is equal to $S$.
Example:
Given the above matrix $M$ and $S=9$:
- $k=5$ is a solution, because $F_5(M)=pmatrix1&2\6&0$ and $1+2+6+0=9$
- $k=1$ is the only other possible solution: $F_1(M)=pmatrix5\4$ and $5+4=9$
So the expected output would be $1,5$.
Clarifications and rules
- The input is guaranteed to admit at least one solution.
- The sum of the elements in the original matrix is guaranteed to be greater than $S$.
- You may assume $S>0$. It means that an empty matrix will never lead to a solution.
- The values of $k$ may be printed or returned in any order and in any reasonable, unambiguous format.
- You are allowed not to deduplicate the output (e.g. $[1,1,5,5]$ or $[1,5,1,5]$ are considered valid answers for the above example).
- This is code-golf.
Test cases
M = [[6,1,5],[1,2,8],[9,8,5],[6,0,4]]
S = 9
Solution = 1,5
M = [[7,2],[1,4]]
S = 7
Solution = 4
M = [[12,5,2,3],[17,11,18,8]]
S = 43
Solution = 5
M = [[7,12],[10,5],[0,13]]
S = 17
Solution = 0,13
M = [[1,1,0,1],[2,0,0,2],[2,0,1,0]]
S = 1
Solution = 2
M = [[57,8,33,84],[84,78,19,14],[43,14,81,30]]
S = 236
Solution = 19,43,57
M = [[2,5,8],[3,5,8],[10,8,5],[10,6,7],[10,6,4]]
S = 49
Solution = 2,3,4,7
M = [[5,4,0],[3,0,4],[8,2,2]]
S = 8
Solution = 0,2,3,4,5,8
code-golf array-manipulation matrix
Definition
Given a matrix $M$ of non-negative integers and a non-negative integer $k$, we define $F_k$ as the "chop-off" function that removes all rows and all columns in $M$ that contain $k$.
Example:
$$beginalignM=pmatrixcolorred6&colorred1&colorwhitebbox[red,1pt]5\1&2&colorred8\colorred9&colorred8&colorwhitebbox[red,1pt]5\6&0&colorred4\\F_5(M)=pmatrix1&2\6&0endalign$$
Your task
Given $M$ and a target sum $S$, your task is to find all possible values of $k$ such that the sum of the remaining elements in $F_k(M)$ is equal to $S$.
Example:
Given the above matrix $M$ and $S=9$:
- $k=5$ is a solution, because $F_5(M)=pmatrix1&2\6&0$ and $1+2+6+0=9$
- $k=1$ is the only other possible solution: $F_1(M)=pmatrix5\4$ and $5+4=9$
So the expected output would be $1,5$.
Clarifications and rules
- The input is guaranteed to admit at least one solution.
- The sum of the elements in the original matrix is guaranteed to be greater than $S$.
- You may assume $S>0$. It means that an empty matrix will never lead to a solution.
- The values of $k$ may be printed or returned in any order and in any reasonable, unambiguous format.
- You are allowed not to deduplicate the output (e.g. $[1,1,5,5]$ or $[1,5,1,5]$ are considered valid answers for the above example).
- This is code-golf.
Test cases
M = [[6,1,5],[1,2,8],[9,8,5],[6,0,4]]
S = 9
Solution = 1,5
M = [[7,2],[1,4]]
S = 7
Solution = 4
M = [[12,5,2,3],[17,11,18,8]]
S = 43
Solution = 5
M = [[7,12],[10,5],[0,13]]
S = 17
Solution = 0,13
M = [[1,1,0,1],[2,0,0,2],[2,0,1,0]]
S = 1
Solution = 2
M = [[57,8,33,84],[84,78,19,14],[43,14,81,30]]
S = 236
Solution = 19,43,57
M = [[2,5,8],[3,5,8],[10,8,5],[10,6,7],[10,6,4]]
S = 49
Solution = 2,3,4,7
M = [[5,4,0],[3,0,4],[8,2,2]]
S = 8
Solution = 0,2,3,4,5,8
code-golf array-manipulation matrix
code-golf array-manipulation matrix
edited Sep 6 at 6:46
asked Sep 5 at 9:08
Arnauld
65.2k581275
65.2k581275
Would retaining the original structure of the input array (e.g.,[[1,5],[1],[5],]for the first test case) be a valid means of output?
â Shaggy
Sep 5 at 16:28
@Shaggy Yes. That looks reasonable.
â Arnauld
Sep 5 at 16:33
add a comment |Â
Would retaining the original structure of the input array (e.g.,[[1,5],[1],[5],]for the first test case) be a valid means of output?
â Shaggy
Sep 5 at 16:28
@Shaggy Yes. That looks reasonable.
â Arnauld
Sep 5 at 16:33
Would retaining the original structure of the input array (e.g.,
[[1,5],[1],[5],] for the first test case) be a valid means of output?â Shaggy
Sep 5 at 16:28
Would retaining the original structure of the input array (e.g.,
[[1,5],[1],[5],] for the first test case) be a valid means of output?â Shaggy
Sep 5 at 16:28
@Shaggy Yes. That looks reasonable.
â Arnauld
Sep 5 at 16:33
@Shaggy Yes. That looks reasonable.
â Arnauld
Sep 5 at 16:33
add a comment |Â
14 Answers
14
active
oldest
votes
up vote
10
down vote
K (ngn/k), 39 bytes
a@&y=x+//x*(&/'b)&:&/b:~x=y/:a:,/x
Try it online!
thanks @Adám for this explanation:
⦠function, x is M and y is S
âÂÂ,/xâÂÂflatten M (these are the k candidates)
âÂÂa:âÂÂassign to a
âÂÂxâ¦/:âÂÂapply the following function to each while using M as fixed left argument (x):
âÂÂâÂÂx=yâÂÂBoolean matrix indicating where elements of M equal the current k candidate
âÂÂâÂÂ~âÂÂnegate that
âÂÂâÂÂb:âÂÂassign that to b
âÂÂâÂÂ&/âÂÂAND reduction (finds columns without that k)
âÂÂâÂÂ(â¦)&:âÂÂAND that with each of the following:
âÂÂâÂÂâÂÂ&/'bâÂÂAND reduction of each (finds rows without that k)
âÂÂâÂÂx*âÂÂmultiply M by that
âÂÂâÂÂ+//âÂÂgrand sum
âÂÂy=âÂÂlist of Booleans indicating where S equals those sums
âÂÂ&âÂÂindices of Trues
âÂÂa@âÂÂuse that to index into the elements (the k candidates)
Feel free to correct the explanation.
â Adám
Sep 5 at 10:11
The dangers of copy-paste explanationâ¦
â Adám
Sep 5 at 10:15
add a comment |Â
up vote
6
down vote
APL (Dyalog Unicode), 35 33 28 bytesSBCS
-7 thanks to ngn.
Anonymous infix lambda. Takes S as left argument and M as right argument.
âµ[â¸âº=(+/âÂÂ,âµÃÂâ§/âÂÂ.â§â§â¿)èâµâ âÂÂâµ]
Try it online!
â¦âÂÂ"dfn", ⺠and âµ are left and right arguments (S and M) respectively:
âÂÂâµ[â¦]âÂÂindex M with the following coordinates:
âÂÂâÂÂâÂÂâµâÂÂenclose M to treat it as a single element
âÂÂâÂÂâµ=âÂÂcompare each element (i.e. k candidate) of M to that entire M
âÂÂâÂÂ(â¦)èâÂÂapply the following tacit function to each:
âÂÂâÂÂâÂÂâ§â¿âÂÂvertical AND reduction (finds columns without that k candidate)
â¦âÂÂ.â§âÂÂCartesian Boolean product with:
âÂÂâÂÂâÂÂâÂÂâ§/âÂÂhorizontal AND reduction (finds rows without that k candidate)
âÂÂâÂÂâÂÂâµÃÂâÂÂmultiply M with that mask
âÂÂâÂÂâÂÂ+/âÂÂ,âÂÂsum the flattened matrix
âÂÂâÂÂâº=âÂÂBoolean indicating where S equals those sums
âÂÂâÂÂâ¸âÂÂindices where that's true
1
M[â¸âº=+/,(â§â¿d)/Mâ¿â¨â§/dâÂÂMâ âµ¨MâÂÂâµ]
â ngn
Sep 5 at 10:06
@ngn Thanks. I won't use the global, though, as it makes order of evaluation confusing: â How can you index intoMwhen it hasn't been created yet?
â Adám
Sep 5 at 10:24
passingâµasâºin the inner dfn is equally confusing to me
â ngn
Sep 5 at 10:28
âµ[â¸âº=+/¨(,âµÃâ§/âÂÂ.â§â§â¿)¨âµâ âÂÂâµ]
â ngn
Sep 5 at 10:35
@ngn Yeah, I wanted to do something like that. Thanks!
â Adám
Sep 5 at 11:59
add a comment |Â
up vote
6
down vote
R, 78 73 bytes
function(m,s)m[Map(function(y)sum(m[(w=-which(m==y,T))[,1],w[,2]]),m)==s]
Try it online!
Does not sort or deduplicate the output.
Credit to J.Doe & Giuseppe for -5 bytes.
4
76 bytes
â J.Doe
Sep 5 at 12:55
4
@J.Doe 73 bytes
â Giuseppe
Sep 5 at 12:56
add a comment |Â
up vote
5
down vote
Jelly, 20 19 17 15 14 bytes
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF
This is a monadic link that takes M as argument and reads S from STDIN.
Try it online!
How it works
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF Main link. Argument: M
Z Zip; transpose the rows and columns of M.
p Take the Cartesian product of M and its transpose, yielding all pairs
(r, c) of rows and columns of M.
F Flatten; yield the elements of M.
nâ±® Not equal map; for each element e of M, compare the elements of the
pairs (r, c) with e.
æâ‰¬ All each each; for each array of Booleans corresponding to an (r, c)
pair, test if all of them are true.
F Flatten; yield the elements of M.
ḠTake the dot product of each list of resulting Booleans and the
elements of M.
ÃÂ Read an integer S from STDIN.
ẹ Find all indices of S in the dot products.
F Flatten; yield the elements of M.
á» Retrieve the elements of the right at the indices from the left.
Mark my words lol :) Nice answer, +1
â Mr. Xcoder
Sep 5 at 17:26
add a comment |Â
up vote
5
down vote
Haskell, 88 86 84 77 bytes
- -2 bytes thanks to BWO
- -7 bytes thanks to Tesseract
m!s=[k|k<-m>>=id,s==sum[x|r<-m,all(/=k)r,(i,x)<-zip[0..]r,all((/=k).(!!i))m]]
Verify all testcases.
Explanation
m ! s = -- function !, taking m and s as input
[k | -- the list of all k's such that
k <- m >>= id, -- * k is an entry of m
s == sum -- * s equals the sum of
[x | -- the list of x's such that
r <- m, -- * r is a row of m
all (/= k) r, -- * r does not contain k
(i, x) <- zip [0 ..] r, -- * i is a valid column index; also let x = r[i]
all ((/= k) . (!! i)) m -- * none of the rows contain k at index i
]
]
Should that say âÂÂfunction fâÂÂ?
â Quintec
Sep 5 at 11:47
1
@Quintec Indeed it should have, but I changed it to "function !" to save 2 bytes thanks to BWO
â Delfad0r
Sep 5 at 11:51
add a comment |Â
up vote
5
down vote
Pyth, Â 27 23 22 21Â 20 bytes
fqvzss.DRsxLTQ-I#TQs
Test suite!
Does not deduplicate.
How it works?
fqvzss.DRsxLTQ-I#TQs Full program.
f s Flatten M and keep only those elements T which satisfy:
qvzss.DRsxLTQ-I#TQ The filtering function. Breakdown:
-I#TQ Discard the rows that contain T. More elaborate explanation:
# Q |-> In M, keep only those elements that are...
I |-> Invariant under (equal to themselves after...)
- T |-> Removing T.
Let's call the result of this expression CR (chopped rows).
xLTQ Map over the rows M and retrieve all indices of T.
s Collect indices in 1D list (flatten). Call this I.
.DR For each row left in CR, remove the elements at indices in I.
ss Sum all the elements of this matrix flattened.
qvz And then finally check whether they equal S.
add a comment |Â
up vote
4
down vote
Python 2, 114 108 bytes
lambda m,s:a for a in sum(m,)if s==sum(v for l in m for i,v in enumerate(l)ifa-set(l)-set(zip(*m)[i]))
Try it online!
add a comment |Â
up vote
4
down vote
Perl 6, 80 bytes
$^s;@^m[*;*].grep($s==sum @m[($!=*.grep(:k,!*.grep($^v)))(@m);[$!([Z] @m)]])
Try it online!
add a comment |Â
up vote
3
down vote
05AB1E, 21 bytes
òÃÂÃÂQõZ+}øõZ<~}ø_*OOùQ
Try it online!
Only after I've written this answer have I seen Kevin's. I believe this is substantially different, so I am posting it separately. My intuition says that the optimal byte count is around 18, so I'll have to revisit this and see what else I can do. With the current code, it is impossible to write a test suite but I have verified all test cases myself and the results are correct.
Cropping Algorithm
To better illustrate the technique used, let's grab an example, with the element to crop $k=5$:
$$M=left(beginmatrix6&1&bbox[green,1pt]colorwhite5\1&2&8\9&8&bbox[green,1pt]colorwhite5\6&0&4endmatrixright)$$
It first generates the matrix of equality with $k$:
$$left(beginmatrix0&0&1\0&0&0\0&0&1\0&0&0endmatrixright)$$
Then, it goes through $M$ and for each row $R$, it adds $max(R)$ to each element in $R$, highlighting the necessary rows while still preserving the uniqueness of the horizontal positions of the searched element:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0endmatrixright)$$
Then, it goes through the transpose of $M$ and for each column $C$, it performs the operation $(max(C)-1)space ||space cspaceforallspace cin C:$ (where $||$ is 05AB1E's bitwise OR â addition should work too, so replace ~ with + if you want to test that too), which results in:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1endmatrixright)$$
Finally, it maps $0$ to $1$ and all other integers to $0$ and performs element-wise multiplication with $M$:
$$left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0\colorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0endmatrixright):longrightarrow:left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&2&colorgreen0\colorgreen0&colorgreen0&colorgreen0\6&0&colorgreen0endmatrixright)$$
After which the sum of the resulting matrix is computed.
1
Nice answer! I knew mine would be golfable for sure. I was already happy enough to have it working, including the annoying case of[[1,1,0,1],[2,0,0,2],[2,0,1,0]]which screwed me over for number1(which removes every column..) I indeed had slightly below 20 in my head as well as possibility. Too bad there are barely any builtins for matrices, despite the added product ones recently. As for the1|2(1 2~in 05AB1E synthax) resulting in 3, this is because thelogical ORacts like abinary ORwhen numbers other than0/1are involved (I think/assume).
â Kevin Cruijssen
Sep 5 at 13:21
@KevinCruijssen Oh you are right! Then, the docs should write bitwise OR, not logical OR. I am going to have to correct that soon. Anyway, bitwise OR should work as well I think. It can be replaced by+anyway I guess, so I hope there will be no issues with it :)
â Mr. Xcoder
Sep 5 at 13:24
add a comment |Â
up vote
2
down vote
05AB1E (legacy), 27 26 bytes
ÃÂÃÂéùõîÃÂ¥_}ùöÃÂîÃÂ¥_}öâÂÂö≬OPOIQ
Does not sort nor uniquify the result.
Only works in the legacy (for now), because sum-each seems to be doing some odd things when part of the inner lists are integers and other are lists..
Try it online or verify all test cases.
Explanation:
ÃÂ # Flatten the (implicit) matrix-input
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [6,1,5,1,2,8,9,8,5,6,0,4]
ÃÂ # Filter this list by:
é # Store the current value in a register-variable
ù # Take the matrix-input
õ } # Map it to:
îÃÂ¥_ # 0 if the current number is in this row, 1 if not
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] and 6 â [0,1,1,0]
ù # Take the matrix-input again
ö # Swap its rows and columns
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [[6,1,9,6],[1,2,8,0],[5,8,5,4]]
ÃÂ } # Filter it by:
îÃÂ¥_ # Only keep the inner lists that does not contain the current number
# i.e. [[6,1,9,6],[1,2,8,0],[5,8,5,4]] and 6 â [[1,2,8,0],[5,8,5,4]]
# i.e. [[1,2,2],[1,0,0],[0,0,1],[1,2,0]] and 1 âÂÂ
ö # After filtering, swap it's rows and columns back again
# i.e. [[1,2,8,0],[5,8,5,4]] â [[1,5],[2,8],[8,5],[0,4]]
âÂÂö # Pair both lists together and zip them
# i.e. [0,1,1,0] and [[1,5],[2,8],[8,5],[0,4]]
# â [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# i.e. [0,1,0] and â [[0,' '],[1,' '],[0,' ']]
⬠# Map each inner list / value to:
â¬O # Sum each
# i.e. [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# â [[0,6],[1,10],[1,13],[0,4]]
# i.e. [[0,' '],[1,' '],[0,' ']]
# â [[0,0],[1,0],[0,0]]
# (NOTE: For most test cases just `O` instead of `≬O` would be enough,
# but not if we removed ALL zipped inner lists for a number, like the
# second example above with input [[1,1,0,1],[2,0,0,2],[2,0,1,0]] and 1)
P # Now take the product of each inner list
# i.e. [[0,6],[1,10],[1,13],[0,4]] â [0,10,13,0]
O # Then take the sum of those
# i.e. [0,10,13,0] â 23
IQ # And only keep those that are equal to the number-input
# i.e. 23 and 9 â 0 (falsey), so it's removed from the flattened input
add a comment |Â
up vote
1
down vote
Jelly, 22 bytes
=+á¹Â$â¬Z$âºâÂÂìÃÂâ¸FS
Fóç=â¹òÃÂ
Try it online!
-6 bytes using the general approach from Mr. Xcoder's 05AB1E answer.
add a comment |Â
up vote
1
down vote
Charcoal, 33 bytes
FøFùâÂÂÃÂ
úIæÃÂ
â¼÷ãEøâ§ÉÂÂûùãEÃȉ§ÉÂÂEøçÃÂþùý
Try it online! Link is to verbose version of code and includes deduplication. Explanation:
FøFùâÂÂÃÂ
ú
Flatten the first input array q into the predefined list u.
ÃÂ
Flattened array
æ Filter elements
ø Input array
ï¼¥ Map over rows
ù Current element
û Current row
â Count matching elements
ì Logical Not
â§ Logical And
û Current row
ï¼¥ Map over columns
ø Input array
ï¼¥ Map over rows
ÃÂ Inner row
þ Column index
ç Inner element
ù Current element
â Count matching elements
ì Logical Not
â§ Logical And
ý Current element
ã Sum
ã Sum
÷ Second input
â¼ Equals
I Cast to string
Implicitly print each result on its own line
For each element of the list, sum the array, but if the row contains the element then use 0 instead of its sum, and when summing rows that don't contain the element, if the column contains the element then use 0 instead of the column's value. This is very slightly golfier than filtering the elements out as Charcoal can't sum an empty list.
add a comment |Â
up vote
1
down vote
Clean, 92 bytes
import StdEnv
$m s=[c\r<-m,c<-r|sum[b\a<-m|all((<>)c)a,b<-a&x<-[0..]|all(u=u!!x<>c)m]==s]
Try it online!
Explained:
$ m s // the function $ of `m` and `s`
= [ // is equal to
c // the value `c`
\ r <- m // for every row `r` in `m`
, c <- r // for every value `c` in `r`
| sum [ // where the sum of
b // the value `b`
\ a <- m // for every row `a` in `m`
| all ((<>)c) a // where `c` isn't in `a`
, b <- a // for every value `b` in `a`
& x <- [0..] // with every column index `x` from zero
| all (u = u!!x <> c) m // where `c` isn't in column `x`
] == s // equals `s`
]
add a comment |Â
up vote
1
down vote
MATLAB - 80 bytes
(Corrected and) Compacted:
function f(M,s);for k=M(:)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
And in a fully developped version:
function getthesum(M,s)
for k=M(:)' % For each element of M
x = M==k ; % Index elements equal to "k"
N = M( ~sum(x,2) , ~sum(x) ) ; % New matrix with only the appropriate rows/columns
if sum(sum(N))==s % sum rows and columns and compare to "s"
k % display "k" in console if "k" is valid
end
end
Thanks to the comments to highlight my initial mistake. Note that this version does not de-duplicate the output.
It is possible to deduplicate the output with 5 more bytes:
% This will only cycle through the unique elements of 'M' (85 bytes):
function f(M,s);for k=unique(M)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
1
k could be any element of the matrix.
â Dennisâ¦
Sep 5 at 19:12
@Dennis, oops that's right ... My bad, i'll correct it later today. Thanks for pointing it out.
â Hoki
Sep 6 at 11:04
1
@Arnauld. Sorry I was on holidays, that it fixed now.
â Hoki
Sep 17 at 17:05
add a comment |Â
14 Answers
14
active
oldest
votes
14 Answers
14
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
K (ngn/k), 39 bytes
a@&y=x+//x*(&/'b)&:&/b:~x=y/:a:,/x
Try it online!
thanks @Adám for this explanation:
⦠function, x is M and y is S
âÂÂ,/xâÂÂflatten M (these are the k candidates)
âÂÂa:âÂÂassign to a
âÂÂxâ¦/:âÂÂapply the following function to each while using M as fixed left argument (x):
âÂÂâÂÂx=yâÂÂBoolean matrix indicating where elements of M equal the current k candidate
âÂÂâÂÂ~âÂÂnegate that
âÂÂâÂÂb:âÂÂassign that to b
âÂÂâÂÂ&/âÂÂAND reduction (finds columns without that k)
âÂÂâÂÂ(â¦)&:âÂÂAND that with each of the following:
âÂÂâÂÂâÂÂ&/'bâÂÂAND reduction of each (finds rows without that k)
âÂÂâÂÂx*âÂÂmultiply M by that
âÂÂâÂÂ+//âÂÂgrand sum
âÂÂy=âÂÂlist of Booleans indicating where S equals those sums
âÂÂ&âÂÂindices of Trues
âÂÂa@âÂÂuse that to index into the elements (the k candidates)
Feel free to correct the explanation.
â Adám
Sep 5 at 10:11
The dangers of copy-paste explanationâ¦
â Adám
Sep 5 at 10:15
add a comment |Â
up vote
10
down vote
K (ngn/k), 39 bytes
a@&y=x+//x*(&/'b)&:&/b:~x=y/:a:,/x
Try it online!
thanks @Adám for this explanation:
⦠function, x is M and y is S
âÂÂ,/xâÂÂflatten M (these are the k candidates)
âÂÂa:âÂÂassign to a
âÂÂxâ¦/:âÂÂapply the following function to each while using M as fixed left argument (x):
âÂÂâÂÂx=yâÂÂBoolean matrix indicating where elements of M equal the current k candidate
âÂÂâÂÂ~âÂÂnegate that
âÂÂâÂÂb:âÂÂassign that to b
âÂÂâÂÂ&/âÂÂAND reduction (finds columns without that k)
âÂÂâÂÂ(â¦)&:âÂÂAND that with each of the following:
âÂÂâÂÂâÂÂ&/'bâÂÂAND reduction of each (finds rows without that k)
âÂÂâÂÂx*âÂÂmultiply M by that
âÂÂâÂÂ+//âÂÂgrand sum
âÂÂy=âÂÂlist of Booleans indicating where S equals those sums
âÂÂ&âÂÂindices of Trues
âÂÂa@âÂÂuse that to index into the elements (the k candidates)
Feel free to correct the explanation.
â Adám
Sep 5 at 10:11
The dangers of copy-paste explanationâ¦
â Adám
Sep 5 at 10:15
add a comment |Â
up vote
10
down vote
up vote
10
down vote
K (ngn/k), 39 bytes
a@&y=x+//x*(&/'b)&:&/b:~x=y/:a:,/x
Try it online!
thanks @Adám for this explanation:
⦠function, x is M and y is S
âÂÂ,/xâÂÂflatten M (these are the k candidates)
âÂÂa:âÂÂassign to a
âÂÂxâ¦/:âÂÂapply the following function to each while using M as fixed left argument (x):
âÂÂâÂÂx=yâÂÂBoolean matrix indicating where elements of M equal the current k candidate
âÂÂâÂÂ~âÂÂnegate that
âÂÂâÂÂb:âÂÂassign that to b
âÂÂâÂÂ&/âÂÂAND reduction (finds columns without that k)
âÂÂâÂÂ(â¦)&:âÂÂAND that with each of the following:
âÂÂâÂÂâÂÂ&/'bâÂÂAND reduction of each (finds rows without that k)
âÂÂâÂÂx*âÂÂmultiply M by that
âÂÂâÂÂ+//âÂÂgrand sum
âÂÂy=âÂÂlist of Booleans indicating where S equals those sums
âÂÂ&âÂÂindices of Trues
âÂÂa@âÂÂuse that to index into the elements (the k candidates)
K (ngn/k), 39 bytes
a@&y=x+//x*(&/'b)&:&/b:~x=y/:a:,/x
Try it online!
thanks @Adám for this explanation:
⦠function, x is M and y is S
âÂÂ,/xâÂÂflatten M (these are the k candidates)
âÂÂa:âÂÂassign to a
âÂÂxâ¦/:âÂÂapply the following function to each while using M as fixed left argument (x):
âÂÂâÂÂx=yâÂÂBoolean matrix indicating where elements of M equal the current k candidate
âÂÂâÂÂ~âÂÂnegate that
âÂÂâÂÂb:âÂÂassign that to b
âÂÂâÂÂ&/âÂÂAND reduction (finds columns without that k)
âÂÂâÂÂ(â¦)&:âÂÂAND that with each of the following:
âÂÂâÂÂâÂÂ&/'bâÂÂAND reduction of each (finds rows without that k)
âÂÂâÂÂx*âÂÂmultiply M by that
âÂÂâÂÂ+//âÂÂgrand sum
âÂÂy=âÂÂlist of Booleans indicating where S equals those sums
âÂÂ&âÂÂindices of Trues
âÂÂa@âÂÂuse that to index into the elements (the k candidates)
edited Sep 5 at 21:51
answered Sep 5 at 9:27
ngn
6,28812357
6,28812357
Feel free to correct the explanation.
â Adám
Sep 5 at 10:11
The dangers of copy-paste explanationâ¦
â Adám
Sep 5 at 10:15
add a comment |Â
Feel free to correct the explanation.
â Adám
Sep 5 at 10:11
The dangers of copy-paste explanationâ¦
â Adám
Sep 5 at 10:15
Feel free to correct the explanation.
â Adám
Sep 5 at 10:11
Feel free to correct the explanation.
â Adám
Sep 5 at 10:11
The dangers of copy-paste explanationâ¦
â Adám
Sep 5 at 10:15
The dangers of copy-paste explanationâ¦
â Adám
Sep 5 at 10:15
add a comment |Â
up vote
6
down vote
APL (Dyalog Unicode), 35 33 28 bytesSBCS
-7 thanks to ngn.
Anonymous infix lambda. Takes S as left argument and M as right argument.
âµ[â¸âº=(+/âÂÂ,âµÃÂâ§/âÂÂ.â§â§â¿)èâµâ âÂÂâµ]
Try it online!
â¦âÂÂ"dfn", ⺠and âµ are left and right arguments (S and M) respectively:
âÂÂâµ[â¦]âÂÂindex M with the following coordinates:
âÂÂâÂÂâÂÂâµâÂÂenclose M to treat it as a single element
âÂÂâÂÂâµ=âÂÂcompare each element (i.e. k candidate) of M to that entire M
âÂÂâÂÂ(â¦)èâÂÂapply the following tacit function to each:
âÂÂâÂÂâÂÂâ§â¿âÂÂvertical AND reduction (finds columns without that k candidate)
â¦âÂÂ.â§âÂÂCartesian Boolean product with:
âÂÂâÂÂâÂÂâÂÂâ§/âÂÂhorizontal AND reduction (finds rows without that k candidate)
âÂÂâÂÂâÂÂâµÃÂâÂÂmultiply M with that mask
âÂÂâÂÂâÂÂ+/âÂÂ,âÂÂsum the flattened matrix
âÂÂâÂÂâº=âÂÂBoolean indicating where S equals those sums
âÂÂâÂÂâ¸âÂÂindices where that's true
1
M[â¸âº=+/,(â§â¿d)/Mâ¿â¨â§/dâÂÂMâ âµ¨MâÂÂâµ]
â ngn
Sep 5 at 10:06
@ngn Thanks. I won't use the global, though, as it makes order of evaluation confusing: â How can you index intoMwhen it hasn't been created yet?
â Adám
Sep 5 at 10:24
passingâµasâºin the inner dfn is equally confusing to me
â ngn
Sep 5 at 10:28
âµ[â¸âº=+/¨(,âµÃâ§/âÂÂ.â§â§â¿)¨âµâ âÂÂâµ]
â ngn
Sep 5 at 10:35
@ngn Yeah, I wanted to do something like that. Thanks!
â Adám
Sep 5 at 11:59
add a comment |Â
up vote
6
down vote
APL (Dyalog Unicode), 35 33 28 bytesSBCS
-7 thanks to ngn.
Anonymous infix lambda. Takes S as left argument and M as right argument.
âµ[â¸âº=(+/âÂÂ,âµÃÂâ§/âÂÂ.â§â§â¿)èâµâ âÂÂâµ]
Try it online!
â¦âÂÂ"dfn", ⺠and âµ are left and right arguments (S and M) respectively:
âÂÂâµ[â¦]âÂÂindex M with the following coordinates:
âÂÂâÂÂâÂÂâµâÂÂenclose M to treat it as a single element
âÂÂâÂÂâµ=âÂÂcompare each element (i.e. k candidate) of M to that entire M
âÂÂâÂÂ(â¦)èâÂÂapply the following tacit function to each:
âÂÂâÂÂâÂÂâ§â¿âÂÂvertical AND reduction (finds columns without that k candidate)
â¦âÂÂ.â§âÂÂCartesian Boolean product with:
âÂÂâÂÂâÂÂâÂÂâ§/âÂÂhorizontal AND reduction (finds rows without that k candidate)
âÂÂâÂÂâÂÂâµÃÂâÂÂmultiply M with that mask
âÂÂâÂÂâÂÂ+/âÂÂ,âÂÂsum the flattened matrix
âÂÂâÂÂâº=âÂÂBoolean indicating where S equals those sums
âÂÂâÂÂâ¸âÂÂindices where that's true
1
M[â¸âº=+/,(â§â¿d)/Mâ¿â¨â§/dâÂÂMâ âµ¨MâÂÂâµ]
â ngn
Sep 5 at 10:06
@ngn Thanks. I won't use the global, though, as it makes order of evaluation confusing: â How can you index intoMwhen it hasn't been created yet?
â Adám
Sep 5 at 10:24
passingâµasâºin the inner dfn is equally confusing to me
â ngn
Sep 5 at 10:28
âµ[â¸âº=+/¨(,âµÃâ§/âÂÂ.â§â§â¿)¨âµâ âÂÂâµ]
â ngn
Sep 5 at 10:35
@ngn Yeah, I wanted to do something like that. Thanks!
â Adám
Sep 5 at 11:59
add a comment |Â
up vote
6
down vote
up vote
6
down vote
APL (Dyalog Unicode), 35 33 28 bytesSBCS
-7 thanks to ngn.
Anonymous infix lambda. Takes S as left argument and M as right argument.
âµ[â¸âº=(+/âÂÂ,âµÃÂâ§/âÂÂ.â§â§â¿)èâµâ âÂÂâµ]
Try it online!
â¦âÂÂ"dfn", ⺠and âµ are left and right arguments (S and M) respectively:
âÂÂâµ[â¦]âÂÂindex M with the following coordinates:
âÂÂâÂÂâÂÂâµâÂÂenclose M to treat it as a single element
âÂÂâÂÂâµ=âÂÂcompare each element (i.e. k candidate) of M to that entire M
âÂÂâÂÂ(â¦)èâÂÂapply the following tacit function to each:
âÂÂâÂÂâÂÂâ§â¿âÂÂvertical AND reduction (finds columns without that k candidate)
â¦âÂÂ.â§âÂÂCartesian Boolean product with:
âÂÂâÂÂâÂÂâÂÂâ§/âÂÂhorizontal AND reduction (finds rows without that k candidate)
âÂÂâÂÂâÂÂâµÃÂâÂÂmultiply M with that mask
âÂÂâÂÂâÂÂ+/âÂÂ,âÂÂsum the flattened matrix
âÂÂâÂÂâº=âÂÂBoolean indicating where S equals those sums
âÂÂâÂÂâ¸âÂÂindices where that's true
APL (Dyalog Unicode), 35 33 28 bytesSBCS
-7 thanks to ngn.
Anonymous infix lambda. Takes S as left argument and M as right argument.
âµ[â¸âº=(+/âÂÂ,âµÃÂâ§/âÂÂ.â§â§â¿)èâµâ âÂÂâµ]
Try it online!
â¦âÂÂ"dfn", ⺠and âµ are left and right arguments (S and M) respectively:
âÂÂâµ[â¦]âÂÂindex M with the following coordinates:
âÂÂâÂÂâÂÂâµâÂÂenclose M to treat it as a single element
âÂÂâÂÂâµ=âÂÂcompare each element (i.e. k candidate) of M to that entire M
âÂÂâÂÂ(â¦)èâÂÂapply the following tacit function to each:
âÂÂâÂÂâÂÂâ§â¿âÂÂvertical AND reduction (finds columns without that k candidate)
â¦âÂÂ.â§âÂÂCartesian Boolean product with:
âÂÂâÂÂâÂÂâÂÂâ§/âÂÂhorizontal AND reduction (finds rows without that k candidate)
âÂÂâÂÂâÂÂâµÃÂâÂÂmultiply M with that mask
âÂÂâÂÂâÂÂ+/âÂÂ,âÂÂsum the flattened matrix
âÂÂâÂÂâº=âÂÂBoolean indicating where S equals those sums
âÂÂâÂÂâ¸âÂÂindices where that's true
edited Sep 5 at 12:04
answered Sep 5 at 9:42
Adám
27.7k268185
27.7k268185
1
M[â¸âº=+/,(â§â¿d)/Mâ¿â¨â§/dâÂÂMâ âµ¨MâÂÂâµ]
â ngn
Sep 5 at 10:06
@ngn Thanks. I won't use the global, though, as it makes order of evaluation confusing: â How can you index intoMwhen it hasn't been created yet?
â Adám
Sep 5 at 10:24
passingâµasâºin the inner dfn is equally confusing to me
â ngn
Sep 5 at 10:28
âµ[â¸âº=+/¨(,âµÃâ§/âÂÂ.â§â§â¿)¨âµâ âÂÂâµ]
â ngn
Sep 5 at 10:35
@ngn Yeah, I wanted to do something like that. Thanks!
â Adám
Sep 5 at 11:59
add a comment |Â
1
M[â¸âº=+/,(â§â¿d)/Mâ¿â¨â§/dâÂÂMâ âµ¨MâÂÂâµ]
â ngn
Sep 5 at 10:06
@ngn Thanks. I won't use the global, though, as it makes order of evaluation confusing: â How can you index intoMwhen it hasn't been created yet?
â Adám
Sep 5 at 10:24
passingâµasâºin the inner dfn is equally confusing to me
â ngn
Sep 5 at 10:28
âµ[â¸âº=+/¨(,âµÃâ§/âÂÂ.â§â§â¿)¨âµâ âÂÂâµ]
â ngn
Sep 5 at 10:35
@ngn Yeah, I wanted to do something like that. Thanks!
â Adám
Sep 5 at 11:59
1
1
M[â¸âº=+/,(â§â¿d)/Mâ¿â¨â§/dâÂÂMâ âµ¨MâÂÂâµ]â ngn
Sep 5 at 10:06
M[â¸âº=+/,(â§â¿d)/Mâ¿â¨â§/dâÂÂMâ âµ¨MâÂÂâµ]â ngn
Sep 5 at 10:06
@ngn Thanks. I won't use the global, though, as it makes order of evaluation confusing: â How can you index into
M when it hasn't been created yet?â Adám
Sep 5 at 10:24
@ngn Thanks. I won't use the global, though, as it makes order of evaluation confusing: â How can you index into
M when it hasn't been created yet?â Adám
Sep 5 at 10:24
passing
âµ as ⺠in the inner dfn is equally confusing to meâ ngn
Sep 5 at 10:28
passing
âµ as ⺠in the inner dfn is equally confusing to meâ ngn
Sep 5 at 10:28
âµ[â¸âº=+/¨(,âµÃâ§/âÂÂ.â§â§â¿)¨âµâ âÂÂâµ]â ngn
Sep 5 at 10:35
âµ[â¸âº=+/¨(,âµÃâ§/âÂÂ.â§â§â¿)¨âµâ âÂÂâµ]â ngn
Sep 5 at 10:35
@ngn Yeah, I wanted to do something like that. Thanks!
â Adám
Sep 5 at 11:59
@ngn Yeah, I wanted to do something like that. Thanks!
â Adám
Sep 5 at 11:59
add a comment |Â
up vote
6
down vote
R, 78 73 bytes
function(m,s)m[Map(function(y)sum(m[(w=-which(m==y,T))[,1],w[,2]]),m)==s]
Try it online!
Does not sort or deduplicate the output.
Credit to J.Doe & Giuseppe for -5 bytes.
4
76 bytes
â J.Doe
Sep 5 at 12:55
4
@J.Doe 73 bytes
â Giuseppe
Sep 5 at 12:56
add a comment |Â
up vote
6
down vote
R, 78 73 bytes
function(m,s)m[Map(function(y)sum(m[(w=-which(m==y,T))[,1],w[,2]]),m)==s]
Try it online!
Does not sort or deduplicate the output.
Credit to J.Doe & Giuseppe for -5 bytes.
4
76 bytes
â J.Doe
Sep 5 at 12:55
4
@J.Doe 73 bytes
â Giuseppe
Sep 5 at 12:56
add a comment |Â
up vote
6
down vote
up vote
6
down vote
R, 78 73 bytes
function(m,s)m[Map(function(y)sum(m[(w=-which(m==y,T))[,1],w[,2]]),m)==s]
Try it online!
Does not sort or deduplicate the output.
Credit to J.Doe & Giuseppe for -5 bytes.
R, 78 73 bytes
function(m,s)m[Map(function(y)sum(m[(w=-which(m==y,T))[,1],w[,2]]),m)==s]
Try it online!
Does not sort or deduplicate the output.
Credit to J.Doe & Giuseppe for -5 bytes.
edited Sep 5 at 13:03
answered Sep 5 at 11:40
Kirill L.
2,6961117
2,6961117
4
76 bytes
â J.Doe
Sep 5 at 12:55
4
@J.Doe 73 bytes
â Giuseppe
Sep 5 at 12:56
add a comment |Â
4
76 bytes
â J.Doe
Sep 5 at 12:55
4
@J.Doe 73 bytes
â Giuseppe
Sep 5 at 12:56
4
4
76 bytes
â J.Doe
Sep 5 at 12:55
76 bytes
â J.Doe
Sep 5 at 12:55
4
4
@J.Doe 73 bytes
â Giuseppe
Sep 5 at 12:56
@J.Doe 73 bytes
â Giuseppe
Sep 5 at 12:56
add a comment |Â
up vote
5
down vote
Jelly, 20 19 17 15 14 bytes
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF
This is a monadic link that takes M as argument and reads S from STDIN.
Try it online!
How it works
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF Main link. Argument: M
Z Zip; transpose the rows and columns of M.
p Take the Cartesian product of M and its transpose, yielding all pairs
(r, c) of rows and columns of M.
F Flatten; yield the elements of M.
nâ±® Not equal map; for each element e of M, compare the elements of the
pairs (r, c) with e.
æâ‰¬ All each each; for each array of Booleans corresponding to an (r, c)
pair, test if all of them are true.
F Flatten; yield the elements of M.
ḠTake the dot product of each list of resulting Booleans and the
elements of M.
ÃÂ Read an integer S from STDIN.
ẹ Find all indices of S in the dot products.
F Flatten; yield the elements of M.
á» Retrieve the elements of the right at the indices from the left.
Mark my words lol :) Nice answer, +1
â Mr. Xcoder
Sep 5 at 17:26
add a comment |Â
up vote
5
down vote
Jelly, 20 19 17 15 14 bytes
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF
This is a monadic link that takes M as argument and reads S from STDIN.
Try it online!
How it works
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF Main link. Argument: M
Z Zip; transpose the rows and columns of M.
p Take the Cartesian product of M and its transpose, yielding all pairs
(r, c) of rows and columns of M.
F Flatten; yield the elements of M.
nâ±® Not equal map; for each element e of M, compare the elements of the
pairs (r, c) with e.
æâ‰¬ All each each; for each array of Booleans corresponding to an (r, c)
pair, test if all of them are true.
F Flatten; yield the elements of M.
ḠTake the dot product of each list of resulting Booleans and the
elements of M.
ÃÂ Read an integer S from STDIN.
ẹ Find all indices of S in the dot products.
F Flatten; yield the elements of M.
á» Retrieve the elements of the right at the indices from the left.
Mark my words lol :) Nice answer, +1
â Mr. Xcoder
Sep 5 at 17:26
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Jelly, 20 19 17 15 14 bytes
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF
This is a monadic link that takes M as argument and reads S from STDIN.
Try it online!
How it works
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF Main link. Argument: M
Z Zip; transpose the rows and columns of M.
p Take the Cartesian product of M and its transpose, yielding all pairs
(r, c) of rows and columns of M.
F Flatten; yield the elements of M.
nâ±® Not equal map; for each element e of M, compare the elements of the
pairs (r, c) with e.
æâ‰¬ All each each; for each array of Booleans corresponding to an (r, c)
pair, test if all of them are true.
F Flatten; yield the elements of M.
ḠTake the dot product of each list of resulting Booleans and the
elements of M.
ÃÂ Read an integer S from STDIN.
ẹ Find all indices of S in the dot products.
F Flatten; yield the elements of M.
á» Retrieve the elements of the right at the indices from the left.
Jelly, 20 19 17 15 14 bytes
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF
This is a monadic link that takes M as argument and reads S from STDIN.
Try it online!
How it works
pZnâ±®Fæâ‰¬á¸ÂFẹÃÂá»ÂF Main link. Argument: M
Z Zip; transpose the rows and columns of M.
p Take the Cartesian product of M and its transpose, yielding all pairs
(r, c) of rows and columns of M.
F Flatten; yield the elements of M.
nâ±® Not equal map; for each element e of M, compare the elements of the
pairs (r, c) with e.
æâ‰¬ All each each; for each array of Booleans corresponding to an (r, c)
pair, test if all of them are true.
F Flatten; yield the elements of M.
ḠTake the dot product of each list of resulting Booleans and the
elements of M.
ÃÂ Read an integer S from STDIN.
ẹ Find all indices of S in the dot products.
F Flatten; yield the elements of M.
á» Retrieve the elements of the right at the indices from the left.
edited Sep 5 at 17:31
answered Sep 5 at 15:37
Dennisâ¦
182k32292725
182k32292725
Mark my words lol :) Nice answer, +1
â Mr. Xcoder
Sep 5 at 17:26
add a comment |Â
Mark my words lol :) Nice answer, +1
â Mr. Xcoder
Sep 5 at 17:26
Mark my words lol :) Nice answer, +1
â Mr. Xcoder
Sep 5 at 17:26
Mark my words lol :) Nice answer, +1
â Mr. Xcoder
Sep 5 at 17:26
add a comment |Â
up vote
5
down vote
Haskell, 88 86 84 77 bytes
- -2 bytes thanks to BWO
- -7 bytes thanks to Tesseract
m!s=[k|k<-m>>=id,s==sum[x|r<-m,all(/=k)r,(i,x)<-zip[0..]r,all((/=k).(!!i))m]]
Verify all testcases.
Explanation
m ! s = -- function !, taking m and s as input
[k | -- the list of all k's such that
k <- m >>= id, -- * k is an entry of m
s == sum -- * s equals the sum of
[x | -- the list of x's such that
r <- m, -- * r is a row of m
all (/= k) r, -- * r does not contain k
(i, x) <- zip [0 ..] r, -- * i is a valid column index; also let x = r[i]
all ((/= k) . (!! i)) m -- * none of the rows contain k at index i
]
]
Should that say âÂÂfunction fâÂÂ?
â Quintec
Sep 5 at 11:47
1
@Quintec Indeed it should have, but I changed it to "function !" to save 2 bytes thanks to BWO
â Delfad0r
Sep 5 at 11:51
add a comment |Â
up vote
5
down vote
Haskell, 88 86 84 77 bytes
- -2 bytes thanks to BWO
- -7 bytes thanks to Tesseract
m!s=[k|k<-m>>=id,s==sum[x|r<-m,all(/=k)r,(i,x)<-zip[0..]r,all((/=k).(!!i))m]]
Verify all testcases.
Explanation
m ! s = -- function !, taking m and s as input
[k | -- the list of all k's such that
k <- m >>= id, -- * k is an entry of m
s == sum -- * s equals the sum of
[x | -- the list of x's such that
r <- m, -- * r is a row of m
all (/= k) r, -- * r does not contain k
(i, x) <- zip [0 ..] r, -- * i is a valid column index; also let x = r[i]
all ((/= k) . (!! i)) m -- * none of the rows contain k at index i
]
]
Should that say âÂÂfunction fâÂÂ?
â Quintec
Sep 5 at 11:47
1
@Quintec Indeed it should have, but I changed it to "function !" to save 2 bytes thanks to BWO
â Delfad0r
Sep 5 at 11:51
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Haskell, 88 86 84 77 bytes
- -2 bytes thanks to BWO
- -7 bytes thanks to Tesseract
m!s=[k|k<-m>>=id,s==sum[x|r<-m,all(/=k)r,(i,x)<-zip[0..]r,all((/=k).(!!i))m]]
Verify all testcases.
Explanation
m ! s = -- function !, taking m and s as input
[k | -- the list of all k's such that
k <- m >>= id, -- * k is an entry of m
s == sum -- * s equals the sum of
[x | -- the list of x's such that
r <- m, -- * r is a row of m
all (/= k) r, -- * r does not contain k
(i, x) <- zip [0 ..] r, -- * i is a valid column index; also let x = r[i]
all ((/= k) . (!! i)) m -- * none of the rows contain k at index i
]
]
Haskell, 88 86 84 77 bytes
- -2 bytes thanks to BWO
- -7 bytes thanks to Tesseract
m!s=[k|k<-m>>=id,s==sum[x|r<-m,all(/=k)r,(i,x)<-zip[0..]r,all((/=k).(!!i))m]]
Verify all testcases.
Explanation
m ! s = -- function !, taking m and s as input
[k | -- the list of all k's such that
k <- m >>= id, -- * k is an entry of m
s == sum -- * s equals the sum of
[x | -- the list of x's such that
r <- m, -- * r is a row of m
all (/= k) r, -- * r does not contain k
(i, x) <- zip [0 ..] r, -- * i is a valid column index; also let x = r[i]
all ((/= k) . (!! i)) m -- * none of the rows contain k at index i
]
]
edited Sep 5 at 18:42
answered Sep 5 at 10:49
Delfad0r
608110
608110
Should that say âÂÂfunction fâÂÂ?
â Quintec
Sep 5 at 11:47
1
@Quintec Indeed it should have, but I changed it to "function !" to save 2 bytes thanks to BWO
â Delfad0r
Sep 5 at 11:51
add a comment |Â
Should that say âÂÂfunction fâÂÂ?
â Quintec
Sep 5 at 11:47
1
@Quintec Indeed it should have, but I changed it to "function !" to save 2 bytes thanks to BWO
â Delfad0r
Sep 5 at 11:51
Should that say âÂÂfunction fâÂÂ?
â Quintec
Sep 5 at 11:47
Should that say âÂÂfunction fâÂÂ?
â Quintec
Sep 5 at 11:47
1
1
@Quintec Indeed it should have, but I changed it to "function !" to save 2 bytes thanks to BWO
â Delfad0r
Sep 5 at 11:51
@Quintec Indeed it should have, but I changed it to "function !" to save 2 bytes thanks to BWO
â Delfad0r
Sep 5 at 11:51
add a comment |Â
up vote
5
down vote
Pyth, Â 27 23 22 21Â 20 bytes
fqvzss.DRsxLTQ-I#TQs
Test suite!
Does not deduplicate.
How it works?
fqvzss.DRsxLTQ-I#TQs Full program.
f s Flatten M and keep only those elements T which satisfy:
qvzss.DRsxLTQ-I#TQ The filtering function. Breakdown:
-I#TQ Discard the rows that contain T. More elaborate explanation:
# Q |-> In M, keep only those elements that are...
I |-> Invariant under (equal to themselves after...)
- T |-> Removing T.
Let's call the result of this expression CR (chopped rows).
xLTQ Map over the rows M and retrieve all indices of T.
s Collect indices in 1D list (flatten). Call this I.
.DR For each row left in CR, remove the elements at indices in I.
ss Sum all the elements of this matrix flattened.
qvz And then finally check whether they equal S.
add a comment |Â
up vote
5
down vote
Pyth, Â 27 23 22 21Â 20 bytes
fqvzss.DRsxLTQ-I#TQs
Test suite!
Does not deduplicate.
How it works?
fqvzss.DRsxLTQ-I#TQs Full program.
f s Flatten M and keep only those elements T which satisfy:
qvzss.DRsxLTQ-I#TQ The filtering function. Breakdown:
-I#TQ Discard the rows that contain T. More elaborate explanation:
# Q |-> In M, keep only those elements that are...
I |-> Invariant under (equal to themselves after...)
- T |-> Removing T.
Let's call the result of this expression CR (chopped rows).
xLTQ Map over the rows M and retrieve all indices of T.
s Collect indices in 1D list (flatten). Call this I.
.DR For each row left in CR, remove the elements at indices in I.
ss Sum all the elements of this matrix flattened.
qvz And then finally check whether they equal S.
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Pyth, Â 27 23 22 21Â 20 bytes
fqvzss.DRsxLTQ-I#TQs
Test suite!
Does not deduplicate.
How it works?
fqvzss.DRsxLTQ-I#TQs Full program.
f s Flatten M and keep only those elements T which satisfy:
qvzss.DRsxLTQ-I#TQ The filtering function. Breakdown:
-I#TQ Discard the rows that contain T. More elaborate explanation:
# Q |-> In M, keep only those elements that are...
I |-> Invariant under (equal to themselves after...)
- T |-> Removing T.
Let's call the result of this expression CR (chopped rows).
xLTQ Map over the rows M and retrieve all indices of T.
s Collect indices in 1D list (flatten). Call this I.
.DR For each row left in CR, remove the elements at indices in I.
ss Sum all the elements of this matrix flattened.
qvz And then finally check whether they equal S.
Pyth, Â 27 23 22 21Â 20 bytes
fqvzss.DRsxLTQ-I#TQs
Test suite!
Does not deduplicate.
How it works?
fqvzss.DRsxLTQ-I#TQs Full program.
f s Flatten M and keep only those elements T which satisfy:
qvzss.DRsxLTQ-I#TQ The filtering function. Breakdown:
-I#TQ Discard the rows that contain T. More elaborate explanation:
# Q |-> In M, keep only those elements that are...
I |-> Invariant under (equal to themselves after...)
- T |-> Removing T.
Let's call the result of this expression CR (chopped rows).
xLTQ Map over the rows M and retrieve all indices of T.
s Collect indices in 1D list (flatten). Call this I.
.DR For each row left in CR, remove the elements at indices in I.
ss Sum all the elements of this matrix flattened.
qvz And then finally check whether they equal S.
edited Sep 5 at 20:09
answered Sep 5 at 10:03
Mr. Xcoder
30.6k758194
30.6k758194
add a comment |Â
add a comment |Â
up vote
4
down vote
Python 2, 114 108 bytes
lambda m,s:a for a in sum(m,)if s==sum(v for l in m for i,v in enumerate(l)ifa-set(l)-set(zip(*m)[i]))
Try it online!
add a comment |Â
up vote
4
down vote
Python 2, 114 108 bytes
lambda m,s:a for a in sum(m,)if s==sum(v for l in m for i,v in enumerate(l)ifa-set(l)-set(zip(*m)[i]))
Try it online!
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Python 2, 114 108 bytes
lambda m,s:a for a in sum(m,)if s==sum(v for l in m for i,v in enumerate(l)ifa-set(l)-set(zip(*m)[i]))
Try it online!
Python 2, 114 108 bytes
lambda m,s:a for a in sum(m,)if s==sum(v for l in m for i,v in enumerate(l)ifa-set(l)-set(zip(*m)[i]))
Try it online!
answered Sep 5 at 10:04
TFeld
11.9k2833
11.9k2833
add a comment |Â
add a comment |Â
up vote
4
down vote
Perl 6, 80 bytes
$^s;@^m[*;*].grep($s==sum @m[($!=*.grep(:k,!*.grep($^v)))(@m);[$!([Z] @m)]])
Try it online!
add a comment |Â
up vote
4
down vote
Perl 6, 80 bytes
$^s;@^m[*;*].grep($s==sum @m[($!=*.grep(:k,!*.grep($^v)))(@m);[$!([Z] @m)]])
Try it online!
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Perl 6, 80 bytes
$^s;@^m[*;*].grep($s==sum @m[($!=*.grep(:k,!*.grep($^v)))(@m);[$!([Z] @m)]])
Try it online!
Perl 6, 80 bytes
$^s;@^m[*;*].grep($s==sum @m[($!=*.grep(:k,!*.grep($^v)))(@m);[$!([Z] @m)]])
Try it online!
edited Sep 5 at 11:44
answered Sep 5 at 11:30
nwellnhof
3,833715
3,833715
add a comment |Â
add a comment |Â
up vote
3
down vote
05AB1E, 21 bytes
òÃÂÃÂQõZ+}øõZ<~}ø_*OOùQ
Try it online!
Only after I've written this answer have I seen Kevin's. I believe this is substantially different, so I am posting it separately. My intuition says that the optimal byte count is around 18, so I'll have to revisit this and see what else I can do. With the current code, it is impossible to write a test suite but I have verified all test cases myself and the results are correct.
Cropping Algorithm
To better illustrate the technique used, let's grab an example, with the element to crop $k=5$:
$$M=left(beginmatrix6&1&bbox[green,1pt]colorwhite5\1&2&8\9&8&bbox[green,1pt]colorwhite5\6&0&4endmatrixright)$$
It first generates the matrix of equality with $k$:
$$left(beginmatrix0&0&1\0&0&0\0&0&1\0&0&0endmatrixright)$$
Then, it goes through $M$ and for each row $R$, it adds $max(R)$ to each element in $R$, highlighting the necessary rows while still preserving the uniqueness of the horizontal positions of the searched element:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0endmatrixright)$$
Then, it goes through the transpose of $M$ and for each column $C$, it performs the operation $(max(C)-1)space ||space cspaceforallspace cin C:$ (where $||$ is 05AB1E's bitwise OR â addition should work too, so replace ~ with + if you want to test that too), which results in:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1endmatrixright)$$
Finally, it maps $0$ to $1$ and all other integers to $0$ and performs element-wise multiplication with $M$:
$$left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0\colorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0endmatrixright):longrightarrow:left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&2&colorgreen0\colorgreen0&colorgreen0&colorgreen0\6&0&colorgreen0endmatrixright)$$
After which the sum of the resulting matrix is computed.
1
Nice answer! I knew mine would be golfable for sure. I was already happy enough to have it working, including the annoying case of[[1,1,0,1],[2,0,0,2],[2,0,1,0]]which screwed me over for number1(which removes every column..) I indeed had slightly below 20 in my head as well as possibility. Too bad there are barely any builtins for matrices, despite the added product ones recently. As for the1|2(1 2~in 05AB1E synthax) resulting in 3, this is because thelogical ORacts like abinary ORwhen numbers other than0/1are involved (I think/assume).
â Kevin Cruijssen
Sep 5 at 13:21
@KevinCruijssen Oh you are right! Then, the docs should write bitwise OR, not logical OR. I am going to have to correct that soon. Anyway, bitwise OR should work as well I think. It can be replaced by+anyway I guess, so I hope there will be no issues with it :)
â Mr. Xcoder
Sep 5 at 13:24
add a comment |Â
up vote
3
down vote
05AB1E, 21 bytes
òÃÂÃÂQõZ+}øõZ<~}ø_*OOùQ
Try it online!
Only after I've written this answer have I seen Kevin's. I believe this is substantially different, so I am posting it separately. My intuition says that the optimal byte count is around 18, so I'll have to revisit this and see what else I can do. With the current code, it is impossible to write a test suite but I have verified all test cases myself and the results are correct.
Cropping Algorithm
To better illustrate the technique used, let's grab an example, with the element to crop $k=5$:
$$M=left(beginmatrix6&1&bbox[green,1pt]colorwhite5\1&2&8\9&8&bbox[green,1pt]colorwhite5\6&0&4endmatrixright)$$
It first generates the matrix of equality with $k$:
$$left(beginmatrix0&0&1\0&0&0\0&0&1\0&0&0endmatrixright)$$
Then, it goes through $M$ and for each row $R$, it adds $max(R)$ to each element in $R$, highlighting the necessary rows while still preserving the uniqueness of the horizontal positions of the searched element:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0endmatrixright)$$
Then, it goes through the transpose of $M$ and for each column $C$, it performs the operation $(max(C)-1)space ||space cspaceforallspace cin C:$ (where $||$ is 05AB1E's bitwise OR â addition should work too, so replace ~ with + if you want to test that too), which results in:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1endmatrixright)$$
Finally, it maps $0$ to $1$ and all other integers to $0$ and performs element-wise multiplication with $M$:
$$left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0\colorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0endmatrixright):longrightarrow:left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&2&colorgreen0\colorgreen0&colorgreen0&colorgreen0\6&0&colorgreen0endmatrixright)$$
After which the sum of the resulting matrix is computed.
1
Nice answer! I knew mine would be golfable for sure. I was already happy enough to have it working, including the annoying case of[[1,1,0,1],[2,0,0,2],[2,0,1,0]]which screwed me over for number1(which removes every column..) I indeed had slightly below 20 in my head as well as possibility. Too bad there are barely any builtins for matrices, despite the added product ones recently. As for the1|2(1 2~in 05AB1E synthax) resulting in 3, this is because thelogical ORacts like abinary ORwhen numbers other than0/1are involved (I think/assume).
â Kevin Cruijssen
Sep 5 at 13:21
@KevinCruijssen Oh you are right! Then, the docs should write bitwise OR, not logical OR. I am going to have to correct that soon. Anyway, bitwise OR should work as well I think. It can be replaced by+anyway I guess, so I hope there will be no issues with it :)
â Mr. Xcoder
Sep 5 at 13:24
add a comment |Â
up vote
3
down vote
up vote
3
down vote
05AB1E, 21 bytes
òÃÂÃÂQõZ+}øõZ<~}ø_*OOùQ
Try it online!
Only after I've written this answer have I seen Kevin's. I believe this is substantially different, so I am posting it separately. My intuition says that the optimal byte count is around 18, so I'll have to revisit this and see what else I can do. With the current code, it is impossible to write a test suite but I have verified all test cases myself and the results are correct.
Cropping Algorithm
To better illustrate the technique used, let's grab an example, with the element to crop $k=5$:
$$M=left(beginmatrix6&1&bbox[green,1pt]colorwhite5\1&2&8\9&8&bbox[green,1pt]colorwhite5\6&0&4endmatrixright)$$
It first generates the matrix of equality with $k$:
$$left(beginmatrix0&0&1\0&0&0\0&0&1\0&0&0endmatrixright)$$
Then, it goes through $M$ and for each row $R$, it adds $max(R)$ to each element in $R$, highlighting the necessary rows while still preserving the uniqueness of the horizontal positions of the searched element:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0endmatrixright)$$
Then, it goes through the transpose of $M$ and for each column $C$, it performs the operation $(max(C)-1)space ||space cspaceforallspace cin C:$ (where $||$ is 05AB1E's bitwise OR â addition should work too, so replace ~ with + if you want to test that too), which results in:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1endmatrixright)$$
Finally, it maps $0$ to $1$ and all other integers to $0$ and performs element-wise multiplication with $M$:
$$left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0\colorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0endmatrixright):longrightarrow:left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&2&colorgreen0\colorgreen0&colorgreen0&colorgreen0\6&0&colorgreen0endmatrixright)$$
After which the sum of the resulting matrix is computed.
05AB1E, 21 bytes
òÃÂÃÂQõZ+}øõZ<~}ø_*OOùQ
Try it online!
Only after I've written this answer have I seen Kevin's. I believe this is substantially different, so I am posting it separately. My intuition says that the optimal byte count is around 18, so I'll have to revisit this and see what else I can do. With the current code, it is impossible to write a test suite but I have verified all test cases myself and the results are correct.
Cropping Algorithm
To better illustrate the technique used, let's grab an example, with the element to crop $k=5$:
$$M=left(beginmatrix6&1&bbox[green,1pt]colorwhite5\1&2&8\9&8&bbox[green,1pt]colorwhite5\6&0&4endmatrixright)$$
It first generates the matrix of equality with $k$:
$$left(beginmatrix0&0&1\0&0&0\0&0&1\0&0&0endmatrixright)$$
Then, it goes through $M$ and for each row $R$, it adds $max(R)$ to each element in $R$, highlighting the necessary rows while still preserving the uniqueness of the horizontal positions of the searched element:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite2\0&0&0endmatrixright)$$
Then, it goes through the transpose of $M$ and for each column $C$, it performs the operation $(max(C)-1)space ||space cspaceforallspace cin C:$ (where $||$ is 05AB1E's bitwise OR â addition should work too, so replace ~ with + if you want to test that too), which results in:
$$left(beginmatrixcolorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1\colorgreen1&colorgreen1&bbox[green,1pt]colorwhite3\0&0&colorgreen1endmatrixright)$$
Finally, it maps $0$ to $1$ and all other integers to $0$ and performs element-wise multiplication with $M$:
$$left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0\colorgreen0&colorgreen0&colorgreen0\1&1&colorgreen0endmatrixright):longrightarrow:left(beginmatrixcolorgreen0&colorgreen0&colorgreen0\1&2&colorgreen0\colorgreen0&colorgreen0&colorgreen0\6&0&colorgreen0endmatrixright)$$
After which the sum of the resulting matrix is computed.
edited Sep 5 at 13:27
answered Sep 5 at 13:11
Mr. Xcoder
30.6k758194
30.6k758194
1
Nice answer! I knew mine would be golfable for sure. I was already happy enough to have it working, including the annoying case of[[1,1,0,1],[2,0,0,2],[2,0,1,0]]which screwed me over for number1(which removes every column..) I indeed had slightly below 20 in my head as well as possibility. Too bad there are barely any builtins for matrices, despite the added product ones recently. As for the1|2(1 2~in 05AB1E synthax) resulting in 3, this is because thelogical ORacts like abinary ORwhen numbers other than0/1are involved (I think/assume).
â Kevin Cruijssen
Sep 5 at 13:21
@KevinCruijssen Oh you are right! Then, the docs should write bitwise OR, not logical OR. I am going to have to correct that soon. Anyway, bitwise OR should work as well I think. It can be replaced by+anyway I guess, so I hope there will be no issues with it :)
â Mr. Xcoder
Sep 5 at 13:24
add a comment |Â
1
Nice answer! I knew mine would be golfable for sure. I was already happy enough to have it working, including the annoying case of[[1,1,0,1],[2,0,0,2],[2,0,1,0]]which screwed me over for number1(which removes every column..) I indeed had slightly below 20 in my head as well as possibility. Too bad there are barely any builtins for matrices, despite the added product ones recently. As for the1|2(1 2~in 05AB1E synthax) resulting in 3, this is because thelogical ORacts like abinary ORwhen numbers other than0/1are involved (I think/assume).
â Kevin Cruijssen
Sep 5 at 13:21
@KevinCruijssen Oh you are right! Then, the docs should write bitwise OR, not logical OR. I am going to have to correct that soon. Anyway, bitwise OR should work as well I think. It can be replaced by+anyway I guess, so I hope there will be no issues with it :)
â Mr. Xcoder
Sep 5 at 13:24
1
1
Nice answer! I knew mine would be golfable for sure. I was already happy enough to have it working, including the annoying case of
[[1,1,0,1],[2,0,0,2],[2,0,1,0]] which screwed me over for number 1 (which removes every column..) I indeed had slightly below 20 in my head as well as possibility. Too bad there are barely any builtins for matrices, despite the added product ones recently. As for the 1|2 (1 2~ in 05AB1E synthax) resulting in 3, this is because the logical OR acts like a binary OR when numbers other than 0/1 are involved (I think/assume).â Kevin Cruijssen
Sep 5 at 13:21
Nice answer! I knew mine would be golfable for sure. I was already happy enough to have it working, including the annoying case of
[[1,1,0,1],[2,0,0,2],[2,0,1,0]] which screwed me over for number 1 (which removes every column..) I indeed had slightly below 20 in my head as well as possibility. Too bad there are barely any builtins for matrices, despite the added product ones recently. As for the 1|2 (1 2~ in 05AB1E synthax) resulting in 3, this is because the logical OR acts like a binary OR when numbers other than 0/1 are involved (I think/assume).â Kevin Cruijssen
Sep 5 at 13:21
@KevinCruijssen Oh you are right! Then, the docs should write bitwise OR, not logical OR. I am going to have to correct that soon. Anyway, bitwise OR should work as well I think. It can be replaced by
+ anyway I guess, so I hope there will be no issues with it :)â Mr. Xcoder
Sep 5 at 13:24
@KevinCruijssen Oh you are right! Then, the docs should write bitwise OR, not logical OR. I am going to have to correct that soon. Anyway, bitwise OR should work as well I think. It can be replaced by
+ anyway I guess, so I hope there will be no issues with it :)â Mr. Xcoder
Sep 5 at 13:24
add a comment |Â
up vote
2
down vote
05AB1E (legacy), 27 26 bytes
ÃÂÃÂéùõîÃÂ¥_}ùöÃÂîÃÂ¥_}öâÂÂö≬OPOIQ
Does not sort nor uniquify the result.
Only works in the legacy (for now), because sum-each seems to be doing some odd things when part of the inner lists are integers and other are lists..
Try it online or verify all test cases.
Explanation:
ÃÂ # Flatten the (implicit) matrix-input
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [6,1,5,1,2,8,9,8,5,6,0,4]
ÃÂ # Filter this list by:
é # Store the current value in a register-variable
ù # Take the matrix-input
õ } # Map it to:
îÃÂ¥_ # 0 if the current number is in this row, 1 if not
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] and 6 â [0,1,1,0]
ù # Take the matrix-input again
ö # Swap its rows and columns
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [[6,1,9,6],[1,2,8,0],[5,8,5,4]]
ÃÂ } # Filter it by:
îÃÂ¥_ # Only keep the inner lists that does not contain the current number
# i.e. [[6,1,9,6],[1,2,8,0],[5,8,5,4]] and 6 â [[1,2,8,0],[5,8,5,4]]
# i.e. [[1,2,2],[1,0,0],[0,0,1],[1,2,0]] and 1 âÂÂ
ö # After filtering, swap it's rows and columns back again
# i.e. [[1,2,8,0],[5,8,5,4]] â [[1,5],[2,8],[8,5],[0,4]]
âÂÂö # Pair both lists together and zip them
# i.e. [0,1,1,0] and [[1,5],[2,8],[8,5],[0,4]]
# â [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# i.e. [0,1,0] and â [[0,' '],[1,' '],[0,' ']]
⬠# Map each inner list / value to:
â¬O # Sum each
# i.e. [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# â [[0,6],[1,10],[1,13],[0,4]]
# i.e. [[0,' '],[1,' '],[0,' ']]
# â [[0,0],[1,0],[0,0]]
# (NOTE: For most test cases just `O` instead of `≬O` would be enough,
# but not if we removed ALL zipped inner lists for a number, like the
# second example above with input [[1,1,0,1],[2,0,0,2],[2,0,1,0]] and 1)
P # Now take the product of each inner list
# i.e. [[0,6],[1,10],[1,13],[0,4]] â [0,10,13,0]
O # Then take the sum of those
# i.e. [0,10,13,0] â 23
IQ # And only keep those that are equal to the number-input
# i.e. 23 and 9 â 0 (falsey), so it's removed from the flattened input
add a comment |Â
up vote
2
down vote
05AB1E (legacy), 27 26 bytes
ÃÂÃÂéùõîÃÂ¥_}ùöÃÂîÃÂ¥_}öâÂÂö≬OPOIQ
Does not sort nor uniquify the result.
Only works in the legacy (for now), because sum-each seems to be doing some odd things when part of the inner lists are integers and other are lists..
Try it online or verify all test cases.
Explanation:
ÃÂ # Flatten the (implicit) matrix-input
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [6,1,5,1,2,8,9,8,5,6,0,4]
ÃÂ # Filter this list by:
é # Store the current value in a register-variable
ù # Take the matrix-input
õ } # Map it to:
îÃÂ¥_ # 0 if the current number is in this row, 1 if not
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] and 6 â [0,1,1,0]
ù # Take the matrix-input again
ö # Swap its rows and columns
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [[6,1,9,6],[1,2,8,0],[5,8,5,4]]
ÃÂ } # Filter it by:
îÃÂ¥_ # Only keep the inner lists that does not contain the current number
# i.e. [[6,1,9,6],[1,2,8,0],[5,8,5,4]] and 6 â [[1,2,8,0],[5,8,5,4]]
# i.e. [[1,2,2],[1,0,0],[0,0,1],[1,2,0]] and 1 âÂÂ
ö # After filtering, swap it's rows and columns back again
# i.e. [[1,2,8,0],[5,8,5,4]] â [[1,5],[2,8],[8,5],[0,4]]
âÂÂö # Pair both lists together and zip them
# i.e. [0,1,1,0] and [[1,5],[2,8],[8,5],[0,4]]
# â [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# i.e. [0,1,0] and â [[0,' '],[1,' '],[0,' ']]
⬠# Map each inner list / value to:
â¬O # Sum each
# i.e. [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# â [[0,6],[1,10],[1,13],[0,4]]
# i.e. [[0,' '],[1,' '],[0,' ']]
# â [[0,0],[1,0],[0,0]]
# (NOTE: For most test cases just `O` instead of `≬O` would be enough,
# but not if we removed ALL zipped inner lists for a number, like the
# second example above with input [[1,1,0,1],[2,0,0,2],[2,0,1,0]] and 1)
P # Now take the product of each inner list
# i.e. [[0,6],[1,10],[1,13],[0,4]] â [0,10,13,0]
O # Then take the sum of those
# i.e. [0,10,13,0] â 23
IQ # And only keep those that are equal to the number-input
# i.e. 23 and 9 â 0 (falsey), so it's removed from the flattened input
add a comment |Â
up vote
2
down vote
up vote
2
down vote
05AB1E (legacy), 27 26 bytes
ÃÂÃÂéùõîÃÂ¥_}ùöÃÂîÃÂ¥_}öâÂÂö≬OPOIQ
Does not sort nor uniquify the result.
Only works in the legacy (for now), because sum-each seems to be doing some odd things when part of the inner lists are integers and other are lists..
Try it online or verify all test cases.
Explanation:
ÃÂ # Flatten the (implicit) matrix-input
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [6,1,5,1,2,8,9,8,5,6,0,4]
ÃÂ # Filter this list by:
é # Store the current value in a register-variable
ù # Take the matrix-input
õ } # Map it to:
îÃÂ¥_ # 0 if the current number is in this row, 1 if not
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] and 6 â [0,1,1,0]
ù # Take the matrix-input again
ö # Swap its rows and columns
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [[6,1,9,6],[1,2,8,0],[5,8,5,4]]
ÃÂ } # Filter it by:
îÃÂ¥_ # Only keep the inner lists that does not contain the current number
# i.e. [[6,1,9,6],[1,2,8,0],[5,8,5,4]] and 6 â [[1,2,8,0],[5,8,5,4]]
# i.e. [[1,2,2],[1,0,0],[0,0,1],[1,2,0]] and 1 âÂÂ
ö # After filtering, swap it's rows and columns back again
# i.e. [[1,2,8,0],[5,8,5,4]] â [[1,5],[2,8],[8,5],[0,4]]
âÂÂö # Pair both lists together and zip them
# i.e. [0,1,1,0] and [[1,5],[2,8],[8,5],[0,4]]
# â [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# i.e. [0,1,0] and â [[0,' '],[1,' '],[0,' ']]
⬠# Map each inner list / value to:
â¬O # Sum each
# i.e. [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# â [[0,6],[1,10],[1,13],[0,4]]
# i.e. [[0,' '],[1,' '],[0,' ']]
# â [[0,0],[1,0],[0,0]]
# (NOTE: For most test cases just `O` instead of `≬O` would be enough,
# but not if we removed ALL zipped inner lists for a number, like the
# second example above with input [[1,1,0,1],[2,0,0,2],[2,0,1,0]] and 1)
P # Now take the product of each inner list
# i.e. [[0,6],[1,10],[1,13],[0,4]] â [0,10,13,0]
O # Then take the sum of those
# i.e. [0,10,13,0] â 23
IQ # And only keep those that are equal to the number-input
# i.e. 23 and 9 â 0 (falsey), so it's removed from the flattened input
05AB1E (legacy), 27 26 bytes
ÃÂÃÂéùõîÃÂ¥_}ùöÃÂîÃÂ¥_}öâÂÂö≬OPOIQ
Does not sort nor uniquify the result.
Only works in the legacy (for now), because sum-each seems to be doing some odd things when part of the inner lists are integers and other are lists..
Try it online or verify all test cases.
Explanation:
ÃÂ # Flatten the (implicit) matrix-input
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [6,1,5,1,2,8,9,8,5,6,0,4]
ÃÂ # Filter this list by:
é # Store the current value in a register-variable
ù # Take the matrix-input
õ } # Map it to:
îÃÂ¥_ # 0 if the current number is in this row, 1 if not
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] and 6 â [0,1,1,0]
ù # Take the matrix-input again
ö # Swap its rows and columns
# i.e. [[6,1,5],[1,2,8],[9,8,5],[6,0,4]] â [[6,1,9,6],[1,2,8,0],[5,8,5,4]]
ÃÂ } # Filter it by:
îÃÂ¥_ # Only keep the inner lists that does not contain the current number
# i.e. [[6,1,9,6],[1,2,8,0],[5,8,5,4]] and 6 â [[1,2,8,0],[5,8,5,4]]
# i.e. [[1,2,2],[1,0,0],[0,0,1],[1,2,0]] and 1 âÂÂ
ö # After filtering, swap it's rows and columns back again
# i.e. [[1,2,8,0],[5,8,5,4]] â [[1,5],[2,8],[8,5],[0,4]]
âÂÂö # Pair both lists together and zip them
# i.e. [0,1,1,0] and [[1,5],[2,8],[8,5],[0,4]]
# â [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# i.e. [0,1,0] and â [[0,' '],[1,' '],[0,' ']]
⬠# Map each inner list / value to:
â¬O # Sum each
# i.e. [[0,[1,5]],[1,[2,8]],[1,[8,5]],[0,[0,4]]]
# â [[0,6],[1,10],[1,13],[0,4]]
# i.e. [[0,' '],[1,' '],[0,' ']]
# â [[0,0],[1,0],[0,0]]
# (NOTE: For most test cases just `O` instead of `≬O` would be enough,
# but not if we removed ALL zipped inner lists for a number, like the
# second example above with input [[1,1,0,1],[2,0,0,2],[2,0,1,0]] and 1)
P # Now take the product of each inner list
# i.e. [[0,6],[1,10],[1,13],[0,4]] â [0,10,13,0]
O # Then take the sum of those
# i.e. [0,10,13,0] â 23
IQ # And only keep those that are equal to the number-input
# i.e. 23 and 9 â 0 (falsey), so it's removed from the flattened input
edited Sep 5 at 13:06
answered Sep 5 at 11:58
Kevin Cruijssen
30.5k553167
30.5k553167
add a comment |Â
add a comment |Â
up vote
1
down vote
Jelly, 22 bytes
=+á¹Â$â¬Z$âºâÂÂìÃÂâ¸FS
Fóç=â¹òÃÂ
Try it online!
-6 bytes using the general approach from Mr. Xcoder's 05AB1E answer.
add a comment |Â
up vote
1
down vote
Jelly, 22 bytes
=+á¹Â$â¬Z$âºâÂÂìÃÂâ¸FS
Fóç=â¹òÃÂ
Try it online!
-6 bytes using the general approach from Mr. Xcoder's 05AB1E answer.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Jelly, 22 bytes
=+á¹Â$â¬Z$âºâÂÂìÃÂâ¸FS
Fóç=â¹òÃÂ
Try it online!
-6 bytes using the general approach from Mr. Xcoder's 05AB1E answer.
Jelly, 22 bytes
=+á¹Â$â¬Z$âºâÂÂìÃÂâ¸FS
Fóç=â¹òÃÂ
Try it online!
-6 bytes using the general approach from Mr. Xcoder's 05AB1E answer.
edited Sep 5 at 14:50
answered Sep 5 at 14:26
HyperNeutrino
18.6k437147
18.6k437147
add a comment |Â
add a comment |Â
up vote
1
down vote
Charcoal, 33 bytes
FøFùâÂÂÃÂ
úIæÃÂ
â¼÷ãEøâ§ÉÂÂûùãEÃȉ§ÉÂÂEøçÃÂþùý
Try it online! Link is to verbose version of code and includes deduplication. Explanation:
FøFùâÂÂÃÂ
ú
Flatten the first input array q into the predefined list u.
ÃÂ
Flattened array
æ Filter elements
ø Input array
ï¼¥ Map over rows
ù Current element
û Current row
â Count matching elements
ì Logical Not
â§ Logical And
û Current row
ï¼¥ Map over columns
ø Input array
ï¼¥ Map over rows
ÃÂ Inner row
þ Column index
ç Inner element
ù Current element
â Count matching elements
ì Logical Not
â§ Logical And
ý Current element
ã Sum
ã Sum
÷ Second input
â¼ Equals
I Cast to string
Implicitly print each result on its own line
For each element of the list, sum the array, but if the row contains the element then use 0 instead of its sum, and when summing rows that don't contain the element, if the column contains the element then use 0 instead of the column's value. This is very slightly golfier than filtering the elements out as Charcoal can't sum an empty list.
add a comment |Â
up vote
1
down vote
Charcoal, 33 bytes
FøFùâÂÂÃÂ
úIæÃÂ
â¼÷ãEøâ§ÉÂÂûùãEÃȉ§ÉÂÂEøçÃÂþùý
Try it online! Link is to verbose version of code and includes deduplication. Explanation:
FøFùâÂÂÃÂ
ú
Flatten the first input array q into the predefined list u.
ÃÂ
Flattened array
æ Filter elements
ø Input array
ï¼¥ Map over rows
ù Current element
û Current row
â Count matching elements
ì Logical Not
â§ Logical And
û Current row
ï¼¥ Map over columns
ø Input array
ï¼¥ Map over rows
ÃÂ Inner row
þ Column index
ç Inner element
ù Current element
â Count matching elements
ì Logical Not
â§ Logical And
ý Current element
ã Sum
ã Sum
÷ Second input
â¼ Equals
I Cast to string
Implicitly print each result on its own line
For each element of the list, sum the array, but if the row contains the element then use 0 instead of its sum, and when summing rows that don't contain the element, if the column contains the element then use 0 instead of the column's value. This is very slightly golfier than filtering the elements out as Charcoal can't sum an empty list.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Charcoal, 33 bytes
FøFùâÂÂÃÂ
úIæÃÂ
â¼÷ãEøâ§ÉÂÂûùãEÃȉ§ÉÂÂEøçÃÂþùý
Try it online! Link is to verbose version of code and includes deduplication. Explanation:
FøFùâÂÂÃÂ
ú
Flatten the first input array q into the predefined list u.
ÃÂ
Flattened array
æ Filter elements
ø Input array
ï¼¥ Map over rows
ù Current element
û Current row
â Count matching elements
ì Logical Not
â§ Logical And
û Current row
ï¼¥ Map over columns
ø Input array
ï¼¥ Map over rows
ÃÂ Inner row
þ Column index
ç Inner element
ù Current element
â Count matching elements
ì Logical Not
â§ Logical And
ý Current element
ã Sum
ã Sum
÷ Second input
â¼ Equals
I Cast to string
Implicitly print each result on its own line
For each element of the list, sum the array, but if the row contains the element then use 0 instead of its sum, and when summing rows that don't contain the element, if the column contains the element then use 0 instead of the column's value. This is very slightly golfier than filtering the elements out as Charcoal can't sum an empty list.
Charcoal, 33 bytes
FøFùâÂÂÃÂ
úIæÃÂ
â¼÷ãEøâ§ÉÂÂûùãEÃȉ§ÉÂÂEøçÃÂþùý
Try it online! Link is to verbose version of code and includes deduplication. Explanation:
FøFùâÂÂÃÂ
ú
Flatten the first input array q into the predefined list u.
ÃÂ
Flattened array
æ Filter elements
ø Input array
ï¼¥ Map over rows
ù Current element
û Current row
â Count matching elements
ì Logical Not
â§ Logical And
û Current row
ï¼¥ Map over columns
ø Input array
ï¼¥ Map over rows
ÃÂ Inner row
þ Column index
ç Inner element
ù Current element
â Count matching elements
ì Logical Not
â§ Logical And
ý Current element
ã Sum
ã Sum
÷ Second input
â¼ Equals
I Cast to string
Implicitly print each result on its own line
For each element of the list, sum the array, but if the row contains the element then use 0 instead of its sum, and when summing rows that don't contain the element, if the column contains the element then use 0 instead of the column's value. This is very slightly golfier than filtering the elements out as Charcoal can't sum an empty list.
answered Sep 5 at 15:59
Neil
75.7k744171
75.7k744171
add a comment |Â
add a comment |Â
up vote
1
down vote
Clean, 92 bytes
import StdEnv
$m s=[c\r<-m,c<-r|sum[b\a<-m|all((<>)c)a,b<-a&x<-[0..]|all(u=u!!x<>c)m]==s]
Try it online!
Explained:
$ m s // the function $ of `m` and `s`
= [ // is equal to
c // the value `c`
\ r <- m // for every row `r` in `m`
, c <- r // for every value `c` in `r`
| sum [ // where the sum of
b // the value `b`
\ a <- m // for every row `a` in `m`
| all ((<>)c) a // where `c` isn't in `a`
, b <- a // for every value `b` in `a`
& x <- [0..] // with every column index `x` from zero
| all (u = u!!x <> c) m // where `c` isn't in column `x`
] == s // equals `s`
]
add a comment |Â
up vote
1
down vote
Clean, 92 bytes
import StdEnv
$m s=[c\r<-m,c<-r|sum[b\a<-m|all((<>)c)a,b<-a&x<-[0..]|all(u=u!!x<>c)m]==s]
Try it online!
Explained:
$ m s // the function $ of `m` and `s`
= [ // is equal to
c // the value `c`
\ r <- m // for every row `r` in `m`
, c <- r // for every value `c` in `r`
| sum [ // where the sum of
b // the value `b`
\ a <- m // for every row `a` in `m`
| all ((<>)c) a // where `c` isn't in `a`
, b <- a // for every value `b` in `a`
& x <- [0..] // with every column index `x` from zero
| all (u = u!!x <> c) m // where `c` isn't in column `x`
] == s // equals `s`
]
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Clean, 92 bytes
import StdEnv
$m s=[c\r<-m,c<-r|sum[b\a<-m|all((<>)c)a,b<-a&x<-[0..]|all(u=u!!x<>c)m]==s]
Try it online!
Explained:
$ m s // the function $ of `m` and `s`
= [ // is equal to
c // the value `c`
\ r <- m // for every row `r` in `m`
, c <- r // for every value `c` in `r`
| sum [ // where the sum of
b // the value `b`
\ a <- m // for every row `a` in `m`
| all ((<>)c) a // where `c` isn't in `a`
, b <- a // for every value `b` in `a`
& x <- [0..] // with every column index `x` from zero
| all (u = u!!x <> c) m // where `c` isn't in column `x`
] == s // equals `s`
]
Clean, 92 bytes
import StdEnv
$m s=[c\r<-m,c<-r|sum[b\a<-m|all((<>)c)a,b<-a&x<-[0..]|all(u=u!!x<>c)m]==s]
Try it online!
Explained:
$ m s // the function $ of `m` and `s`
= [ // is equal to
c // the value `c`
\ r <- m // for every row `r` in `m`
, c <- r // for every value `c` in `r`
| sum [ // where the sum of
b // the value `b`
\ a <- m // for every row `a` in `m`
| all ((<>)c) a // where `c` isn't in `a`
, b <- a // for every value `b` in `a`
& x <- [0..] // with every column index `x` from zero
| all (u = u!!x <> c) m // where `c` isn't in column `x`
] == s // equals `s`
]
answered Sep 5 at 23:05
ÃÂurous
5,33311031
5,33311031
add a comment |Â
add a comment |Â
up vote
1
down vote
MATLAB - 80 bytes
(Corrected and) Compacted:
function f(M,s);for k=M(:)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
And in a fully developped version:
function getthesum(M,s)
for k=M(:)' % For each element of M
x = M==k ; % Index elements equal to "k"
N = M( ~sum(x,2) , ~sum(x) ) ; % New matrix with only the appropriate rows/columns
if sum(sum(N))==s % sum rows and columns and compare to "s"
k % display "k" in console if "k" is valid
end
end
Thanks to the comments to highlight my initial mistake. Note that this version does not de-duplicate the output.
It is possible to deduplicate the output with 5 more bytes:
% This will only cycle through the unique elements of 'M' (85 bytes):
function f(M,s);for k=unique(M)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
1
k could be any element of the matrix.
â Dennisâ¦
Sep 5 at 19:12
@Dennis, oops that's right ... My bad, i'll correct it later today. Thanks for pointing it out.
â Hoki
Sep 6 at 11:04
1
@Arnauld. Sorry I was on holidays, that it fixed now.
â Hoki
Sep 17 at 17:05
add a comment |Â
up vote
1
down vote
MATLAB - 80 bytes
(Corrected and) Compacted:
function f(M,s);for k=M(:)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
And in a fully developped version:
function getthesum(M,s)
for k=M(:)' % For each element of M
x = M==k ; % Index elements equal to "k"
N = M( ~sum(x,2) , ~sum(x) ) ; % New matrix with only the appropriate rows/columns
if sum(sum(N))==s % sum rows and columns and compare to "s"
k % display "k" in console if "k" is valid
end
end
Thanks to the comments to highlight my initial mistake. Note that this version does not de-duplicate the output.
It is possible to deduplicate the output with 5 more bytes:
% This will only cycle through the unique elements of 'M' (85 bytes):
function f(M,s);for k=unique(M)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
1
k could be any element of the matrix.
â Dennisâ¦
Sep 5 at 19:12
@Dennis, oops that's right ... My bad, i'll correct it later today. Thanks for pointing it out.
â Hoki
Sep 6 at 11:04
1
@Arnauld. Sorry I was on holidays, that it fixed now.
â Hoki
Sep 17 at 17:05
add a comment |Â
up vote
1
down vote
up vote
1
down vote
MATLAB - 80 bytes
(Corrected and) Compacted:
function f(M,s);for k=M(:)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
And in a fully developped version:
function getthesum(M,s)
for k=M(:)' % For each element of M
x = M==k ; % Index elements equal to "k"
N = M( ~sum(x,2) , ~sum(x) ) ; % New matrix with only the appropriate rows/columns
if sum(sum(N))==s % sum rows and columns and compare to "s"
k % display "k" in console if "k" is valid
end
end
Thanks to the comments to highlight my initial mistake. Note that this version does not de-duplicate the output.
It is possible to deduplicate the output with 5 more bytes:
% This will only cycle through the unique elements of 'M' (85 bytes):
function f(M,s);for k=unique(M)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
MATLAB - 80 bytes
(Corrected and) Compacted:
function f(M,s);for k=M(:)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
And in a fully developped version:
function getthesum(M,s)
for k=M(:)' % For each element of M
x = M==k ; % Index elements equal to "k"
N = M( ~sum(x,2) , ~sum(x) ) ; % New matrix with only the appropriate rows/columns
if sum(sum(N))==s % sum rows and columns and compare to "s"
k % display "k" in console if "k" is valid
end
end
Thanks to the comments to highlight my initial mistake. Note that this version does not de-duplicate the output.
It is possible to deduplicate the output with 5 more bytes:
% This will only cycle through the unique elements of 'M' (85 bytes):
function f(M,s);for k=unique(M)';if sum(sum(M(~sum(M==k,2),~sum(M==k))))==s;k,end;end
edited Sep 17 at 17:03
answered Sep 5 at 18:22
Hoki
25127
25127
1
k could be any element of the matrix.
â Dennisâ¦
Sep 5 at 19:12
@Dennis, oops that's right ... My bad, i'll correct it later today. Thanks for pointing it out.
â Hoki
Sep 6 at 11:04
1
@Arnauld. Sorry I was on holidays, that it fixed now.
â Hoki
Sep 17 at 17:05
add a comment |Â
1
k could be any element of the matrix.
â Dennisâ¦
Sep 5 at 19:12
@Dennis, oops that's right ... My bad, i'll correct it later today. Thanks for pointing it out.
â Hoki
Sep 6 at 11:04
1
@Arnauld. Sorry I was on holidays, that it fixed now.
â Hoki
Sep 17 at 17:05
1
1
k could be any element of the matrix.
â Dennisâ¦
Sep 5 at 19:12
k could be any element of the matrix.
â Dennisâ¦
Sep 5 at 19:12
@Dennis, oops that's right ... My bad, i'll correct it later today. Thanks for pointing it out.
â Hoki
Sep 6 at 11:04
@Dennis, oops that's right ... My bad, i'll correct it later today. Thanks for pointing it out.
â Hoki
Sep 6 at 11:04
1
1
@Arnauld. Sorry I was on holidays, that it fixed now.
â Hoki
Sep 17 at 17:05
@Arnauld. Sorry I was on holidays, that it fixed now.
â Hoki
Sep 17 at 17:05
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f171747%2fchop-off-the-matrix-to-get-the-desired-sum%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Would retaining the original structure of the input array (e.g.,
[[1,5],[1],[5],]for the first test case) be a valid means of output?â Shaggy
Sep 5 at 16:28
@Shaggy Yes. That looks reasonable.
â Arnauld
Sep 5 at 16:33