Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Which of these codes do you think is the simplest

  1. Nov 10, 2016 #1
    Codes.JPG Since i don't know how to write code here on forums i will upload a picture and explain whats it about.
    In the picture there are three codes that define the same procedure of finding the biggest number out of three given. The first code in the picture is defining a procedure called ##biggest(x,y,z)## and my friend and i agree that its the simplest one but it defines a procedure that is used in the given procedure to help the calculation. The second code is made by me and the third by my friend. We're kinda arguing whose is simpler and more elegant to use xD, what do you think?
    Last edited by a moderator: Nov 10, 2016
  2. jcsd
  3. Nov 10, 2016 #2


    User Avatar
    Gold Member

    I think if you only have 3 vars, it should be

    biggest = max(max(x,y),z) or whatever construct is equivalent in whatever language you are using.
  4. Nov 10, 2016 #3
    Yeah i agree :-D, but the goal is to define a procedure and not use any built in, we are both beginners so that's why. We're using python :-D
  5. Nov 10, 2016 #4


    Staff: Mentor

    Then use your bigger() function, following the idea that @phinds gave.

    Posting an image of your code isn't very helpful. If you have an error, we can't insert a comment in your code. It's best to copy and paste the code directly into the page here, surrounding it with code tags.

    Just like this:
    Code (Python):

    def bigger(x, y):
       if (x >= y):
          return x
          return y
    (Since I have two sets of code tags, the outer set is not being rendered by the browser, due to some alterations I made. The inner set is causing the Python code to be displayed with different colors. When you do this, just use one set of tags.
  6. Nov 10, 2016 #5


    User Avatar
    Science Advisor

    Code (Python):
    def biggest(x,y,z) : return x if x > y and x > z else y if y > z else z
  7. Nov 10, 2016 #6


    User Avatar

    If you're writing a function to find the maximum of more than two numbers as an exercise then you might as well make it work for any number of numbers.

    In Python if you put *args in a function's argument list then Python will collect all the extra arguments into a tuple called args that you can access and use in your function (more info here and elsewhere on the web). For example, this function simply prints all the arguments it is called with:
    Code (Python):
    def print_args(*arguments):
      for arg in arguments:
    You could similarly write a biggest function that identifies and returns the biggest argument it is called with.

    Code (Python):
    def biggest(number, *more_numbers):
      max_found = number
      for n in more_numbers:
        if n > max_found:
          max_found = n
      return max_found
  8. Nov 10, 2016 #7


    Staff: Mentor

    Use your bigger function to define your biggest function:
    Code (Python):

    def biggest(x,y,z):
        return bigger(x,bigger(y,z))
    Last edited: Nov 10, 2016
  9. Nov 11, 2016 #8
    Thanks i tried all and i think i like using the bigger function as i find it the simplest..:-)
  10. Nov 11, 2016 #9


    User Avatar
    Science Advisor

    Also note that instead of 'x == False' you can write 'not x'.
  11. Nov 13, 2016 #10
    Before I saw the Python label, I was thinking this (C):
    #define biggest(x,y,z) ((x>y)?((x>z)?x:z):((y>z)?y:z))
  12. Nov 13, 2016 #11

    jack action

    User Avatar
    Science Advisor
    Gold Member

    I prefer biggest2, based on a performance point of view. With 2 comparisons you get your answer.

    With biggest you need at least 3 comparisons, worst case scenario 4 comparisons (+ a function call).

    With biggest1, you need at least 4 comparisons, worst case scenario 8 comparisons, before getting an answer!
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: Which of these codes do you think is the simplest