mardi 16 mai 2023

My Python code is running in an unintended fashion

I took 10 of my codes in one line and wrote a code that outputs the next 2 lines after the 2 lines from the 0th line of the list are output. But it works like this:

Before mergeSort of L (size: 50): 12 22

6

29 30 35

3 4 48 19 49

21 32 20 42 41 14 28 2 1 5 8 36 26 33 16 23 24 37 25 18 9 44 34 39 38 15 45 27 47 43 0 40 7 13 10 46 11 31 17

what shoud i do? i can't find problem in my level.

i tried Initialization of line_num variable, rewrite a code by recommended GPT, and I checked by printing all the variables and lists.

here is full code here

import random
import random, time

def genRandList(size_L):
    L = random.sample(range(size_L), size_L)
    print(L)

    return L
    

def printListSample(L, num_elements_per_Line, num_sample_Lines):
    line_num  = 0 
    for i in range(len(L)):
        print(L[i], end=" ")
        if i != 0: 
            if (num_elements_per_Line % i) == 0:
                print("\n")
                line_num  += 1
        
    if line_num == num_sample_Lines:
                print("..............................")

                for a in range(len(L)):
                    print(L[-(num_elements_per_Line * num_sample_Lines) + a] , end=" ")


    

def mergeSort(merge_list):
    
    return merge_list

num_sample_Lines = 2
num_elements_per_Line = 10
size_L = 50
L = genRandList(size_L)
print("\nBefore mergeSort of L (size: {}):".format(size_L))
printListSample(L, num_elements_per_Line, num_sample_Lines)
t1 = time.time()
L = mergeSort(L)
t2 = time.time()



Aucun commentaire:

Enregistrer un commentaire