Python Forum
How to improve the quality of my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to improve the quality of my code?
#1
Here is some code I wrote that finds some values: a,b,c,d,e, that satisfy some expression. I would like to know: how I could shorten this code (perhaps using a list comprehension?), how I could make the code more readable, if there is a more efficient way to do this, and any other tips you could give me. Any criticism is welcome as long as you also tell me how to address the criticism. Thanks!

nums = [2,3,5,7,9]
for a in nums:
    for b in nums:
        for c in nums:
            for d in nums:
                for e in nums:
                    x = a + b * c**2 + d**3 - e
                    if x == 399:
                        print(a,b,c,d,e)
P.S. This code was written to complete a task in the synacor challenge, I recommend it if you have some spare time and want to test your skills. (I am stuck on the teleport task so any hints for that would also be appreciated).
Reply
#2
I have no idea what synacor challange is. As I understand there is a list, some calculations should be performed with list elements and result is compared to 399. If result is equal to 399 then list elements are printed separated by space.

One can take advantage of indices:

>>> nums = [2,3,5,7,9]
>>> if nums[0] + nums[1] * nums[2] ** 2 + nums[3] ** 3 - nums[4] == 399:
...     print(*nums, sep=' ')
For readability purposes one can use unpacking:

>>> nums = [2,3,5,7,9]
>>> a, b, c, d, e = nums
>>> if a + b * c**2 + d**3 - e == 399:
...     print(*nums, sep=' ')
To improve readability even more one can move calculation under name (this can also be combined with unpacking):

>>> nums = [2,3,5,7,9]
>>> result = nums[0] + nums[1] * nums[2] ** 2 + nums[3] ** 3 - nums[4]
>>> if result == 399:
...     print(*nums, sep=' ')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Have a look at itertools.product()

from itertools import product

nums = [2,3,5,7,9]
for a, b, c, d, e in product(nums, repeat=5):
    if a + b * c**2 + d**3 - e == 399:
        print(a,b,c,d,e)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
It seems like some search pruning would be useful here. It's possible that b * c ** 2 > 399 or d ** 3 > 399. In which case you don't need to check the combinations of the other values. You are subtracting e, but you could just move the threshold to 408 to cover that. The problem is that itertools.product is going to be faster than doing the loops yourself. I'm not sure you can save enough time with search pruning to outweigh the speed benefits of product.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (OpenCV) Help to improve code for object detection and finding center of it saoko 0 1,201 May-14-2022, 05:34 PM
Last Post: saoko
  Best Video Quality And Stream Harshil 2 2,232 Aug-19-2020, 09:03 AM
Last Post: Harshil
  how can I improve the code to get it faster? aquerci 2 1,700 Feb-15-2020, 02:52 PM
Last Post: aquerci
  How can I improve this piece of code? aquerci 3 2,191 Nov-17-2019, 10:57 AM
Last Post: Gribouillis
  Help improve code efficiency benbrown03 9 4,338 Feb-20-2019, 03:45 PM
Last Post: ichabod801
  Python packages - quality j.crater 8 5,737 Mar-13-2017, 08:58 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020