Integer and sort in ascending order

Asked 2 years ago, Updated 2 years ago, 26 views

import random
import math
import numpy as np
import xlwings as xw
                 #***The average distance (dis)** after n times of simulation picking t times per person
print("How many products do you have?>> ")
q = int(input())
print("What number(n) would you like to simulate?>> ")
n = int(input())
arr = []
def sort_num(x):
    return x

A1 = (1,3,5,7,9,11,13,15,17,19)
B1 = (5,7,9,11,13,15,17,19,21,23,7,9,11,13,15,17,19,21,23,25,9,11,13,15,17,19,21,23,25,27)
C1 = (11,13,15,17,19,21,23,25,27,29,13,15,17,19,21,23,25,27,29,31,15,17,19,21,23,25,27,29,31,33,17,19,21,23,25,27,29,31,33,35,19,21,23,25,27,29,31,33,35,37)
result1 = 0

for i in range(n):
    for i in range(int(q)):
        x = int(random.randrange(1, 11))
        if x<=7 :
            ax = int(random.choice(A1))
            result1 = ax
        if x>7 and x<=9:
            ax = int(random.choice(B1))
            result1 = ax
        if x>9 and x<=10:
            ax = int(random.choice(C1))
            result1 = ax
    arr.append(result1)

print(sorted(arr))

I made the code like this, but I don't know how to proceed with this.

python

2022-09-20 14:32

1 Answers

arr = sorted(arr)
diffsum = 0
for x, x_next in zip(arr[:-1], arr[1:]):
    diff_sum += x_next-x

You can do this, but if you calculate the equation, it's erased one after another, and eventually x_q - x_1

arr = sorted(arr)
diffsum = arr[-1] - arr[0]


2022-09-20 14:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.