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

Post History

71%
+3 −0
Challenges Operation "Find The Operator"

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!

posted 3y ago by celtschk‭  ·  edited 3y ago by celtschk‭

Answer
#2: Post edited by user avatar celtschk‭ · 2021-10-08T05:18:53Z (about 3 years ago)
Saved 15 bytes thanks to @Moshi in the comments
  • # [Python 3], 106 bytes
  • This uses `'//'` to denote integer division and `'**'` to denote exponentiation. Returns an empty list if none of the operations work.
  • <!-- language-all: lang-python -->
  • <pre><code>def f(a,b,c):
  • &#9;o=[]
  • &#9;for p in "+ - * // % **".split():
  • &#9;&#9;try:o+=[p]*eval(f"a{p}b==c")
  • &#9;&#9;except:0
  • &#9;return o
  • </code></pre>
  • [Try it online!][TIO-kugrxwdb]
  • [Python 3]: https://docs.python.org/3/
  • [TIO-kugrxwdb]: https://tio.run/##fZJLi4MwEMfP5lMMQjHatNrXPgQPhd2FhT0s9Fg8WJtsBavBxKWL@Nld4yMI@8ghh/n9/zOTyfAvecmzTdOcKQOGI3Iise0jIw@OITJYXgCHJANzDgtwwHVhBo5jLgVPE4mV0JDFl5/PgyMPHfoZpZiZUcXrUxDEpt1ieospl76HjILKssggbyQVMo4EFRBAhaA9GNYENgR2tg9wBGtuQUgGct@R9UBcd4IeCTwQ2HZIB1dEJRv1zkS@7shW1yAKq2siWm0J3LVpB9FiYt8R8MbEOuh1wdVYbVpOtdbSAc0mpDd500YWfTcQdpIaIfUfalA4Kj7KK82kIEBvnMaSntu5K1VBRZnKdogMO1pldyhhmgba1rvU4UWSScys9/3h8PzkA6u0v1af0ntrq09GU0F/el/2r2//eAmIS16mZzhRqMYOVEakluqXR6k105uxTCS9Cjw89M85oOYb "Python 3 – Try It Online"
  • # [Python 3], <s>106</s> 91 bytes
  • Saved 15 bytes thanks to [Moshi](https://codegolf.codidact.com/users/53196) in the [comments](https://codegolf.codidact.com/comments/thread/4781#comment-14226)
  • <!-- language-all: lang-python -->
  • lambda a,b,c:[p for p in"+ - * // % **".split()if(p in'+-**'or b)and eval(f"{a}{p}{b}")==c]
  • [Try it online!][TIO-kuhwxmjm]
  • [Python 3]: https://docs.python.org/3/
  • [TIO-kuhwxmjm]: https://tio.run/##fZJJa8MwEIXv/hWPQPASOc7axeBDoC0UeijkmOYg21JjcBxjKaXF@Le7lhdh6KKDDvO9NzMaTf4lT5dsXXMEeKtTeg5jCkpCEvmHHPxSIEeSTWZw4cDzMIXjTOYiTxNp2Qm3FDVnruOYjTS0aRaDfdDU4pOSVmVelWE1sYMgOtaSCRlRwURTqTTQHAsrgjXB1vaBA8yZiSPpyW1LVj3xvBG6J7gj2LRIB5dEJRv0zki@aslG1yAKq2skWm4Ibpq0vcgd2bcEiyGxDi7a4HKoNi6nWmtoj6Yj0pkW40bcrhscW0llGDHjUIOyaPF@PbNMCgL2mbNIstj2W1XBxDWVzRC55WiV3aKEaxpoW@dSJy@STFrcfN3t948PPnip/ZX6lM5bmV0ylgr20/u0e375x0sgTpdrGiNkKIcOVEZDrdIvj2rWB3oz5olkZ2H1D/1zDkb9DQ "Python 3 – Try It Online"
#1: Initial revision by user avatar celtschk‭ · 2021-10-07T10:10:18Z (about 3 years ago)
# [Python 3], 106 bytes

This uses `'//'` to denote integer division and `'**'` to denote exponentiation. Returns an empty list if none of the operations work.

<!-- language-all: lang-python -->

<pre><code>def f(a,b,c):
&#9;o=[]
&#9;for p in "+ - * // % **".split():
&#9;&#9;try:o+=[p]*eval(f"a{p}b==c")
&#9;&#9;except:0
&#9;return o
</code></pre>

[Try it online!][TIO-kugrxwdb]

[Python 3]: https://docs.python.org/3/
[TIO-kugrxwdb]: https://tio.run/##fZJLi4MwEMfP5lMMQjHatNrXPgQPhd2FhT0s9Fg8WJtsBavBxKWL@Nld4yMI@8ghh/n9/zOTyfAvecmzTdOcKQOGI3Iise0jIw@OITJYXgCHJANzDgtwwHVhBo5jLgVPE4mV0JDFl5/PgyMPHfoZpZiZUcXrUxDEpt1ieospl76HjILKssggbyQVMo4EFRBAhaA9GNYENgR2tg9wBGtuQUgGct@R9UBcd4IeCTwQ2HZIB1dEJRv1zkS@7shW1yAKq2siWm0J3LVpB9FiYt8R8MbEOuh1wdVYbVpOtdbSAc0mpDd500YWfTcQdpIaIfUfalA4Kj7KK82kIEBvnMaSntu5K1VBRZnKdogMO1pldyhhmgba1rvU4UWSScys9/3h8PzkA6u0v1af0ntrq09GU0F/el/2r2//eAmIS16mZzhRqMYOVEakluqXR6k105uxTCS9Cjw89M85oOYb "Python 3 – Try It Online"