After I looked at the example someone gave me I was able to work it out.
first_integer = input("Please enter the first integer: ")
second_integer = input("Please enter the second integer: ")
third_integer = input("Please enter the third integer: ")
fourth_integer = input("Please enter the fourth integer: ")
integer1 = int(first_integer)
integer2 = int(second_integer)
integer3 = int(third_integer)
integer4 = int(fourth_integer)
min_number = min(integer1, integer2, integer3, integer4)
max_number = max(integer1, integer2, integer3, integer4)
mid_number_min = min(max(integer1, integer2), max(integer2, integer3),
max(integer3, integer4), max(integer1, integer3),
max(integer1, integer4), max(integer2, integer4))
mid_number_max = max(min(integer1, integer2), min(integer2, integer3),
min(integer3, integer4), min(integer1, integer3),
min(integer1, integer4), min(integer2, integer4))
print(min_number, mid_number_min, mid_number_max, max_number)
Results in:
1 2 3 4
Not sure how you expect to get the mid numbers out of the tuple with just min
max
across the tuple. You need to check each combination of integers:
>>>
import itertools as it
>>>
integers = 4, 3, 2, 1 >>>
(min(integers),
...min( * (max(a, b) for a, b in it.combinations(integers, 2)))
...max( * (min(a, b) for a, b in it.combinations(integers, 2)))
...max(integers)
(1, 2, 3, 4)
If you use a list
instead of a tuple
, you can do this:
integers = [integer1, integer2, integer3, integer4] min_number = min(integers) max_number = max(integers) # Now remove the already found numbers integers.remove(min_number) integers.remove(max_number) mid_number_max = max(integers) mid_number_min = min(integers)
To find out the max_number
and mid_number_max
, we first find out the max value of the left 3 elements (max_left
) and the max value of the right 3 elements (max_right
). Then compare max_left
and max_right
. If they are not equal, it means one of them is the max_number
and the other is mid_number_max
. If they are equal, it means the max_number
locates at integers[1]
or integers[2]
. So the mid_number_max
value should be the min(integers[1], integers[2])
or max(integers[0], integers[3])
. So, just pick up the larger one as the mid_number_max
. The same idea to find out the min_number
and mid_number_low
.
integers = (3, 1, 2, 4) max_left = max(integers[0], integers[1], integers[2]) max_right = max(integers[1], integers[2], integers[3]) min_left = min(integers[0], integers[1], integers[2]) min_right = min(integers[1], integers[2], integers[3]) if (max_left != max_right): max_number = max(max_left, max_right) mid_number_max = min(max_left, max_right) else: max_number = max_left mid_number_max = max(min(integers[1], integers[2]), max(integers[0], integers[3])) if (min_left != min_right): min_number = min(min_left, min_right) mid_number_low = max(min_left, min_right) else: min_number = min_left mid_number_low = min(max(integers[1], integers[2]), min(integers[0], integers[3])) print(min_number) print(mid_number_low) print(mid_number_max) print(max_number)
Last Updated : 23 Jun, 2022,GATE Live Course 2023
Input: a = 3, b = 2, c = 9
Output: 2 3 9
Input: a = 4, b = 1, c = 9
Output: 1 4 9
Output:
1 4 9
The sort() method sorts the list ascending by default.,You can also make a function to decide the sorting criteria(s).,Yes,sort() is a built in function in python.,Python Program to Sort List in Ascending Order without using Sort. In this program, we are using Nested For Loop to iterate each number in a list, and sort them in ascending order.
Yes it is possible. You can refer to the following code to understand it.
my_list = [-15, -26, 15, 1, 23, -64, 23, 76]
new_list = []
while my_list:
min = my_list[0]
for x in my_list:
if x < min:
min = x
new_list.append(min)
my_list.remove(min)
print(new_list)
You can try this:
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " % i))
NumList.append(value)
for i in range(Number):
for j in range(i + 1, Number):
if (NumList[i] > NumList[j]):
temp = NumList[i]
NumList[i] = NumList[j]
NumList[j] = temp
print("Element After Sorting List in Ascending Order is : ", NumList)
list = [1, 3, 123, 1, 42, 123] # RANDOM NUMBERS
list = [1, 3, 123, 1, 42, 123] # RANDOM NUMBERS
list.sort()
Syntax
list.sort(reverse = True | False, key = myFunc)
Questions: how to sort a list of numbers without using built - in functions from command prompt.please
try this code.
Questions:how to sort a list of numbers without using built-in functions from command prompt.please try this code.
import sys
import sys
sortval = [] val = sys.argv[1: ]
I guess you are trying to do something like this:
data_list = [-5, -23, 5, 0, 23, -6, 23, 67] new_list = [] while data_list: minimum = data_list[0] # arbitrary number in list for x in data_list: if x < minimum: minimum = x new_list.append(minimum) data_list.remove(minimum) print new_list
Problem Description: Given an array A[] of size n, you need to find the maximum and minimum element present in the array. Your algorithm should make the minimum number of comparisons.,We initialize both minimum and maximum element to the first element and then traverse the array, comparing each element and update minimum and maximum whenever necessary.,We have initialized maximum and minimum with the first element of the array - why?,Find the smallest and second smallest element in the array using minimum number of comparsions
Pseudo-Code
int[] getMinMax(int A[], int n) {
int max = A[0]
int min = A[0]
for (i = 1 to n - 1) {
if (A[i] > max)
max = A[i]
else if (A[i] < min)
min = A[i]
}
// By convention, let ans[0] = maximum and ans[1] = minimum
int ans[2] = {
max,
min
}
return ans
}
Pseudo Code
int[] findMinMax(int A[], int start, int end) {
int max;
int min;
if (start == end) {
max = A[start]
min = A[start]
} else if (start + 1 == end) {
if (A[start] < A[end]) {
max = A[end]
min = A[start]
} else {
max = A[start]
min = A[end]
}
} else {
int mid = start + (end - start) / 2
int left[] = findMinMax(A, start, mid)
int right[] = findMinMax(A, mid + 1, end)
if (left[0] > right[0])
max = left[0]
else
max = right[0]
if (left[1] < right[1])
min = left[1]
else
min = right[1]
}
// By convention, we assume ans[0] as max and ans[1] as min
int ans[2] = {
max,
min
}
return ans
}
For counting the number of comparisons, since this is a recursive function, let us define the recurrence relation :
T(n) = 2 T(n / 2) + 2
T(2) = 1
T(1) = 0
We can solve this recurrence relation by master method / recursion tree method.
if n is a power of 2
T(n) = 3n / 2 - 2
Note: You can replace the number 5 with any row number you want.,So, what if you don’t care about a single column. You want to know the maximum and minimum of the whole data set? So be it…,You think that has proven the flexibility of min and max? Wait for the next example…,Our example vector consists of five numbers, stored in the data object x1. Now, let’s compute the maximum and minimum of this vector.
max(x) min(x)
x1 < -c(4, 1, -50, 20, 8) # Create example vector
max(x1) # Apply max to vector
# 20
min(x1) # Apply min to vector
# - 50
x2 < -c(x1, NA) # Create example vector with NA
x2 # Print vector to RStudio console
# 4 1 - 50 20 8 NA
max(x2) # max returns NA # NA