The values 3 and 6 are already sorted from smallest to largest. It's not a good example to test if you're ordering them right or not :-)
Your function intercambiar(x,y)does not directly exchange the parameters that you pass to it (this cannot be done, because inside the function xe yare a copy of those parameters and they are not directly the same variables aas bthe calling function). But you have implemented it correctly and in addition to swapping xwith y, you also return the new swapped partner.
What you need is to receive that result in the function that makes use of intercambiar(), that is:
a, b = intercambiar(a, b)
To swap variables in python you don't need to write a function. You don't even need helper variables like aux. The following will work:
def ordenar2num(a,b):
if a>b:
a,b = b,a
return(a,b)
What happens is that you are passing the parameters to your functions by value , without retrieving the value returned by them.
If you retrieve the value returned by your function, you will see that it works correctly as shown in the following code
num1 = int(input("ingresa primer número: "));
num2 = int(input("ingresa segundo número: "));
def intercambiar(x, y):
aux = x;
x = y;
y = aux;
return (x, y);
def ordenar2num(x, y):
if x > y:
return intercambiar(x, y);
return (x, y);
numeros = ordenar2num(num1, num2)
print(numeros);
I hope I have helped. Luck!
update
After looking at the refinement of the question:
Note that ordering two numbers is trivial, but when we increase the number of numbers to order, the complexity skyrockets:
num1 = int(input("ingresa primer número: "));
num2 = int(input("ingresa segundo número: "));
num3 = int(input("ingresa tercer numero"));
def ordenar3num(x, y, z):
if x < y:
if x < z:
if (y < z):
return (x, y, z);
return (x, z, y);
return (z, x, y);
if y < z:
if x < z:
return (y, x, z);
return (y, z, x);
return (z, y, x);
numeros = ordenar3num(num1, num2, num3)
print(numeros);
As each time we add a new number to order we must take into account this new element in each case of comparison, perhaps we should think of a better solution.
We can start to think of the group of numbers entered as a list, and work on the list instead of working on each number. Which has the advantage that our solution becomes agnostic regarding the number of numbers entered.
num1 = int(input("ingresa primer número: "));
num2 = int(input("ingresa segundo número: "));
num3 = int(input("ingresa tercer numero: "));
def ordenarMejor(nums):
s = len(nums);
for i in range(s):
for j in range(1, s - i):
if nums[j-1] > nums[j]:
(nums[j-1], nums[j]) = (nums[j], nums[j-1])
return nums;
numeros = ordenarMejor([num1, num2, num3]);
print(numeros);
Which satisfies the restriction of not using python internal functions. If you consider that any other restriction should apply, do not hesitate to leave a comment.
The study of sorting algorithms is a very nice branch of computer science, I leave a link for reference.
Several things.
The values 3 and 6 are already sorted from smallest to largest. It's not a good example to test if you're ordering them right or not :-)
Your function
intercambiar(x,y)
does not directly exchange the parameters that you pass to it (this cannot be done, because inside the functionx
ey
are a copy of those parameters and they are not directly the same variablesa
asb
the calling function). But you have implemented it correctly and in addition to swappingx
withy
, you also return the new swapped partner.What you need is to receive that result in the function that makes use of
intercambiar()
, that is:To swap variables in python you don't need to write a function. You don't even need helper variables like
aux
. The following will work:What happens is that you are passing the parameters to your functions by value , without retrieving the value returned by them.
If you retrieve the value returned by your function, you will see that it works correctly as shown in the following code
I hope I have helped. Luck!
update
After looking at the refinement of the question:
Note that ordering two numbers is trivial, but when we increase the number of numbers to order, the complexity skyrockets:
As each time we add a new number to order we must take into account this new element in each case of comparison, perhaps we should think of a better solution.
We can start to think of the group of numbers entered as a list, and work on the list instead of working on each number. Which has the advantage that our solution becomes agnostic regarding the number of numbers entered.
Which satisfies the restriction of not using python internal functions. If you consider that any other restriction should apply, do not hesitate to leave a comment.
The study of sorting algorithms is a very nice branch of computer science, I leave a link for reference.
I hope I have helped. Luck!