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
edited Sep 6 at 6:46
asked Sep 5 at 9:08


Arnauld
63.7k580268
63.7k580268
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 intoM
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
âµ[â¸âº=+/¨(,âµ×∧/∘.∧∧⌿)¨âµ≠⊂âµ]
– 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
]
]
New contributor
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 OR
acts like abinary OR
when numbers other than0
/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
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
0
down vote
MATLAB - 78 bytes
Compacted:
function f(M,s);for k=1:9;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=1:9 % For each single digit
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
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
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,13812256
6,13812256
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 intoM
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
âµ[â¸âº=+/¨(,âµ×∧/∘.∧∧⌿)¨âµ≠⊂âµ]
– 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 intoM
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
âµ[â¸âº=+/¨(,âµ×∧/∘.∧∧⌿)¨âµ≠⊂âµ]
– 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 intoM
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
âµ[â¸âº=+/¨(,âµ×∧/∘.∧∧⌿)¨âµ≠⊂âµ]
– 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 intoM
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
âµ[â¸âº=+/¨(,âµ×∧/∘.∧∧⌿)¨âµ≠⊂âµ]
– 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,4061116
2,4061116
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♦
182k32291722
182k32291722
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
]
]
New contributor
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
]
]
New contributor
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
]
]
New contributor
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
]
]
New contributor
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Sep 5 at 18:42
New contributor
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Sep 5 at 10:49
Delfad0r
914
914
New contributor
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Delfad0r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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.3k758193
30.3k758193
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.3k2833
11.3k2833
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,503714
3,503714
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 OR
acts like abinary OR
when numbers other than0
/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
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 OR
acts like abinary OR
when numbers other than0
/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
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.3k758193
30.3k758193
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 OR
acts like abinary OR
when numbers other than0
/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
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 OR
acts like abinary OR
when numbers other than0
/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
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
29.6k551164
29.6k551164
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.1k744170
75.1k744170
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,2131931
5,2131931
add a comment |Â
add a comment |Â
up vote
0
down vote
MATLAB - 78 bytes
Compacted:
function f(M,s);for k=1:9;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=1:9 % For each single digit
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
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
add a comment |Â
up vote
0
down vote
MATLAB - 78 bytes
Compacted:
function f(M,s);for k=1:9;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=1:9 % For each single digit
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
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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
MATLAB - 78 bytes
Compacted:
function f(M,s);for k=1:9;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=1:9 % For each single digit
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
MATLAB - 78 bytes
Compacted:
function f(M,s);for k=1:9;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=1:9 % For each single digit
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
answered Sep 5 at 18:22


Hoki
24127
24127
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
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
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
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