Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Operation "Find The Operator"

+7
−0

Challenge

Make a program that takes input of 3 non-negative integers: a result and 2 other values that once calculated results to the 3rd value.

  • The program must figure out how to get the 2 first numbers to equivalence itself with the 3rd. It could be either addition, subtraction, multiplication, integer division, modulo and exponentiation.
  • You can't change the places of the 2 variables when building the equation, meaning you can't get some value by intertwining in the methods excluding addition and multiplication.
  • The returning value is the operator that makes the equation true (it's best to use some distinguishable character to separate it from the other methods).
  • Use whatever characters you want to use to determine the method that makes the equation truthy.
  • If multiple operators make the equation truthy, then output them in any order.
  • What makes it true? If an equation exists within the 2 numbers that results to the value of the 3rd.
  • What to return when it's false? Something but not nothing (whitespaces and newlines aren't allowed as such output).
  • This is code-golf so the shortest program wins!

Test Cases

# Input (#, #, #)
// Output (+-*/%^ or .)

2 3 5
+

7 3 2
/

9 8 4
.

1 2 2
*

2 2 4
+ * ^

14 6 8
-

5 0 2
.

0 0 1
^

8 4 0
%

0 0 0
+ - *
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

5 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+3
−0

Python 3, 106 91 bytes

Saved 15 bytes thanks to Moshi in the comments

lambda a,b,c:[p for p in"+ - * // % **".split()if(p in'+-**'or b)and eval(f"{a}{p}{b}")==c]

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Further improvements (1 comment)
91 bytes with list comprehension (2 comments)
+4
−0

C (gcc), 147 151 bytes

#define L(X)X(+,a+b)X(-,a-b)X(*,a*b)X(/,b&&a/b)X(%,b&&a%b)X(^,pow(a,b))
#define M(o,d)printf("%s",d==c?#o" ":z++==5?".":"");
z;s[99];f(a,b,c){L(M)}

Try it online!

Somewhat naive solution with X macros that builds up a string before printing. The b&&a/b checks are there to deal with division by zero.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Ruby, 58 bytes

Simple eval solution. Using ** for exponentiation.

->a,b,c{%w[+ - * / % **].select{eval("#{a}#{_1}#{b}")==c}}

Attempt This Online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

-8b (2 comments)
+1
−0

Japt, 16 bytes

Takes the result as the first input and the other 2 values as an array as the second input. Uses z for floor division & p for exponentiation and outputs " if there's no match (costing 3 bytes :\)

"+-*z%p"ƶVrXêQ

Try it

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

I'm not sure the conditions allow reordering the input values that way. Maybe Mark Giraffe can clarif... (2 comments)
+1
−0

Ruby, 57 bytes

->a,b,c{%w[+ - * / % **].select{c==a.send(_1,b)rescue p}}

Attempt This Online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »