[SOLVED] How do I generate a list of truthy expressions based from some numbers given? more details in Description | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

[SOLVED] How do I generate a list of truthy expressions based from some numbers given? more details in Description

Dear SoloLearners, Please guide me on how to generate (or just ouput) any truthy expression based on some given numbers. My idea is by placing arbitrary operators in between the numbers to make the expression becomes truthy. Order of numbers or operators in the expression doesn't matter, as long as they form a truthy expression. I have no idea whether there is an algorithm to use unfortunately. Did think about permutation, but I have no idea how should permutation work with operators placement in an expression. For example (three numbers given - from user): 3, 15, 5 May generate/output something like these: 3 * 5 = 15 5 * 3 = 15 15 / 3 = 5 15 / 5 = 3 15 = 5 * 3 15 = 3 * 5 5 = 15 / 3 3 = 15 / 5 And more truthy expressions follows, if any, How can I do this? is this even possible? * Any hints, insights, guides or critics are welcome. Thank you in advance 🙏

21st Jul 2021, 7:57 AM
Ipang
9 Answers
+ 4
NotAPythonNinja You might want to change your elif to a 2nd to process inputs like: 2 2 4 Also, you might want to check on division by zero scenarios. Ex: 0 0 0
22nd Jul 2021, 7:29 AM
David Carroll
David Carroll - avatar
+ 3
I couldn't notice any difference in speed using either method from the Sololearn App. How are you guys running the code to see the speed differences?
23rd Jul 2021, 6:55 AM
David Carroll
David Carroll - avatar
22nd Jul 2021, 10:56 AM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Calvin, why the oneliner is slow?
22nd Jul 2021, 11:38 AM
Clear
+ 1
NotAPythonNinja Nice one bro! 👍 I will try some things up based on it 👌
21st Jul 2021, 10:47 AM
Ipang
+ 1
NotAPythonNinja, Why did you remove your answer though? now watching this thread doesn't feel right cause there are now missing links 😁 Stone age or not, I appreciate all contributors sparing their time to answer my question here 👌 Big Thanks Everyone 🙏
22nd Jul 2021, 9:53 AM
Ipang
+ 1
Cheralathan. E Probably due to the list comprehension along with the bool() typecast and the null-string multiplication, I guess. I'm not sure about the performance of __import__ though (I suppose that creating the string "itertools" takes up a bit of time).
22nd Jul 2021, 11:46 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
David Carroll I don't know any other method to test the speed performance other than manually running the two codes and comparing it. After a significant number of such trial runs, I've noticed that the second one is slower than the first one.
23rd Jul 2021, 7:33 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas there is a builtin module 'time' but is doesn't hive accurate result. There are many 3rd party libs tho.
23rd Jul 2021, 3:36 PM
Clear