Missing numbers in arithmetic sum
Clash Royale CLAN TAG#URR8PPP
up vote
13
down vote
favorite
Challenge
Giving a valid arithmetic sum with some missing numbers, output the full expression.
Example:
1#3 123
+ 45# => + 456
-------- --------
579 579
Input
- Expression format can be an array
["1#3", "45#", "579"]
, a string"1#3+45#=579"
, or 3 inputsf("1#3","45#","579")
Output
- Same as input
- You don't need to output the result
Notes
- The missing numbers are going to be represented using
#
or any other constant non-numeric character you want - Assume result wont have a missing number
- Assume Input/Output consist in 2 terms and a final result
- Assume both term > 0 and result >= 2
- There might be multiple solutions. You can output anyone as long as the sum result match
Test Cases with possibly outputs (pretty format)
#79 879
+ 44# => + 444
-------- --------
1323 1323
5#5 555
+ 3#3 => + 343
-------- --------
898 898
# 1
+ # => + 1
-------- --------
2 2
### 998
+ ### => + 1 PD: there are a lot of possible outputs for this one
-------- --------
999 999
123 123
+ # => + 1
-------- --------
124 124
9 9
+ #6 => + 46
-------- --------
55 55
#123651 1123651
+ #98# => + 7981
------------ -----------
1131632 1131632
Standard code-golf rules apply
code-golf arithmetic integer
 |Â
show 3 more comments
up vote
13
down vote
favorite
Challenge
Giving a valid arithmetic sum with some missing numbers, output the full expression.
Example:
1#3 123
+ 45# => + 456
-------- --------
579 579
Input
- Expression format can be an array
["1#3", "45#", "579"]
, a string"1#3+45#=579"
, or 3 inputsf("1#3","45#","579")
Output
- Same as input
- You don't need to output the result
Notes
- The missing numbers are going to be represented using
#
or any other constant non-numeric character you want - Assume result wont have a missing number
- Assume Input/Output consist in 2 terms and a final result
- Assume both term > 0 and result >= 2
- There might be multiple solutions. You can output anyone as long as the sum result match
Test Cases with possibly outputs (pretty format)
#79 879
+ 44# => + 444
-------- --------
1323 1323
5#5 555
+ 3#3 => + 343
-------- --------
898 898
# 1
+ # => + 1
-------- --------
2 2
### 998
+ ### => + 1 PD: there are a lot of possible outputs for this one
-------- --------
999 999
123 123
+ # => + 1
-------- --------
124 124
9 9
+ #6 => + 46
-------- --------
55 55
#123651 1123651
+ #98# => + 7981
------------ -----------
1131632 1131632
Standard code-golf rules apply
code-golf arithmetic integer
Do we need to strip leading zeros?
– Mnemonic
Aug 30 at 14:55
@Mnemonic not necessarily
– Luis felipe De jesus Munoz
Aug 30 at 14:58
can I take the input with the sides around=
swapped? e.g.579=1#3+45#
– dzaima
Aug 30 at 15:25
1
"Assume both term > 0" does "assume" mean that I have to output both terms > 0 or that I can assume that there's always a solution with both > 0 but output whatever?
– dzaima
Aug 30 at 15:48
1
also your added test-case avoids exactly what I was asking for - the leading zeroes
– dzaima
Aug 30 at 15:48
 |Â
show 3 more comments
up vote
13
down vote
favorite
up vote
13
down vote
favorite
Challenge
Giving a valid arithmetic sum with some missing numbers, output the full expression.
Example:
1#3 123
+ 45# => + 456
-------- --------
579 579
Input
- Expression format can be an array
["1#3", "45#", "579"]
, a string"1#3+45#=579"
, or 3 inputsf("1#3","45#","579")
Output
- Same as input
- You don't need to output the result
Notes
- The missing numbers are going to be represented using
#
or any other constant non-numeric character you want - Assume result wont have a missing number
- Assume Input/Output consist in 2 terms and a final result
- Assume both term > 0 and result >= 2
- There might be multiple solutions. You can output anyone as long as the sum result match
Test Cases with possibly outputs (pretty format)
#79 879
+ 44# => + 444
-------- --------
1323 1323
5#5 555
+ 3#3 => + 343
-------- --------
898 898
# 1
+ # => + 1
-------- --------
2 2
### 998
+ ### => + 1 PD: there are a lot of possible outputs for this one
-------- --------
999 999
123 123
+ # => + 1
-------- --------
124 124
9 9
+ #6 => + 46
-------- --------
55 55
#123651 1123651
+ #98# => + 7981
------------ -----------
1131632 1131632
Standard code-golf rules apply
code-golf arithmetic integer
Challenge
Giving a valid arithmetic sum with some missing numbers, output the full expression.
Example:
1#3 123
+ 45# => + 456
-------- --------
579 579
Input
- Expression format can be an array
["1#3", "45#", "579"]
, a string"1#3+45#=579"
, or 3 inputsf("1#3","45#","579")
Output
- Same as input
- You don't need to output the result
Notes
- The missing numbers are going to be represented using
#
or any other constant non-numeric character you want - Assume result wont have a missing number
- Assume Input/Output consist in 2 terms and a final result
- Assume both term > 0 and result >= 2
- There might be multiple solutions. You can output anyone as long as the sum result match
Test Cases with possibly outputs (pretty format)
#79 879
+ 44# => + 444
-------- --------
1323 1323
5#5 555
+ 3#3 => + 343
-------- --------
898 898
# 1
+ # => + 1
-------- --------
2 2
### 998
+ ### => + 1 PD: there are a lot of possible outputs for this one
-------- --------
999 999
123 123
+ # => + 1
-------- --------
124 124
9 9
+ #6 => + 46
-------- --------
55 55
#123651 1123651
+ #98# => + 7981
------------ -----------
1131632 1131632
Standard code-golf rules apply
code-golf arithmetic integer
edited Aug 30 at 15:44
asked Aug 30 at 14:36


Luis felipe De jesus Munoz
2,9511044
2,9511044
Do we need to strip leading zeros?
– Mnemonic
Aug 30 at 14:55
@Mnemonic not necessarily
– Luis felipe De jesus Munoz
Aug 30 at 14:58
can I take the input with the sides around=
swapped? e.g.579=1#3+45#
– dzaima
Aug 30 at 15:25
1
"Assume both term > 0" does "assume" mean that I have to output both terms > 0 or that I can assume that there's always a solution with both > 0 but output whatever?
– dzaima
Aug 30 at 15:48
1
also your added test-case avoids exactly what I was asking for - the leading zeroes
– dzaima
Aug 30 at 15:48
 |Â
show 3 more comments
Do we need to strip leading zeros?
– Mnemonic
Aug 30 at 14:55
@Mnemonic not necessarily
– Luis felipe De jesus Munoz
Aug 30 at 14:58
can I take the input with the sides around=
swapped? e.g.579=1#3+45#
– dzaima
Aug 30 at 15:25
1
"Assume both term > 0" does "assume" mean that I have to output both terms > 0 or that I can assume that there's always a solution with both > 0 but output whatever?
– dzaima
Aug 30 at 15:48
1
also your added test-case avoids exactly what I was asking for - the leading zeroes
– dzaima
Aug 30 at 15:48
Do we need to strip leading zeros?
– Mnemonic
Aug 30 at 14:55
Do we need to strip leading zeros?
– Mnemonic
Aug 30 at 14:55
@Mnemonic not necessarily
– Luis felipe De jesus Munoz
Aug 30 at 14:58
@Mnemonic not necessarily
– Luis felipe De jesus Munoz
Aug 30 at 14:58
can I take the input with the sides around
=
swapped? e.g. 579=1#3+45#
– dzaima
Aug 30 at 15:25
can I take the input with the sides around
=
swapped? e.g. 579=1#3+45#
– dzaima
Aug 30 at 15:25
1
1
"Assume both term > 0" does "assume" mean that I have to output both terms > 0 or that I can assume that there's always a solution with both > 0 but output whatever?
– dzaima
Aug 30 at 15:48
"Assume both term > 0" does "assume" mean that I have to output both terms > 0 or that I can assume that there's always a solution with both > 0 but output whatever?
– dzaima
Aug 30 at 15:48
1
1
also your added test-case avoids exactly what I was asking for - the leading zeroes
– dzaima
Aug 30 at 15:48
also your added test-case avoids exactly what I was asking for - the leading zeroes
– dzaima
Aug 30 at 15:48
 |Â
show 3 more comments
12 Answers
12
active
oldest
votes
up vote
9
down vote
Brachylog, 22 16 bytes
áµÂ²ịáµÂ.k+~t
Try it online!
-6 bytes thanks to @Fatelize
Explanation
áµÂ²ịáµÂ.k+~t
áµÂ² # for each letter in each string
Ṣ∧Ị∋ # if " " return a digit; else input
| #
ịᵠ# cast each string to number
k+ # the sum of all but the last one
~t # is equal to the last one
. # output that list
1
áµÂ²á»‹áµÂ.k+~t
is 4 bytes shorter. I'm not sure why you did something this convoluted in your map.
– Fatalize
Aug 31 at 6:57
Since we can use any non-numeric character, you should use e.g. space withá¹¢
instead of"#"
which will save two more bytes.
– Fatalize
Aug 31 at 6:59
add a comment |Â
up vote
8
down vote
JavaScript (ES6), 74 57 bytes
Takes input as (a)(b)(result)
, where a and b are strings with .
for unknown digits and result is an integer. Returns an array of 2 integers.
a=>b=>F=(c,n)=>`$r=[c,n]`.match(`^`+[a,b])?r:F(c-1,-~n)
Try it online!
Commented
a => b => // a, b = term patterns (e.g. a = ".79", b = "44.")
F = (c, // c = expected result (e.g. 1323)
n) => // n = guessed value of b, initially undefined
`$r = [c, n]` // we coerce r = [c, n] to a string (e.g. "879,444")
// if n is still undefined, this gives just c followed by a comma
.match(`^` + [a, b]) // we coerce [a, b] to a string, prefixed with "^" (e.g. "^.79,44.")
? // this is implicitly turned into a regular expression; if matching:
r // return r
: // else:
F(c - 1, -~n) // decrement c, increment n and do a recursive call
Ah, so that's what's happening. I tried to understand your code without explanation yesterday (and I'm bad at JS), but didn't understand why-~n
couldn't be justn+1
and howF=(c,n)=>
was used. Now that you added an explanation it all makes sense.c
is the third input,n
is undefined (and~undefined
becomes-1
unlikeundefined+1
). All clear now (and not something I can port to Java unfortunately, which was mainly why I tried to understand it xD). PS: Already upvoted yesterday, so I just upvoted one of your other answers (which I didn't upvote already, not a lot available..) ;p
– Kevin Cruijssen
Aug 31 at 15:59
@KevinCruijssen FWIW, I wrote a tip about this some times ago. But yeah ... that's a JS thing and is probably not portable to many other languages.
– Arnauld
Aug 31 at 16:08
Well, I might be able to semi-port it but just creating a recursive second method, and using a ternary-if to check fornull
, manually converting it to-1
. However, Java has a (very) limited recursive StackOverflow limit, so using a recursive method with randomness hoping it would end up correct within about 1024 recursive calls isn't going to work anyway in Java. Ah well. I've upvoted your tip. Have a nice weekend! :)
– Kevin Cruijssen
Aug 31 at 16:38
@KevinCruijssen My first JS attempt was doing exactly that: trying random values with a recursive function. And it was usually doing significantly less iterations than the one using a counter. Fun fact: even for###+###=999
, your odds are 1 in 1000. So with 1024 iterations, you should succeed slightly more often than you fail. :)
– Arnauld
Aug 31 at 17:05
add a comment |Â
up vote
7
down vote
Matlab, 143 134 132 119 115 bytes
function[m]=f(x,y,r),p=@(v)str2num(strrep(v,'#',char(randi([48,57]))));m=[1,1];while sum(m)-r,m=[p(x),p(y)];end;end
-4 bytes thanks to @Luismendo
Try it Online
Pretty big and pretty stupid. It simply replaces all #
with random digits until it finds the correct ones.
add a comment |Â
up vote
4
down vote
R, 67 51 bytes
Rock simple and scales horribly, just grep all the sum combinations. Use "." for unknown digits. It won't find the same answer as test case number 4, but it will give a possible answer, which follows the letter of the rules as given.
-16 bytes by grepping after forming the output and replacing paste
with the ?
operator.
function(x,y,z,`?`=paste)grep(x?y,1:z?z:1-1,v=T)[1]
Try it online!
1
Awesome idea, I would never have thought of that. You can save a few bytes by using * in place of grepl : tio.run/##PYzLCoMwEEX3/…
– JayCe
Aug 31 at 0:44
1
I was looking for various operators and you come up with?
... I think this is a first. by the way, I forgot if I already told you but we're trying to get R nominated for september language of the month - you can upvote if not done already.
– JayCe
Aug 31 at 14:26
I could've chosen anything with low precedence. Feels like there should be a better way to get the match...
– J.Doe
Aug 31 at 14:31
add a comment |Â
up vote
3
down vote
Charcoal, 32 bytes
F²⊞ÃÂ…0ï¼·â»ζΣIÅ≔E⟦θη⟧â†κ⎇â¼μ#‽ÇμÅâ†ÂEÅ⮌ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞ÃÂ…0
Push two string 0
s to the predefined empty list u
to get the while loop going.
ï¼·â»ζΣIÃÂ…
Repeat while the sum of casting the values in u
to integer is not equal to the desired result.
≔E⟦θη⟧
Create an array of the two inputs and map over it.
â†κ⎇â¼μ#‽ÇμÃÂ…
Replace each #
with a random digit and assign the result back to u
.
â†ÂEÅ⮌ι
Print the result right justified. (Left justified would be just ÃÂ…
for a 4-byte saving.)
add a comment |Â
up vote
3
down vote
Jelly, 20 bytes
ØDṛċ¡V€)Ã…Â’pḌð€ŒpSâ¼¥ƇḢ
Try it online!
add a comment |Â
up vote
3
down vote
05AB1E (legacy), 23 20 bytes
[²³«εð9ÃÂΩ:}²gôJDO¹Q#
-3 bytes thanks to @Emigna.
Unknown digits are spaces (
). Input order should be: expected result; longest string; shortest string.
Try it online.
Explanation:
[ # Start an infinite loop
²³« # Take the second and third inputs, and merge them together
# i.e. " 79" and " 4 " → " 79 4 "
ε } # Map each character to:
ð : # Replace a space with:
9ÃÂΩ # A random digit in the range [0,9]
# i.e. " 79 4 " → ['3','7','9','2','4','3']
# i.e. " 79 4 " → ['5','7','9','7','4','4']
²g # Get the length of the second input
# i.e. " 79" → 3
ô # And split it into two numbers again
# i.e. ['3','7','9','2','4','3'] and 3 → [['3','7','9'],['2','4','3']]
# i.e. ['5','7','9','7','4','4'] and 3 → [['5','7','9'],['7','4','4']]
J # Join each list together to a single number
# i.e. [['3','7','9'],['2','4','3']] → [379,243]
# i.e. [['5','7','9'],['7','4','4']] → [579,744]
D # Duplicate this list
O # Sum the list
# i.e. [379,243] → 622
# i.e. [579,744] → 1323
¹Q# # If it's equal to the first input: stop the infinite loop
# (and output the duplicate list implicitly)
# i.e. 1323 and 622 → 0 (falsey) → continue the loop
# i.e. 1323 and 1323 → 1 (truthy) → stop the loop and output [579,744]
1
Replace saves 3 over the if.
– Emigna
Aug 31 at 6:10
@Emigna Ah, of course. Thanks!
– Kevin Cruijssen
Aug 31 at 6:52
add a comment |Â
up vote
3
down vote
Perl 6, 81 74 bytes
-7 bytes thanks to nwellnhof!
first try S/=/==/.EVAL,map $^a;S:g[#]=$a[$++],[X] ^10 xx.comb('#')
Try it online!
Anonymous code block that takes input as a string containing an arithmetical expression e.g. "12#+45#=579". Substitutes each #
with possible permutations of digits, substitutes the=
with ==
and finds the first valid result.
Explanation:
# Anonymous code block
first # Find the first of:
^10 # The range of 0 to 9
xx.comb('#') # Multiplied by the number #s in the code
,[X] # The cross-product of these lists
map # Map each crossproduct to:
$^a;.trans: "#"=>$a[$++] # The given string with each # translated to each element in the list
try S/=/==/.EVAL, # Find which of these is true when = are changed to == and it is eval'd
You can useS:g[#]=$a[$++]
instead oftrans
for 74 bytes.
– nwellnhof
Sep 1 at 13:05
@nwellnhof I didn't realise you could useS///
in that sort of syntax! Thanks!
– Jo King
Sep 1 at 13:13
add a comment |Â
up vote
2
down vote
APL (Dyalog Unicode), 22 bytes
'#'⎕Râ•?10tâ£âÂŽâºt∘â†Â
Try it online!
add a comment |Â
up vote
2
down vote
Java 10, 203 197 bytes
(a,b,c)->int A=0,B=0,l=a.length();for(a+=b,b="";A+B!=c;A=c.valueOf(b.substring(0,l)),B=c.valueOf(b.substring(l)),b="")for(var t:a.toCharArray())b+=t<36?(int)(Math.random()*9)+"":t;return A+"+"+B;
Try it online.
Explanation:
(a,b,c)-> // Method with 2 Strings & integer parameters and String return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.length(); // Length of the first String-input
for(a+=b, // Concat the second String-input to the first
b=""; // Reuse `b`, and start it as an empty String
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=c.valueOf(b.substring(0,l)),
// Set `A` to the first String-part as integer
B=c.valueOf(n.substring(l)),
// Set `B` to the second String-part as integer
b="") // Reset `b` to an empty String
for(var t:a.toCharArray())
// Inner loop over the characters of the concatted String inputs
b+=t<36? // If the current character is a '#':
(int)(Math.random()*9)+""
// Append a random digit to `b`
: // Else (it already is a digit):
t; // Append this digit as is to `b`
return A+"+"+B; // After the loop, return `A` and `B` as result
add a comment |Â
up vote
1
down vote
C# .NET, 225 220 196 bytes
(a,b,c)=>int A=0,B=0,l=a.Length;for(a+=b,b="";A+B!=c;A=int.Parse(b.Substring(0,l)),B=int.Parse(b.Substring(l)),b="")foreach(var t in a)b+=(t<36?new System.Random().Next(10):t-48)+"";return(A,B);
Port of my Java 10 answer.
(I'm very rusty in C# .NET golfing, so can defintely be golfed..)
-3 bytes implicitly thanks to @user82593 and this new C# tip he added.
-29 bytes thanks to @hvd.
Try it online.
Explanation:
(a,b,c)=> // Method with 2 string & int parameters and int-tuple return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.Length; // Length of the first string-input
for(a+=b, // Concat the second string-input to the first
b=""; // Reuse `b`, and start it as an empty string
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=int.Parse(b.Substring(0,l)),
// Set `A` to the first string-part as integer
B=int.Parse(b.Substring(l)),
// Set `B` to the second string-part as integer
b="") // Reset `b` to an empty string
foreach(var t in a)
// Inner loop over the characters of the concatted string inputs
b+=(t<36? // If the current character is a '#':
new System.Random().Next(10)
// Use a random digit
: // Else (it already is a digit):
t-48) // Use this digit as is
+""; // And convert it to a string so it can be appended to the string
return(A,B); // After the loop, return `A` and `B` in a tuple as result
You can use the regularusing System;
instead, it is shorter thannamespace System
.
– hvd
Sep 1 at 8:01
@hvd That was it!.. I haven't done C# in years, lol.. I triedusing System.*;
similar as imports in Java, but that didn't work. Forgot I had to remove the.*
part.. Thanks for the -5 bytes.
– Kevin Cruijssen
Sep 1 at 11:01
1
Re-reading it now, that was actually a sub-optimal suggestion. You can writeint.Parse
(-4), usenew System.Random()
(+7) and dropusing System;
(-13) to save another 10 bytes. :) Also, you do not need.ToCharArray()
, that takes off 14 more bytes.
– hvd
Sep 1 at 11:05
@hvd Thanks! Not sure how I forgot aboutint.Parse
vsSystem.Int32.Parse
... It's basically the same asSystem.String
andstring
.. And didn't knew it was possible to loop over the characters without the.ToCharArray()
. Thanks for another -24 bytes. :D
– Kevin Cruijssen
Sep 1 at 11:49
add a comment |Â
up vote
1
down vote
Python 3, 121 155 152 149 bytes
import re
def f(i,k=0,S=re.sub):s=S('#','%s',i)%(*list('%0*d'%(i.count('#'),k)),);print(s)if eval(S('=','==',S('\b0*([1-9])','\1',s)))else f(i,k+1)
Try it online!
+34 New solution with regex to circumvent the fact that python doesn't support numbers with leading zeroes.
-3 thanks to @Jonathan Frech
The old solution doesn't work if # is the first character in any number (because eval doesn't accept leading zeroes) and is therefore invalid :(
def f(i,k=0):
s=i.replace('#','%s')%(*list('%0*d'%(i.count('#'),k)),)
print(s)if eval(s.replace('=','=='))else f(i,k+1)
Try it online!
1
I'm afraid this submission is invalid for the reason stated in the post.
– Erik the Outgolfer
Aug 30 at 17:10
2
Since your function does not contain any compound statements you can condense it to only one line.
– Jonathan Frech
Aug 30 at 23:21
add a comment |Â
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
Brachylog, 22 16 bytes
áµÂ²ịáµÂ.k+~t
Try it online!
-6 bytes thanks to @Fatelize
Explanation
áµÂ²ịáµÂ.k+~t
áµÂ² # for each letter in each string
Ṣ∧Ị∋ # if " " return a digit; else input
| #
ịᵠ# cast each string to number
k+ # the sum of all but the last one
~t # is equal to the last one
. # output that list
1
áµÂ²á»‹áµÂ.k+~t
is 4 bytes shorter. I'm not sure why you did something this convoluted in your map.
– Fatalize
Aug 31 at 6:57
Since we can use any non-numeric character, you should use e.g. space withá¹¢
instead of"#"
which will save two more bytes.
– Fatalize
Aug 31 at 6:59
add a comment |Â
up vote
9
down vote
Brachylog, 22 16 bytes
áµÂ²ịáµÂ.k+~t
Try it online!
-6 bytes thanks to @Fatelize
Explanation
áµÂ²ịáµÂ.k+~t
áµÂ² # for each letter in each string
Ṣ∧Ị∋ # if " " return a digit; else input
| #
ịᵠ# cast each string to number
k+ # the sum of all but the last one
~t # is equal to the last one
. # output that list
1
áµÂ²á»‹áµÂ.k+~t
is 4 bytes shorter. I'm not sure why you did something this convoluted in your map.
– Fatalize
Aug 31 at 6:57
Since we can use any non-numeric character, you should use e.g. space withá¹¢
instead of"#"
which will save two more bytes.
– Fatalize
Aug 31 at 6:59
add a comment |Â
up vote
9
down vote
up vote
9
down vote
Brachylog, 22 16 bytes
áµÂ²ịáµÂ.k+~t
Try it online!
-6 bytes thanks to @Fatelize
Explanation
áµÂ²ịáµÂ.k+~t
áµÂ² # for each letter in each string
Ṣ∧Ị∋ # if " " return a digit; else input
| #
ịᵠ# cast each string to number
k+ # the sum of all but the last one
~t # is equal to the last one
. # output that list
Brachylog, 22 16 bytes
áµÂ²ịáµÂ.k+~t
Try it online!
-6 bytes thanks to @Fatelize
Explanation
áµÂ²ịáµÂ.k+~t
áµÂ² # for each letter in each string
Ṣ∧Ị∋ # if " " return a digit; else input
| #
ịᵠ# cast each string to number
k+ # the sum of all but the last one
~t # is equal to the last one
. # output that list
edited Aug 31 at 8:07
answered Aug 30 at 23:08


Kroppeb
88628
88628
1
áµÂ²á»‹áµÂ.k+~t
is 4 bytes shorter. I'm not sure why you did something this convoluted in your map.
– Fatalize
Aug 31 at 6:57
Since we can use any non-numeric character, you should use e.g. space withá¹¢
instead of"#"
which will save two more bytes.
– Fatalize
Aug 31 at 6:59
add a comment |Â
1
áµÂ²á»‹áµÂ.k+~t
is 4 bytes shorter. I'm not sure why you did something this convoluted in your map.
– Fatalize
Aug 31 at 6:57
Since we can use any non-numeric character, you should use e.g. space withá¹¢
instead of"#"
which will save two more bytes.
– Fatalize
Aug 31 at 6:59
1
1
áµÂ²á»‹áµÂ.k+~t
is 4 bytes shorter. I'm not sure why you did something this convoluted in your map.– Fatalize
Aug 31 at 6:57
áµÂ²á»‹áµÂ.k+~t
is 4 bytes shorter. I'm not sure why you did something this convoluted in your map.– Fatalize
Aug 31 at 6:57
Since we can use any non-numeric character, you should use e.g. space with
á¹¢
instead of "#"
which will save two more bytes.– Fatalize
Aug 31 at 6:59
Since we can use any non-numeric character, you should use e.g. space with
á¹¢
instead of "#"
which will save two more bytes.– Fatalize
Aug 31 at 6:59
add a comment |Â
up vote
8
down vote
JavaScript (ES6), 74 57 bytes
Takes input as (a)(b)(result)
, where a and b are strings with .
for unknown digits and result is an integer. Returns an array of 2 integers.
a=>b=>F=(c,n)=>`$r=[c,n]`.match(`^`+[a,b])?r:F(c-1,-~n)
Try it online!
Commented
a => b => // a, b = term patterns (e.g. a = ".79", b = "44.")
F = (c, // c = expected result (e.g. 1323)
n) => // n = guessed value of b, initially undefined
`$r = [c, n]` // we coerce r = [c, n] to a string (e.g. "879,444")
// if n is still undefined, this gives just c followed by a comma
.match(`^` + [a, b]) // we coerce [a, b] to a string, prefixed with "^" (e.g. "^.79,44.")
? // this is implicitly turned into a regular expression; if matching:
r // return r
: // else:
F(c - 1, -~n) // decrement c, increment n and do a recursive call
Ah, so that's what's happening. I tried to understand your code without explanation yesterday (and I'm bad at JS), but didn't understand why-~n
couldn't be justn+1
and howF=(c,n)=>
was used. Now that you added an explanation it all makes sense.c
is the third input,n
is undefined (and~undefined
becomes-1
unlikeundefined+1
). All clear now (and not something I can port to Java unfortunately, which was mainly why I tried to understand it xD). PS: Already upvoted yesterday, so I just upvoted one of your other answers (which I didn't upvote already, not a lot available..) ;p
– Kevin Cruijssen
Aug 31 at 15:59
@KevinCruijssen FWIW, I wrote a tip about this some times ago. But yeah ... that's a JS thing and is probably not portable to many other languages.
– Arnauld
Aug 31 at 16:08
Well, I might be able to semi-port it but just creating a recursive second method, and using a ternary-if to check fornull
, manually converting it to-1
. However, Java has a (very) limited recursive StackOverflow limit, so using a recursive method with randomness hoping it would end up correct within about 1024 recursive calls isn't going to work anyway in Java. Ah well. I've upvoted your tip. Have a nice weekend! :)
– Kevin Cruijssen
Aug 31 at 16:38
@KevinCruijssen My first JS attempt was doing exactly that: trying random values with a recursive function. And it was usually doing significantly less iterations than the one using a counter. Fun fact: even for###+###=999
, your odds are 1 in 1000. So with 1024 iterations, you should succeed slightly more often than you fail. :)
– Arnauld
Aug 31 at 17:05
add a comment |Â
up vote
8
down vote
JavaScript (ES6), 74 57 bytes
Takes input as (a)(b)(result)
, where a and b are strings with .
for unknown digits and result is an integer. Returns an array of 2 integers.
a=>b=>F=(c,n)=>`$r=[c,n]`.match(`^`+[a,b])?r:F(c-1,-~n)
Try it online!
Commented
a => b => // a, b = term patterns (e.g. a = ".79", b = "44.")
F = (c, // c = expected result (e.g. 1323)
n) => // n = guessed value of b, initially undefined
`$r = [c, n]` // we coerce r = [c, n] to a string (e.g. "879,444")
// if n is still undefined, this gives just c followed by a comma
.match(`^` + [a, b]) // we coerce [a, b] to a string, prefixed with "^" (e.g. "^.79,44.")
? // this is implicitly turned into a regular expression; if matching:
r // return r
: // else:
F(c - 1, -~n) // decrement c, increment n and do a recursive call
Ah, so that's what's happening. I tried to understand your code without explanation yesterday (and I'm bad at JS), but didn't understand why-~n
couldn't be justn+1
and howF=(c,n)=>
was used. Now that you added an explanation it all makes sense.c
is the third input,n
is undefined (and~undefined
becomes-1
unlikeundefined+1
). All clear now (and not something I can port to Java unfortunately, which was mainly why I tried to understand it xD). PS: Already upvoted yesterday, so I just upvoted one of your other answers (which I didn't upvote already, not a lot available..) ;p
– Kevin Cruijssen
Aug 31 at 15:59
@KevinCruijssen FWIW, I wrote a tip about this some times ago. But yeah ... that's a JS thing and is probably not portable to many other languages.
– Arnauld
Aug 31 at 16:08
Well, I might be able to semi-port it but just creating a recursive second method, and using a ternary-if to check fornull
, manually converting it to-1
. However, Java has a (very) limited recursive StackOverflow limit, so using a recursive method with randomness hoping it would end up correct within about 1024 recursive calls isn't going to work anyway in Java. Ah well. I've upvoted your tip. Have a nice weekend! :)
– Kevin Cruijssen
Aug 31 at 16:38
@KevinCruijssen My first JS attempt was doing exactly that: trying random values with a recursive function. And it was usually doing significantly less iterations than the one using a counter. Fun fact: even for###+###=999
, your odds are 1 in 1000. So with 1024 iterations, you should succeed slightly more often than you fail. :)
– Arnauld
Aug 31 at 17:05
add a comment |Â
up vote
8
down vote
up vote
8
down vote
JavaScript (ES6), 74 57 bytes
Takes input as (a)(b)(result)
, where a and b are strings with .
for unknown digits and result is an integer. Returns an array of 2 integers.
a=>b=>F=(c,n)=>`$r=[c,n]`.match(`^`+[a,b])?r:F(c-1,-~n)
Try it online!
Commented
a => b => // a, b = term patterns (e.g. a = ".79", b = "44.")
F = (c, // c = expected result (e.g. 1323)
n) => // n = guessed value of b, initially undefined
`$r = [c, n]` // we coerce r = [c, n] to a string (e.g. "879,444")
// if n is still undefined, this gives just c followed by a comma
.match(`^` + [a, b]) // we coerce [a, b] to a string, prefixed with "^" (e.g. "^.79,44.")
? // this is implicitly turned into a regular expression; if matching:
r // return r
: // else:
F(c - 1, -~n) // decrement c, increment n and do a recursive call
JavaScript (ES6), 74 57 bytes
Takes input as (a)(b)(result)
, where a and b are strings with .
for unknown digits and result is an integer. Returns an array of 2 integers.
a=>b=>F=(c,n)=>`$r=[c,n]`.match(`^`+[a,b])?r:F(c-1,-~n)
Try it online!
Commented
a => b => // a, b = term patterns (e.g. a = ".79", b = "44.")
F = (c, // c = expected result (e.g. 1323)
n) => // n = guessed value of b, initially undefined
`$r = [c, n]` // we coerce r = [c, n] to a string (e.g. "879,444")
// if n is still undefined, this gives just c followed by a comma
.match(`^` + [a, b]) // we coerce [a, b] to a string, prefixed with "^" (e.g. "^.79,44.")
? // this is implicitly turned into a regular expression; if matching:
r // return r
: // else:
F(c - 1, -~n) // decrement c, increment n and do a recursive call
edited Aug 30 at 19:19
answered Aug 30 at 15:23


Arnauld
63.6k580268
63.6k580268
Ah, so that's what's happening. I tried to understand your code without explanation yesterday (and I'm bad at JS), but didn't understand why-~n
couldn't be justn+1
and howF=(c,n)=>
was used. Now that you added an explanation it all makes sense.c
is the third input,n
is undefined (and~undefined
becomes-1
unlikeundefined+1
). All clear now (and not something I can port to Java unfortunately, which was mainly why I tried to understand it xD). PS: Already upvoted yesterday, so I just upvoted one of your other answers (which I didn't upvote already, not a lot available..) ;p
– Kevin Cruijssen
Aug 31 at 15:59
@KevinCruijssen FWIW, I wrote a tip about this some times ago. But yeah ... that's a JS thing and is probably not portable to many other languages.
– Arnauld
Aug 31 at 16:08
Well, I might be able to semi-port it but just creating a recursive second method, and using a ternary-if to check fornull
, manually converting it to-1
. However, Java has a (very) limited recursive StackOverflow limit, so using a recursive method with randomness hoping it would end up correct within about 1024 recursive calls isn't going to work anyway in Java. Ah well. I've upvoted your tip. Have a nice weekend! :)
– Kevin Cruijssen
Aug 31 at 16:38
@KevinCruijssen My first JS attempt was doing exactly that: trying random values with a recursive function. And it was usually doing significantly less iterations than the one using a counter. Fun fact: even for###+###=999
, your odds are 1 in 1000. So with 1024 iterations, you should succeed slightly more often than you fail. :)
– Arnauld
Aug 31 at 17:05
add a comment |Â
Ah, so that's what's happening. I tried to understand your code without explanation yesterday (and I'm bad at JS), but didn't understand why-~n
couldn't be justn+1
and howF=(c,n)=>
was used. Now that you added an explanation it all makes sense.c
is the third input,n
is undefined (and~undefined
becomes-1
unlikeundefined+1
). All clear now (and not something I can port to Java unfortunately, which was mainly why I tried to understand it xD). PS: Already upvoted yesterday, so I just upvoted one of your other answers (which I didn't upvote already, not a lot available..) ;p
– Kevin Cruijssen
Aug 31 at 15:59
@KevinCruijssen FWIW, I wrote a tip about this some times ago. But yeah ... that's a JS thing and is probably not portable to many other languages.
– Arnauld
Aug 31 at 16:08
Well, I might be able to semi-port it but just creating a recursive second method, and using a ternary-if to check fornull
, manually converting it to-1
. However, Java has a (very) limited recursive StackOverflow limit, so using a recursive method with randomness hoping it would end up correct within about 1024 recursive calls isn't going to work anyway in Java. Ah well. I've upvoted your tip. Have a nice weekend! :)
– Kevin Cruijssen
Aug 31 at 16:38
@KevinCruijssen My first JS attempt was doing exactly that: trying random values with a recursive function. And it was usually doing significantly less iterations than the one using a counter. Fun fact: even for###+###=999
, your odds are 1 in 1000. So with 1024 iterations, you should succeed slightly more often than you fail. :)
– Arnauld
Aug 31 at 17:05
Ah, so that's what's happening. I tried to understand your code without explanation yesterday (and I'm bad at JS), but didn't understand why
-~n
couldn't be just n+1
and how F=(c,n)=>
was used. Now that you added an explanation it all makes sense. c
is the third input, n
is undefined (and ~undefined
becomes -1
unlike undefined+1
). All clear now (and not something I can port to Java unfortunately, which was mainly why I tried to understand it xD). PS: Already upvoted yesterday, so I just upvoted one of your other answers (which I didn't upvote already, not a lot available..) ;p– Kevin Cruijssen
Aug 31 at 15:59
Ah, so that's what's happening. I tried to understand your code without explanation yesterday (and I'm bad at JS), but didn't understand why
-~n
couldn't be just n+1
and how F=(c,n)=>
was used. Now that you added an explanation it all makes sense. c
is the third input, n
is undefined (and ~undefined
becomes -1
unlike undefined+1
). All clear now (and not something I can port to Java unfortunately, which was mainly why I tried to understand it xD). PS: Already upvoted yesterday, so I just upvoted one of your other answers (which I didn't upvote already, not a lot available..) ;p– Kevin Cruijssen
Aug 31 at 15:59
@KevinCruijssen FWIW, I wrote a tip about this some times ago. But yeah ... that's a JS thing and is probably not portable to many other languages.
– Arnauld
Aug 31 at 16:08
@KevinCruijssen FWIW, I wrote a tip about this some times ago. But yeah ... that's a JS thing and is probably not portable to many other languages.
– Arnauld
Aug 31 at 16:08
Well, I might be able to semi-port it but just creating a recursive second method, and using a ternary-if to check for
null
, manually converting it to -1
. However, Java has a (very) limited recursive StackOverflow limit, so using a recursive method with randomness hoping it would end up correct within about 1024 recursive calls isn't going to work anyway in Java. Ah well. I've upvoted your tip. Have a nice weekend! :)– Kevin Cruijssen
Aug 31 at 16:38
Well, I might be able to semi-port it but just creating a recursive second method, and using a ternary-if to check for
null
, manually converting it to -1
. However, Java has a (very) limited recursive StackOverflow limit, so using a recursive method with randomness hoping it would end up correct within about 1024 recursive calls isn't going to work anyway in Java. Ah well. I've upvoted your tip. Have a nice weekend! :)– Kevin Cruijssen
Aug 31 at 16:38
@KevinCruijssen My first JS attempt was doing exactly that: trying random values with a recursive function. And it was usually doing significantly less iterations than the one using a counter. Fun fact: even for
###+###=999
, your odds are 1 in 1000. So with 1024 iterations, you should succeed slightly more often than you fail. :)– Arnauld
Aug 31 at 17:05
@KevinCruijssen My first JS attempt was doing exactly that: trying random values with a recursive function. And it was usually doing significantly less iterations than the one using a counter. Fun fact: even for
###+###=999
, your odds are 1 in 1000. So with 1024 iterations, you should succeed slightly more often than you fail. :)– Arnauld
Aug 31 at 17:05
add a comment |Â
up vote
7
down vote
Matlab, 143 134 132 119 115 bytes
function[m]=f(x,y,r),p=@(v)str2num(strrep(v,'#',char(randi([48,57]))));m=[1,1];while sum(m)-r,m=[p(x),p(y)];end;end
-4 bytes thanks to @Luismendo
Try it Online
Pretty big and pretty stupid. It simply replaces all #
with random digits until it finds the correct ones.
add a comment |Â
up vote
7
down vote
Matlab, 143 134 132 119 115 bytes
function[m]=f(x,y,r),p=@(v)str2num(strrep(v,'#',char(randi([48,57]))));m=[1,1];while sum(m)-r,m=[p(x),p(y)];end;end
-4 bytes thanks to @Luismendo
Try it Online
Pretty big and pretty stupid. It simply replaces all #
with random digits until it finds the correct ones.
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Matlab, 143 134 132 119 115 bytes
function[m]=f(x,y,r),p=@(v)str2num(strrep(v,'#',char(randi([48,57]))));m=[1,1];while sum(m)-r,m=[p(x),p(y)];end;end
-4 bytes thanks to @Luismendo
Try it Online
Pretty big and pretty stupid. It simply replaces all #
with random digits until it finds the correct ones.
Matlab, 143 134 132 119 115 bytes
function[m]=f(x,y,r),p=@(v)str2num(strrep(v,'#',char(randi([48,57]))));m=[1,1];while sum(m)-r,m=[p(x),p(y)];end;end
-4 bytes thanks to @Luismendo
Try it Online
Pretty big and pretty stupid. It simply replaces all #
with random digits until it finds the correct ones.
edited Aug 31 at 0:50
answered Aug 30 at 15:18


DimChtz
601111
601111
add a comment |Â
add a comment |Â
up vote
4
down vote
R, 67 51 bytes
Rock simple and scales horribly, just grep all the sum combinations. Use "." for unknown digits. It won't find the same answer as test case number 4, but it will give a possible answer, which follows the letter of the rules as given.
-16 bytes by grepping after forming the output and replacing paste
with the ?
operator.
function(x,y,z,`?`=paste)grep(x?y,1:z?z:1-1,v=T)[1]
Try it online!
1
Awesome idea, I would never have thought of that. You can save a few bytes by using * in place of grepl : tio.run/##PYzLCoMwEEX3/…
– JayCe
Aug 31 at 0:44
1
I was looking for various operators and you come up with?
... I think this is a first. by the way, I forgot if I already told you but we're trying to get R nominated for september language of the month - you can upvote if not done already.
– JayCe
Aug 31 at 14:26
I could've chosen anything with low precedence. Feels like there should be a better way to get the match...
– J.Doe
Aug 31 at 14:31
add a comment |Â
up vote
4
down vote
R, 67 51 bytes
Rock simple and scales horribly, just grep all the sum combinations. Use "." for unknown digits. It won't find the same answer as test case number 4, but it will give a possible answer, which follows the letter of the rules as given.
-16 bytes by grepping after forming the output and replacing paste
with the ?
operator.
function(x,y,z,`?`=paste)grep(x?y,1:z?z:1-1,v=T)[1]
Try it online!
1
Awesome idea, I would never have thought of that. You can save a few bytes by using * in place of grepl : tio.run/##PYzLCoMwEEX3/…
– JayCe
Aug 31 at 0:44
1
I was looking for various operators and you come up with?
... I think this is a first. by the way, I forgot if I already told you but we're trying to get R nominated for september language of the month - you can upvote if not done already.
– JayCe
Aug 31 at 14:26
I could've chosen anything with low precedence. Feels like there should be a better way to get the match...
– J.Doe
Aug 31 at 14:31
add a comment |Â
up vote
4
down vote
up vote
4
down vote
R, 67 51 bytes
Rock simple and scales horribly, just grep all the sum combinations. Use "." for unknown digits. It won't find the same answer as test case number 4, but it will give a possible answer, which follows the letter of the rules as given.
-16 bytes by grepping after forming the output and replacing paste
with the ?
operator.
function(x,y,z,`?`=paste)grep(x?y,1:z?z:1-1,v=T)[1]
Try it online!
R, 67 51 bytes
Rock simple and scales horribly, just grep all the sum combinations. Use "." for unknown digits. It won't find the same answer as test case number 4, but it will give a possible answer, which follows the letter of the rules as given.
-16 bytes by grepping after forming the output and replacing paste
with the ?
operator.
function(x,y,z,`?`=paste)grep(x?y,1:z?z:1-1,v=T)[1]
Try it online!
edited Aug 31 at 14:42
answered Aug 30 at 18:01
J.Doe
5718
5718
1
Awesome idea, I would never have thought of that. You can save a few bytes by using * in place of grepl : tio.run/##PYzLCoMwEEX3/…
– JayCe
Aug 31 at 0:44
1
I was looking for various operators and you come up with?
... I think this is a first. by the way, I forgot if I already told you but we're trying to get R nominated for september language of the month - you can upvote if not done already.
– JayCe
Aug 31 at 14:26
I could've chosen anything with low precedence. Feels like there should be a better way to get the match...
– J.Doe
Aug 31 at 14:31
add a comment |Â
1
Awesome idea, I would never have thought of that. You can save a few bytes by using * in place of grepl : tio.run/##PYzLCoMwEEX3/…
– JayCe
Aug 31 at 0:44
1
I was looking for various operators and you come up with?
... I think this is a first. by the way, I forgot if I already told you but we're trying to get R nominated for september language of the month - you can upvote if not done already.
– JayCe
Aug 31 at 14:26
I could've chosen anything with low precedence. Feels like there should be a better way to get the match...
– J.Doe
Aug 31 at 14:31
1
1
Awesome idea, I would never have thought of that. You can save a few bytes by using * in place of grepl : tio.run/##PYzLCoMwEEX3/…
– JayCe
Aug 31 at 0:44
Awesome idea, I would never have thought of that. You can save a few bytes by using * in place of grepl : tio.run/##PYzLCoMwEEX3/…
– JayCe
Aug 31 at 0:44
1
1
I was looking for various operators and you come up with
?
... I think this is a first. by the way, I forgot if I already told you but we're trying to get R nominated for september language of the month - you can upvote if not done already.– JayCe
Aug 31 at 14:26
I was looking for various operators and you come up with
?
... I think this is a first. by the way, I forgot if I already told you but we're trying to get R nominated for september language of the month - you can upvote if not done already.– JayCe
Aug 31 at 14:26
I could've chosen anything with low precedence. Feels like there should be a better way to get the match...
– J.Doe
Aug 31 at 14:31
I could've chosen anything with low precedence. Feels like there should be a better way to get the match...
– J.Doe
Aug 31 at 14:31
add a comment |Â
up vote
3
down vote
Charcoal, 32 bytes
F²⊞ÃÂ…0ï¼·â»ζΣIÅ≔E⟦θη⟧â†κ⎇â¼μ#‽ÇμÅâ†ÂEÅ⮌ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞ÃÂ…0
Push two string 0
s to the predefined empty list u
to get the while loop going.
ï¼·â»ζΣIÃÂ…
Repeat while the sum of casting the values in u
to integer is not equal to the desired result.
≔E⟦θη⟧
Create an array of the two inputs and map over it.
â†κ⎇â¼μ#‽ÇμÃÂ…
Replace each #
with a random digit and assign the result back to u
.
â†ÂEÅ⮌ι
Print the result right justified. (Left justified would be just ÃÂ…
for a 4-byte saving.)
add a comment |Â
up vote
3
down vote
Charcoal, 32 bytes
F²⊞ÃÂ…0ï¼·â»ζΣIÅ≔E⟦θη⟧â†κ⎇â¼μ#‽ÇμÅâ†ÂEÅ⮌ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞ÃÂ…0
Push two string 0
s to the predefined empty list u
to get the while loop going.
ï¼·â»ζΣIÃÂ…
Repeat while the sum of casting the values in u
to integer is not equal to the desired result.
≔E⟦θη⟧
Create an array of the two inputs and map over it.
â†κ⎇â¼μ#‽ÇμÃÂ…
Replace each #
with a random digit and assign the result back to u
.
â†ÂEÅ⮌ι
Print the result right justified. (Left justified would be just ÃÂ…
for a 4-byte saving.)
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Charcoal, 32 bytes
F²⊞ÃÂ…0ï¼·â»ζΣIÅ≔E⟦θη⟧â†κ⎇â¼μ#‽ÇμÅâ†ÂEÅ⮌ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞ÃÂ…0
Push two string 0
s to the predefined empty list u
to get the while loop going.
ï¼·â»ζΣIÃÂ…
Repeat while the sum of casting the values in u
to integer is not equal to the desired result.
≔E⟦θη⟧
Create an array of the two inputs and map over it.
â†κ⎇â¼μ#‽ÇμÃÂ…
Replace each #
with a random digit and assign the result back to u
.
â†ÂEÅ⮌ι
Print the result right justified. (Left justified would be just ÃÂ…
for a 4-byte saving.)
Charcoal, 32 bytes
F²⊞ÃÂ…0ï¼·â»ζΣIÅ≔E⟦θη⟧â†κ⎇â¼μ#‽ÇμÅâ†ÂEÅ⮌ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞ÃÂ…0
Push two string 0
s to the predefined empty list u
to get the while loop going.
ï¼·â»ζΣIÃÂ…
Repeat while the sum of casting the values in u
to integer is not equal to the desired result.
≔E⟦θη⟧
Create an array of the two inputs and map over it.
â†κ⎇â¼μ#‽ÇμÃÂ…
Replace each #
with a random digit and assign the result back to u
.
â†ÂEÅ⮌ι
Print the result right justified. (Left justified would be just ÃÂ…
for a 4-byte saving.)
answered Aug 30 at 16:16
Neil
75.1k744170
75.1k744170
add a comment |Â
add a comment |Â
up vote
3
down vote
Jelly, 20 bytes
ØDṛċ¡V€)Ã…Â’pḌð€ŒpSâ¼¥ƇḢ
Try it online!
add a comment |Â
up vote
3
down vote
Jelly, 20 bytes
ØDṛċ¡V€)Ã…Â’pḌð€ŒpSâ¼¥ƇḢ
Try it online!
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Jelly, 20 bytes
ØDṛċ¡V€)Ã…Â’pḌð€ŒpSâ¼¥ƇḢ
Try it online!
Jelly, 20 bytes
ØDṛċ¡V€)Ã…Â’pḌð€ŒpSâ¼¥ƇḢ
Try it online!
answered Aug 30 at 16:30


Erik the Outgolfer
29.4k42698
29.4k42698
add a comment |Â
add a comment |Â
up vote
3
down vote
05AB1E (legacy), 23 20 bytes
[²³«εð9ÃÂΩ:}²gôJDO¹Q#
-3 bytes thanks to @Emigna.
Unknown digits are spaces (
). Input order should be: expected result; longest string; shortest string.
Try it online.
Explanation:
[ # Start an infinite loop
²³« # Take the second and third inputs, and merge them together
# i.e. " 79" and " 4 " → " 79 4 "
ε } # Map each character to:
ð : # Replace a space with:
9ÃÂΩ # A random digit in the range [0,9]
# i.e. " 79 4 " → ['3','7','9','2','4','3']
# i.e. " 79 4 " → ['5','7','9','7','4','4']
²g # Get the length of the second input
# i.e. " 79" → 3
ô # And split it into two numbers again
# i.e. ['3','7','9','2','4','3'] and 3 → [['3','7','9'],['2','4','3']]
# i.e. ['5','7','9','7','4','4'] and 3 → [['5','7','9'],['7','4','4']]
J # Join each list together to a single number
# i.e. [['3','7','9'],['2','4','3']] → [379,243]
# i.e. [['5','7','9'],['7','4','4']] → [579,744]
D # Duplicate this list
O # Sum the list
# i.e. [379,243] → 622
# i.e. [579,744] → 1323
¹Q# # If it's equal to the first input: stop the infinite loop
# (and output the duplicate list implicitly)
# i.e. 1323 and 622 → 0 (falsey) → continue the loop
# i.e. 1323 and 1323 → 1 (truthy) → stop the loop and output [579,744]
1
Replace saves 3 over the if.
– Emigna
Aug 31 at 6:10
@Emigna Ah, of course. Thanks!
– Kevin Cruijssen
Aug 31 at 6:52
add a comment |Â
up vote
3
down vote
05AB1E (legacy), 23 20 bytes
[²³«εð9ÃÂΩ:}²gôJDO¹Q#
-3 bytes thanks to @Emigna.
Unknown digits are spaces (
). Input order should be: expected result; longest string; shortest string.
Try it online.
Explanation:
[ # Start an infinite loop
²³« # Take the second and third inputs, and merge them together
# i.e. " 79" and " 4 " → " 79 4 "
ε } # Map each character to:
ð : # Replace a space with:
9ÃÂΩ # A random digit in the range [0,9]
# i.e. " 79 4 " → ['3','7','9','2','4','3']
# i.e. " 79 4 " → ['5','7','9','7','4','4']
²g # Get the length of the second input
# i.e. " 79" → 3
ô # And split it into two numbers again
# i.e. ['3','7','9','2','4','3'] and 3 → [['3','7','9'],['2','4','3']]
# i.e. ['5','7','9','7','4','4'] and 3 → [['5','7','9'],['7','4','4']]
J # Join each list together to a single number
# i.e. [['3','7','9'],['2','4','3']] → [379,243]
# i.e. [['5','7','9'],['7','4','4']] → [579,744]
D # Duplicate this list
O # Sum the list
# i.e. [379,243] → 622
# i.e. [579,744] → 1323
¹Q# # If it's equal to the first input: stop the infinite loop
# (and output the duplicate list implicitly)
# i.e. 1323 and 622 → 0 (falsey) → continue the loop
# i.e. 1323 and 1323 → 1 (truthy) → stop the loop and output [579,744]
1
Replace saves 3 over the if.
– Emigna
Aug 31 at 6:10
@Emigna Ah, of course. Thanks!
– Kevin Cruijssen
Aug 31 at 6:52
add a comment |Â
up vote
3
down vote
up vote
3
down vote
05AB1E (legacy), 23 20 bytes
[²³«εð9ÃÂΩ:}²gôJDO¹Q#
-3 bytes thanks to @Emigna.
Unknown digits are spaces (
). Input order should be: expected result; longest string; shortest string.
Try it online.
Explanation:
[ # Start an infinite loop
²³« # Take the second and third inputs, and merge them together
# i.e. " 79" and " 4 " → " 79 4 "
ε } # Map each character to:
ð : # Replace a space with:
9ÃÂΩ # A random digit in the range [0,9]
# i.e. " 79 4 " → ['3','7','9','2','4','3']
# i.e. " 79 4 " → ['5','7','9','7','4','4']
²g # Get the length of the second input
# i.e. " 79" → 3
ô # And split it into two numbers again
# i.e. ['3','7','9','2','4','3'] and 3 → [['3','7','9'],['2','4','3']]
# i.e. ['5','7','9','7','4','4'] and 3 → [['5','7','9'],['7','4','4']]
J # Join each list together to a single number
# i.e. [['3','7','9'],['2','4','3']] → [379,243]
# i.e. [['5','7','9'],['7','4','4']] → [579,744]
D # Duplicate this list
O # Sum the list
# i.e. [379,243] → 622
# i.e. [579,744] → 1323
¹Q# # If it's equal to the first input: stop the infinite loop
# (and output the duplicate list implicitly)
# i.e. 1323 and 622 → 0 (falsey) → continue the loop
# i.e. 1323 and 1323 → 1 (truthy) → stop the loop and output [579,744]
05AB1E (legacy), 23 20 bytes
[²³«εð9ÃÂΩ:}²gôJDO¹Q#
-3 bytes thanks to @Emigna.
Unknown digits are spaces (
). Input order should be: expected result; longest string; shortest string.
Try it online.
Explanation:
[ # Start an infinite loop
²³« # Take the second and third inputs, and merge them together
# i.e. " 79" and " 4 " → " 79 4 "
ε } # Map each character to:
ð : # Replace a space with:
9ÃÂΩ # A random digit in the range [0,9]
# i.e. " 79 4 " → ['3','7','9','2','4','3']
# i.e. " 79 4 " → ['5','7','9','7','4','4']
²g # Get the length of the second input
# i.e. " 79" → 3
ô # And split it into two numbers again
# i.e. ['3','7','9','2','4','3'] and 3 → [['3','7','9'],['2','4','3']]
# i.e. ['5','7','9','7','4','4'] and 3 → [['5','7','9'],['7','4','4']]
J # Join each list together to a single number
# i.e. [['3','7','9'],['2','4','3']] → [379,243]
# i.e. [['5','7','9'],['7','4','4']] → [579,744]
D # Duplicate this list
O # Sum the list
# i.e. [379,243] → 622
# i.e. [579,744] → 1323
¹Q# # If it's equal to the first input: stop the infinite loop
# (and output the duplicate list implicitly)
# i.e. 1323 and 622 → 0 (falsey) → continue the loop
# i.e. 1323 and 1323 → 1 (truthy) → stop the loop and output [579,744]
edited Aug 31 at 6:51
answered Aug 30 at 16:50


Kevin Cruijssen
29.5k550162
29.5k550162
1
Replace saves 3 over the if.
– Emigna
Aug 31 at 6:10
@Emigna Ah, of course. Thanks!
– Kevin Cruijssen
Aug 31 at 6:52
add a comment |Â
1
Replace saves 3 over the if.
– Emigna
Aug 31 at 6:10
@Emigna Ah, of course. Thanks!
– Kevin Cruijssen
Aug 31 at 6:52
1
1
Replace saves 3 over the if.
– Emigna
Aug 31 at 6:10
Replace saves 3 over the if.
– Emigna
Aug 31 at 6:10
@Emigna Ah, of course. Thanks!
– Kevin Cruijssen
Aug 31 at 6:52
@Emigna Ah, of course. Thanks!
– Kevin Cruijssen
Aug 31 at 6:52
add a comment |Â
up vote
3
down vote
Perl 6, 81 74 bytes
-7 bytes thanks to nwellnhof!
first try S/=/==/.EVAL,map $^a;S:g[#]=$a[$++],[X] ^10 xx.comb('#')
Try it online!
Anonymous code block that takes input as a string containing an arithmetical expression e.g. "12#+45#=579". Substitutes each #
with possible permutations of digits, substitutes the=
with ==
and finds the first valid result.
Explanation:
# Anonymous code block
first # Find the first of:
^10 # The range of 0 to 9
xx.comb('#') # Multiplied by the number #s in the code
,[X] # The cross-product of these lists
map # Map each crossproduct to:
$^a;.trans: "#"=>$a[$++] # The given string with each # translated to each element in the list
try S/=/==/.EVAL, # Find which of these is true when = are changed to == and it is eval'd
You can useS:g[#]=$a[$++]
instead oftrans
for 74 bytes.
– nwellnhof
Sep 1 at 13:05
@nwellnhof I didn't realise you could useS///
in that sort of syntax! Thanks!
– Jo King
Sep 1 at 13:13
add a comment |Â
up vote
3
down vote
Perl 6, 81 74 bytes
-7 bytes thanks to nwellnhof!
first try S/=/==/.EVAL,map $^a;S:g[#]=$a[$++],[X] ^10 xx.comb('#')
Try it online!
Anonymous code block that takes input as a string containing an arithmetical expression e.g. "12#+45#=579". Substitutes each #
with possible permutations of digits, substitutes the=
with ==
and finds the first valid result.
Explanation:
# Anonymous code block
first # Find the first of:
^10 # The range of 0 to 9
xx.comb('#') # Multiplied by the number #s in the code
,[X] # The cross-product of these lists
map # Map each crossproduct to:
$^a;.trans: "#"=>$a[$++] # The given string with each # translated to each element in the list
try S/=/==/.EVAL, # Find which of these is true when = are changed to == and it is eval'd
You can useS:g[#]=$a[$++]
instead oftrans
for 74 bytes.
– nwellnhof
Sep 1 at 13:05
@nwellnhof I didn't realise you could useS///
in that sort of syntax! Thanks!
– Jo King
Sep 1 at 13:13
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Perl 6, 81 74 bytes
-7 bytes thanks to nwellnhof!
first try S/=/==/.EVAL,map $^a;S:g[#]=$a[$++],[X] ^10 xx.comb('#')
Try it online!
Anonymous code block that takes input as a string containing an arithmetical expression e.g. "12#+45#=579". Substitutes each #
with possible permutations of digits, substitutes the=
with ==
and finds the first valid result.
Explanation:
# Anonymous code block
first # Find the first of:
^10 # The range of 0 to 9
xx.comb('#') # Multiplied by the number #s in the code
,[X] # The cross-product of these lists
map # Map each crossproduct to:
$^a;.trans: "#"=>$a[$++] # The given string with each # translated to each element in the list
try S/=/==/.EVAL, # Find which of these is true when = are changed to == and it is eval'd
Perl 6, 81 74 bytes
-7 bytes thanks to nwellnhof!
first try S/=/==/.EVAL,map $^a;S:g[#]=$a[$++],[X] ^10 xx.comb('#')
Try it online!
Anonymous code block that takes input as a string containing an arithmetical expression e.g. "12#+45#=579". Substitutes each #
with possible permutations of digits, substitutes the=
with ==
and finds the first valid result.
Explanation:
# Anonymous code block
first # Find the first of:
^10 # The range of 0 to 9
xx.comb('#') # Multiplied by the number #s in the code
,[X] # The cross-product of these lists
map # Map each crossproduct to:
$^a;.trans: "#"=>$a[$++] # The given string with each # translated to each element in the list
try S/=/==/.EVAL, # Find which of these is true when = are changed to == and it is eval'd
edited Sep 1 at 13:13
answered Aug 30 at 21:18
Jo King
14.9k24084
14.9k24084
You can useS:g[#]=$a[$++]
instead oftrans
for 74 bytes.
– nwellnhof
Sep 1 at 13:05
@nwellnhof I didn't realise you could useS///
in that sort of syntax! Thanks!
– Jo King
Sep 1 at 13:13
add a comment |Â
You can useS:g[#]=$a[$++]
instead oftrans
for 74 bytes.
– nwellnhof
Sep 1 at 13:05
@nwellnhof I didn't realise you could useS///
in that sort of syntax! Thanks!
– Jo King
Sep 1 at 13:13
You can use
S:g[#]=$a[$++]
instead of trans
for 74 bytes.– nwellnhof
Sep 1 at 13:05
You can use
S:g[#]=$a[$++]
instead of trans
for 74 bytes.– nwellnhof
Sep 1 at 13:05
@nwellnhof I didn't realise you could use
S///
in that sort of syntax! Thanks!– Jo King
Sep 1 at 13:13
@nwellnhof I didn't realise you could use
S///
in that sort of syntax! Thanks!– Jo King
Sep 1 at 13:13
add a comment |Â
up vote
2
down vote
APL (Dyalog Unicode), 22 bytes
'#'⎕Râ•?10tâ£âÂŽâºt∘â†Â
Try it online!
add a comment |Â
up vote
2
down vote
APL (Dyalog Unicode), 22 bytes
'#'⎕Râ•?10tâ£âÂŽâºt∘â†Â
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
APL (Dyalog Unicode), 22 bytes
'#'⎕Râ•?10tâ£âÂŽâºt∘â†Â
Try it online!
APL (Dyalog Unicode), 22 bytes
'#'⎕Râ•?10tâ£âÂŽâºt∘â†Â
Try it online!
edited Aug 30 at 17:10
answered Aug 30 at 17:01


dzaima
12.8k21652
12.8k21652
add a comment |Â
add a comment |Â
up vote
2
down vote
Java 10, 203 197 bytes
(a,b,c)->int A=0,B=0,l=a.length();for(a+=b,b="";A+B!=c;A=c.valueOf(b.substring(0,l)),B=c.valueOf(b.substring(l)),b="")for(var t:a.toCharArray())b+=t<36?(int)(Math.random()*9)+"":t;return A+"+"+B;
Try it online.
Explanation:
(a,b,c)-> // Method with 2 Strings & integer parameters and String return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.length(); // Length of the first String-input
for(a+=b, // Concat the second String-input to the first
b=""; // Reuse `b`, and start it as an empty String
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=c.valueOf(b.substring(0,l)),
// Set `A` to the first String-part as integer
B=c.valueOf(n.substring(l)),
// Set `B` to the second String-part as integer
b="") // Reset `b` to an empty String
for(var t:a.toCharArray())
// Inner loop over the characters of the concatted String inputs
b+=t<36? // If the current character is a '#':
(int)(Math.random()*9)+""
// Append a random digit to `b`
: // Else (it already is a digit):
t; // Append this digit as is to `b`
return A+"+"+B; // After the loop, return `A` and `B` as result
add a comment |Â
up vote
2
down vote
Java 10, 203 197 bytes
(a,b,c)->int A=0,B=0,l=a.length();for(a+=b,b="";A+B!=c;A=c.valueOf(b.substring(0,l)),B=c.valueOf(b.substring(l)),b="")for(var t:a.toCharArray())b+=t<36?(int)(Math.random()*9)+"":t;return A+"+"+B;
Try it online.
Explanation:
(a,b,c)-> // Method with 2 Strings & integer parameters and String return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.length(); // Length of the first String-input
for(a+=b, // Concat the second String-input to the first
b=""; // Reuse `b`, and start it as an empty String
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=c.valueOf(b.substring(0,l)),
// Set `A` to the first String-part as integer
B=c.valueOf(n.substring(l)),
// Set `B` to the second String-part as integer
b="") // Reset `b` to an empty String
for(var t:a.toCharArray())
// Inner loop over the characters of the concatted String inputs
b+=t<36? // If the current character is a '#':
(int)(Math.random()*9)+""
// Append a random digit to `b`
: // Else (it already is a digit):
t; // Append this digit as is to `b`
return A+"+"+B; // After the loop, return `A` and `B` as result
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Java 10, 203 197 bytes
(a,b,c)->int A=0,B=0,l=a.length();for(a+=b,b="";A+B!=c;A=c.valueOf(b.substring(0,l)),B=c.valueOf(b.substring(l)),b="")for(var t:a.toCharArray())b+=t<36?(int)(Math.random()*9)+"":t;return A+"+"+B;
Try it online.
Explanation:
(a,b,c)-> // Method with 2 Strings & integer parameters and String return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.length(); // Length of the first String-input
for(a+=b, // Concat the second String-input to the first
b=""; // Reuse `b`, and start it as an empty String
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=c.valueOf(b.substring(0,l)),
// Set `A` to the first String-part as integer
B=c.valueOf(n.substring(l)),
// Set `B` to the second String-part as integer
b="") // Reset `b` to an empty String
for(var t:a.toCharArray())
// Inner loop over the characters of the concatted String inputs
b+=t<36? // If the current character is a '#':
(int)(Math.random()*9)+""
// Append a random digit to `b`
: // Else (it already is a digit):
t; // Append this digit as is to `b`
return A+"+"+B; // After the loop, return `A` and `B` as result
Java 10, 203 197 bytes
(a,b,c)->int A=0,B=0,l=a.length();for(a+=b,b="";A+B!=c;A=c.valueOf(b.substring(0,l)),B=c.valueOf(b.substring(l)),b="")for(var t:a.toCharArray())b+=t<36?(int)(Math.random()*9)+"":t;return A+"+"+B;
Try it online.
Explanation:
(a,b,c)-> // Method with 2 Strings & integer parameters and String return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.length(); // Length of the first String-input
for(a+=b, // Concat the second String-input to the first
b=""; // Reuse `b`, and start it as an empty String
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=c.valueOf(b.substring(0,l)),
// Set `A` to the first String-part as integer
B=c.valueOf(n.substring(l)),
// Set `B` to the second String-part as integer
b="") // Reset `b` to an empty String
for(var t:a.toCharArray())
// Inner loop over the characters of the concatted String inputs
b+=t<36? // If the current character is a '#':
(int)(Math.random()*9)+""
// Append a random digit to `b`
: // Else (it already is a digit):
t; // Append this digit as is to `b`
return A+"+"+B; // After the loop, return `A` and `B` as result
edited Aug 31 at 15:46
answered Aug 30 at 18:01


Kevin Cruijssen
29.5k550162
29.5k550162
add a comment |Â
add a comment |Â
up vote
1
down vote
C# .NET, 225 220 196 bytes
(a,b,c)=>int A=0,B=0,l=a.Length;for(a+=b,b="";A+B!=c;A=int.Parse(b.Substring(0,l)),B=int.Parse(b.Substring(l)),b="")foreach(var t in a)b+=(t<36?new System.Random().Next(10):t-48)+"";return(A,B);
Port of my Java 10 answer.
(I'm very rusty in C# .NET golfing, so can defintely be golfed..)
-3 bytes implicitly thanks to @user82593 and this new C# tip he added.
-29 bytes thanks to @hvd.
Try it online.
Explanation:
(a,b,c)=> // Method with 2 string & int parameters and int-tuple return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.Length; // Length of the first string-input
for(a+=b, // Concat the second string-input to the first
b=""; // Reuse `b`, and start it as an empty string
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=int.Parse(b.Substring(0,l)),
// Set `A` to the first string-part as integer
B=int.Parse(b.Substring(l)),
// Set `B` to the second string-part as integer
b="") // Reset `b` to an empty string
foreach(var t in a)
// Inner loop over the characters of the concatted string inputs
b+=(t<36? // If the current character is a '#':
new System.Random().Next(10)
// Use a random digit
: // Else (it already is a digit):
t-48) // Use this digit as is
+""; // And convert it to a string so it can be appended to the string
return(A,B); // After the loop, return `A` and `B` in a tuple as result
You can use the regularusing System;
instead, it is shorter thannamespace System
.
– hvd
Sep 1 at 8:01
@hvd That was it!.. I haven't done C# in years, lol.. I triedusing System.*;
similar as imports in Java, but that didn't work. Forgot I had to remove the.*
part.. Thanks for the -5 bytes.
– Kevin Cruijssen
Sep 1 at 11:01
1
Re-reading it now, that was actually a sub-optimal suggestion. You can writeint.Parse
(-4), usenew System.Random()
(+7) and dropusing System;
(-13) to save another 10 bytes. :) Also, you do not need.ToCharArray()
, that takes off 14 more bytes.
– hvd
Sep 1 at 11:05
@hvd Thanks! Not sure how I forgot aboutint.Parse
vsSystem.Int32.Parse
... It's basically the same asSystem.String
andstring
.. And didn't knew it was possible to loop over the characters without the.ToCharArray()
. Thanks for another -24 bytes. :D
– Kevin Cruijssen
Sep 1 at 11:49
add a comment |Â
up vote
1
down vote
C# .NET, 225 220 196 bytes
(a,b,c)=>int A=0,B=0,l=a.Length;for(a+=b,b="";A+B!=c;A=int.Parse(b.Substring(0,l)),B=int.Parse(b.Substring(l)),b="")foreach(var t in a)b+=(t<36?new System.Random().Next(10):t-48)+"";return(A,B);
Port of my Java 10 answer.
(I'm very rusty in C# .NET golfing, so can defintely be golfed..)
-3 bytes implicitly thanks to @user82593 and this new C# tip he added.
-29 bytes thanks to @hvd.
Try it online.
Explanation:
(a,b,c)=> // Method with 2 string & int parameters and int-tuple return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.Length; // Length of the first string-input
for(a+=b, // Concat the second string-input to the first
b=""; // Reuse `b`, and start it as an empty string
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=int.Parse(b.Substring(0,l)),
// Set `A` to the first string-part as integer
B=int.Parse(b.Substring(l)),
// Set `B` to the second string-part as integer
b="") // Reset `b` to an empty string
foreach(var t in a)
// Inner loop over the characters of the concatted string inputs
b+=(t<36? // If the current character is a '#':
new System.Random().Next(10)
// Use a random digit
: // Else (it already is a digit):
t-48) // Use this digit as is
+""; // And convert it to a string so it can be appended to the string
return(A,B); // After the loop, return `A` and `B` in a tuple as result
You can use the regularusing System;
instead, it is shorter thannamespace System
.
– hvd
Sep 1 at 8:01
@hvd That was it!.. I haven't done C# in years, lol.. I triedusing System.*;
similar as imports in Java, but that didn't work. Forgot I had to remove the.*
part.. Thanks for the -5 bytes.
– Kevin Cruijssen
Sep 1 at 11:01
1
Re-reading it now, that was actually a sub-optimal suggestion. You can writeint.Parse
(-4), usenew System.Random()
(+7) and dropusing System;
(-13) to save another 10 bytes. :) Also, you do not need.ToCharArray()
, that takes off 14 more bytes.
– hvd
Sep 1 at 11:05
@hvd Thanks! Not sure how I forgot aboutint.Parse
vsSystem.Int32.Parse
... It's basically the same asSystem.String
andstring
.. And didn't knew it was possible to loop over the characters without the.ToCharArray()
. Thanks for another -24 bytes. :D
– Kevin Cruijssen
Sep 1 at 11:49
add a comment |Â
up vote
1
down vote
up vote
1
down vote
C# .NET, 225 220 196 bytes
(a,b,c)=>int A=0,B=0,l=a.Length;for(a+=b,b="";A+B!=c;A=int.Parse(b.Substring(0,l)),B=int.Parse(b.Substring(l)),b="")foreach(var t in a)b+=(t<36?new System.Random().Next(10):t-48)+"";return(A,B);
Port of my Java 10 answer.
(I'm very rusty in C# .NET golfing, so can defintely be golfed..)
-3 bytes implicitly thanks to @user82593 and this new C# tip he added.
-29 bytes thanks to @hvd.
Try it online.
Explanation:
(a,b,c)=> // Method with 2 string & int parameters and int-tuple return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.Length; // Length of the first string-input
for(a+=b, // Concat the second string-input to the first
b=""; // Reuse `b`, and start it as an empty string
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=int.Parse(b.Substring(0,l)),
// Set `A` to the first string-part as integer
B=int.Parse(b.Substring(l)),
// Set `B` to the second string-part as integer
b="") // Reset `b` to an empty string
foreach(var t in a)
// Inner loop over the characters of the concatted string inputs
b+=(t<36? // If the current character is a '#':
new System.Random().Next(10)
// Use a random digit
: // Else (it already is a digit):
t-48) // Use this digit as is
+""; // And convert it to a string so it can be appended to the string
return(A,B); // After the loop, return `A` and `B` in a tuple as result
C# .NET, 225 220 196 bytes
(a,b,c)=>int A=0,B=0,l=a.Length;for(a+=b,b="";A+B!=c;A=int.Parse(b.Substring(0,l)),B=int.Parse(b.Substring(l)),b="")foreach(var t in a)b+=(t<36?new System.Random().Next(10):t-48)+"";return(A,B);
Port of my Java 10 answer.
(I'm very rusty in C# .NET golfing, so can defintely be golfed..)
-3 bytes implicitly thanks to @user82593 and this new C# tip he added.
-29 bytes thanks to @hvd.
Try it online.
Explanation:
(a,b,c)=> // Method with 2 string & int parameters and int-tuple return-type
int A=0,B=0, // Result-integers, starting both at 0
l=a.Length; // Length of the first string-input
for(a+=b, // Concat the second string-input to the first
b=""; // Reuse `b`, and start it as an empty string
A+B!=c // Loop as long as `A+B` isn't equal to the integer-input
; // After every iteration:
A=int.Parse(b.Substring(0,l)),
// Set `A` to the first string-part as integer
B=int.Parse(b.Substring(l)),
// Set `B` to the second string-part as integer
b="") // Reset `b` to an empty string
foreach(var t in a)
// Inner loop over the characters of the concatted string inputs
b+=(t<36? // If the current character is a '#':
new System.Random().Next(10)
// Use a random digit
: // Else (it already is a digit):
t-48) // Use this digit as is
+""; // And convert it to a string so it can be appended to the string
return(A,B); // After the loop, return `A` and `B` in a tuple as result
edited Sep 1 at 11:48
answered Aug 31 at 15:43


Kevin Cruijssen
29.5k550162
29.5k550162
You can use the regularusing System;
instead, it is shorter thannamespace System
.
– hvd
Sep 1 at 8:01
@hvd That was it!.. I haven't done C# in years, lol.. I triedusing System.*;
similar as imports in Java, but that didn't work. Forgot I had to remove the.*
part.. Thanks for the -5 bytes.
– Kevin Cruijssen
Sep 1 at 11:01
1
Re-reading it now, that was actually a sub-optimal suggestion. You can writeint.Parse
(-4), usenew System.Random()
(+7) and dropusing System;
(-13) to save another 10 bytes. :) Also, you do not need.ToCharArray()
, that takes off 14 more bytes.
– hvd
Sep 1 at 11:05
@hvd Thanks! Not sure how I forgot aboutint.Parse
vsSystem.Int32.Parse
... It's basically the same asSystem.String
andstring
.. And didn't knew it was possible to loop over the characters without the.ToCharArray()
. Thanks for another -24 bytes. :D
– Kevin Cruijssen
Sep 1 at 11:49
add a comment |Â
You can use the regularusing System;
instead, it is shorter thannamespace System
.
– hvd
Sep 1 at 8:01
@hvd That was it!.. I haven't done C# in years, lol.. I triedusing System.*;
similar as imports in Java, but that didn't work. Forgot I had to remove the.*
part.. Thanks for the -5 bytes.
– Kevin Cruijssen
Sep 1 at 11:01
1
Re-reading it now, that was actually a sub-optimal suggestion. You can writeint.Parse
(-4), usenew System.Random()
(+7) and dropusing System;
(-13) to save another 10 bytes. :) Also, you do not need.ToCharArray()
, that takes off 14 more bytes.
– hvd
Sep 1 at 11:05
@hvd Thanks! Not sure how I forgot aboutint.Parse
vsSystem.Int32.Parse
... It's basically the same asSystem.String
andstring
.. And didn't knew it was possible to loop over the characters without the.ToCharArray()
. Thanks for another -24 bytes. :D
– Kevin Cruijssen
Sep 1 at 11:49
You can use the regular
using System;
instead, it is shorter than namespace System
.– hvd
Sep 1 at 8:01
You can use the regular
using System;
instead, it is shorter than namespace System
.– hvd
Sep 1 at 8:01
@hvd That was it!.. I haven't done C# in years, lol.. I tried
using System.*;
similar as imports in Java, but that didn't work. Forgot I had to remove the .*
part.. Thanks for the -5 bytes.– Kevin Cruijssen
Sep 1 at 11:01
@hvd That was it!.. I haven't done C# in years, lol.. I tried
using System.*;
similar as imports in Java, but that didn't work. Forgot I had to remove the .*
part.. Thanks for the -5 bytes.– Kevin Cruijssen
Sep 1 at 11:01
1
1
Re-reading it now, that was actually a sub-optimal suggestion. You can write
int.Parse
(-4), use new System.Random()
(+7) and drop using System;
(-13) to save another 10 bytes. :) Also, you do not need .ToCharArray()
, that takes off 14 more bytes.– hvd
Sep 1 at 11:05
Re-reading it now, that was actually a sub-optimal suggestion. You can write
int.Parse
(-4), use new System.Random()
(+7) and drop using System;
(-13) to save another 10 bytes. :) Also, you do not need .ToCharArray()
, that takes off 14 more bytes.– hvd
Sep 1 at 11:05
@hvd Thanks! Not sure how I forgot about
int.Parse
vs System.Int32.Parse
... It's basically the same as System.String
and string
.. And didn't knew it was possible to loop over the characters without the .ToCharArray()
. Thanks for another -24 bytes. :D– Kevin Cruijssen
Sep 1 at 11:49
@hvd Thanks! Not sure how I forgot about
int.Parse
vs System.Int32.Parse
... It's basically the same as System.String
and string
.. And didn't knew it was possible to loop over the characters without the .ToCharArray()
. Thanks for another -24 bytes. :D– Kevin Cruijssen
Sep 1 at 11:49
add a comment |Â
up vote
1
down vote
Python 3, 121 155 152 149 bytes
import re
def f(i,k=0,S=re.sub):s=S('#','%s',i)%(*list('%0*d'%(i.count('#'),k)),);print(s)if eval(S('=','==',S('\b0*([1-9])','\1',s)))else f(i,k+1)
Try it online!
+34 New solution with regex to circumvent the fact that python doesn't support numbers with leading zeroes.
-3 thanks to @Jonathan Frech
The old solution doesn't work if # is the first character in any number (because eval doesn't accept leading zeroes) and is therefore invalid :(
def f(i,k=0):
s=i.replace('#','%s')%(*list('%0*d'%(i.count('#'),k)),)
print(s)if eval(s.replace('=','=='))else f(i,k+1)
Try it online!
1
I'm afraid this submission is invalid for the reason stated in the post.
– Erik the Outgolfer
Aug 30 at 17:10
2
Since your function does not contain any compound statements you can condense it to only one line.
– Jonathan Frech
Aug 30 at 23:21
add a comment |Â
up vote
1
down vote
Python 3, 121 155 152 149 bytes
import re
def f(i,k=0,S=re.sub):s=S('#','%s',i)%(*list('%0*d'%(i.count('#'),k)),);print(s)if eval(S('=','==',S('\b0*([1-9])','\1',s)))else f(i,k+1)
Try it online!
+34 New solution with regex to circumvent the fact that python doesn't support numbers with leading zeroes.
-3 thanks to @Jonathan Frech
The old solution doesn't work if # is the first character in any number (because eval doesn't accept leading zeroes) and is therefore invalid :(
def f(i,k=0):
s=i.replace('#','%s')%(*list('%0*d'%(i.count('#'),k)),)
print(s)if eval(s.replace('=','=='))else f(i,k+1)
Try it online!
1
I'm afraid this submission is invalid for the reason stated in the post.
– Erik the Outgolfer
Aug 30 at 17:10
2
Since your function does not contain any compound statements you can condense it to only one line.
– Jonathan Frech
Aug 30 at 23:21
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Python 3, 121 155 152 149 bytes
import re
def f(i,k=0,S=re.sub):s=S('#','%s',i)%(*list('%0*d'%(i.count('#'),k)),);print(s)if eval(S('=','==',S('\b0*([1-9])','\1',s)))else f(i,k+1)
Try it online!
+34 New solution with regex to circumvent the fact that python doesn't support numbers with leading zeroes.
-3 thanks to @Jonathan Frech
The old solution doesn't work if # is the first character in any number (because eval doesn't accept leading zeroes) and is therefore invalid :(
def f(i,k=0):
s=i.replace('#','%s')%(*list('%0*d'%(i.count('#'),k)),)
print(s)if eval(s.replace('=','=='))else f(i,k+1)
Try it online!
Python 3, 121 155 152 149 bytes
import re
def f(i,k=0,S=re.sub):s=S('#','%s',i)%(*list('%0*d'%(i.count('#'),k)),);print(s)if eval(S('=','==',S('\b0*([1-9])','\1',s)))else f(i,k+1)
Try it online!
+34 New solution with regex to circumvent the fact that python doesn't support numbers with leading zeroes.
-3 thanks to @Jonathan Frech
The old solution doesn't work if # is the first character in any number (because eval doesn't accept leading zeroes) and is therefore invalid :(
def f(i,k=0):
s=i.replace('#','%s')%(*list('%0*d'%(i.count('#'),k)),)
print(s)if eval(s.replace('=','=='))else f(i,k+1)
Try it online!
edited Sep 6 at 13:07
answered Aug 30 at 17:02
Heiteira
913
913
1
I'm afraid this submission is invalid for the reason stated in the post.
– Erik the Outgolfer
Aug 30 at 17:10
2
Since your function does not contain any compound statements you can condense it to only one line.
– Jonathan Frech
Aug 30 at 23:21
add a comment |Â
1
I'm afraid this submission is invalid for the reason stated in the post.
– Erik the Outgolfer
Aug 30 at 17:10
2
Since your function does not contain any compound statements you can condense it to only one line.
– Jonathan Frech
Aug 30 at 23:21
1
1
I'm afraid this submission is invalid for the reason stated in the post.
– Erik the Outgolfer
Aug 30 at 17:10
I'm afraid this submission is invalid for the reason stated in the post.
– Erik the Outgolfer
Aug 30 at 17:10
2
2
Since your function does not contain any compound statements you can condense it to only one line.
– Jonathan Frech
Aug 30 at 23:21
Since your function does not contain any compound statements you can condense it to only one line.
– Jonathan Frech
Aug 30 at 23:21
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%2f171470%2fmissing-numbers-in-arithmetic-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
Do we need to strip leading zeros?
– Mnemonic
Aug 30 at 14:55
@Mnemonic not necessarily
– Luis felipe De jesus Munoz
Aug 30 at 14:58
can I take the input with the sides around
=
swapped? e.g.579=1#3+45#
– dzaima
Aug 30 at 15:25
1
"Assume both term > 0" does "assume" mean that I have to output both terms > 0 or that I can assume that there's always a solution with both > 0 but output whatever?
– dzaima
Aug 30 at 15:48
1
also your added test-case avoids exactly what I was asking for - the leading zeroes
– dzaima
Aug 30 at 15:48