Find operations required to get result
TSQL(sqlserver 2016) 310 294 280 bytes
What a wonderful opportunity to write ugly code:
Golfed:
DECLARE @ varchar(max)= '5,5,5'
DECLARE @a varchar(20) = '125'
,@ varchar(max)='';WITH D as(SELECT @a a UNION ALL SELECT STUFF(a,charindex(',',a),1,value)FROM STRING_SPLIT('*,+,./,-,',',')x,d WHERE a like'%,%')SELECT @+=a+','''+REPLACE(a,'.','')+'''),('FROM D WHERE a not like'%,%'EXEC('SELECT y FROM(values('+@+'null,null))g(x,y)WHERE x='+@b)
Try it online
Readable:(insertion of decimal point(.) and removal of the same is necessary in order for sql to accept that 4/5 is not 0 - well removal is for the people testing it)
DECLARE @a varchar(max)= '5,5,5'
DECLARE @b varchar(20) = '5'
,@ varchar(max)=''
;WITH D as
(
SELECT @a a
UNION ALL
SELECT STUFF(a,charindex(',',a),1,value)
FROM STRING_SPLIT('*,+,./,-,',',')x,d
WHERE a like'%,%'
)
SELECT @+=a+','''+REPLACE(a,',','')+'''),('
FROM D
WHERE a not like'%,%'
EXEC('SELECT y FROM(values('+@+'null,null))g(x,y)WHERE x='+@b)
This solution can also handle these types of input:
Input :[1,2,3,4,5] 0 Output : 12-3-4-5
Oracle SQL 11.2, 322 304 270 bytes
SELECT o FROM(SELECT REPLACE(SUBSTR(:1,1,1)||REPLACE(SYS_CONNECT_BY_PATH(a||SUBSTR(:1,LEVEL*2+1,1),','),','),'_')o,LEVEL l FROM(SELECT SUBSTR('+-*/_',LEVEL,1)a FROM DUAL CONNECT BY LEVEL<6)CONNECT BY LEVEL<LENGTH(:1)/2)WHERE:2=dbms_aw.eval_number(o)AND l>LENGTH(:1)/2-1;
:1 is the list of digits
:2 is the result searched
Un-golfed :
SELECT o
FROM (
SELECT REPLACE(SUBSTR(:1,1,1)||REPLACE(SYS_CONNECT_BY_PATH(a||SUBSTR(:1,LEVEL*2+1,1),','),','),'_')o,LEVEL l
FROM ( -- Create one row per operator
SELECT SUBSTR('+-*/_',LEVEL,1)a FROM DUAL CONNECT BY LEVEL<6
) CONNECT BY LEVEL<LENGTH(:1)/2 -- Create every combination of operators, one per ','
)
WHERE :2=dbms_aw.eval_number(o) -- filter on result = evaluation
AND l>LENGTH(:1)/2-1 -- keep only expressions using every digits
Pyth, 23 bytes
Due to security reasons, *
and /
won't evaluate online, but they theoretically work.
fqeQvTms.ihQd^"+-*/"lth
Test suite with only +
and -
.