Which of these codes do you think is the simplest

In summary: I prefer biggest2, based on a performance point of view.biggest2 = max(max(x,y),z) or whatever construct is equivalent in whatever language you are using.
  • #1
doktorwho
181
6
Codes.JPG
Since i don't know how to write code here on forums i will upload a picture and explain what's 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?
Codes.JPG
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
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.
 
  • Like
Likes Rx7man and doktorwho
  • #3
phinds said:
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.
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
 
  • #4
doktorwho said:
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
Then use your bigger() function, following the idea that @phinds gave.

doktorwho said:
Since i don't know how to write code here on forums i will upload a picture and explain what's it about.
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]
Python:
def bigger(x, y):
   if (x >= y):
      return x
   else:
      return y
[/code]
(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.
 
  • #5
doktorwho said:
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?
Python:
def biggest(x,y,z) : return x if x > y and x > z else y if y > z else z
 
  • #6
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:
Python:
def print_args(*arguments):
  for arg in arguments:
    print(arg)
You could similarly write a biggest function that identifies and returns the biggest argument it is called with.

Python:
def biggest(number, *more_numbers):
  max_found = number
  for n in more_numbers:
    if n > max_found:
      max_found = n
  return max_found
 
  • Like
Likes PeroK
  • #7
Use your bigger function to define your biggest function:
Python:
def biggest(x,y,z):
    return bigger(x,bigger(y,z))
 
Last edited:
  • Like
Likes jim mcnamara
  • #8
Thanks i tried all and i think i like using the bigger function as i find it the simplest..:-)
 
  • #9
doktorwho said:
Thanks i tried all and i think i like using the bigger function as i find it the simplest..:-)
Also note that instead of 'x == False' you can write 'not x'.
 
  • #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))
 
  • #11
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!
 

1. What is the definition of "simple" in this context?

In this context, "simple" refers to the code that is the most straightforward and easy to understand, with the fewest lines of code and minimal complexity.

2. Is there a specific programming language that is considered the simplest?

No, the simplicity of a code depends on the problem it is trying to solve and the programmer's approach. Some programming languages may be more suited for certain tasks, but there is no definitive answer to which language is the simplest overall.

3. How do you determine which code is the simplest?

The simplest code is often subjective and can vary depending on the person evaluating it. However, some factors that can contribute to simplicity include readability, efficiency, and the use of logical and concise expressions.

4. Can a simple code still be effective?

Yes, a simple code can be very effective. In fact, simplicity is often a desirable quality in programming as it can lead to easier maintenance and debugging. A simpler code may also run faster and be more scalable than a more complex one.

5. Is there a trade-off between simplicity and functionality in coding?

Sometimes, but not always. In some cases, adding more functionality may make the code more complex. However, a skilled programmer can often find ways to achieve both simplicity and functionality through efficient and strategic coding techniques.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
5K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
6K
  • Special and General Relativity
Replies
1
Views
523
Back
Top