简体   繁体   中英

How to calculate time complexity for this python program?

Hey i dont know how to calculate the complexity for the below program, it would be great if you can say Big o asymptomatic notation for this python program

added this paragraph to get pass the error leave this

Note that we will focus our study in these common time complexities but there are some other time complexities out there which you can study later. As already said, we generally use the Big-O notation to describe the time complexity of algorithms. There's a lot of math involved in the formal definition of the notation, but informally we can assume that the Big-O notation gives us the algorithm's approximate run time in the worst case. When using the Big-O notation, we describe the algorithm's efficiency based on the increasing size of the input data (n). For example, if the input is a string, the n will be the length of the string. If it is a list, the n will be the length of the list and so on. Now, let's go through each one of these common time complexities and see some examples of algorithms. Note that I tried to follow the following approach: present a little description, show a simple and understandable example and show a more complex example (usually from a real-world problem).


Maths_mx,Biology_mx,English_mx,Physics_mx,Chemistry_mx,Hindi_mx=0,0,0,0,0,0
students_total={}

with open('Student_marks_list.csv',mode='r') as marks_csv:
    marklist=csv.reader(marks_csv,delimiter=',')
    line=0
    for row in marklist:
        if line>0:
            Student_name=row[0]
            Maths,Biology,English,Physics,Chemistry,Hindi=int(row[1]),int(row[2]),int(row[3]),int(row[4]),int(row[5]),int(row[6])
            total=Maths+Biology+English+Physics+Chemistry+Hindi
            students_total[total]=Student_name
            if Maths_mx<Maths:
                Maths_mx=Maths
                Maths_topper=Student_name
            if Biology_mx<Biology:
                Biology_mx=Biology
                Biology_topper=Student_name
            if English_mx<English:
                English_mx=English
                English_topper=Student_name
            if Physics_mx<Physics:
                Physics_mx=Physics
                Physics_topper=Student_name
            if Chemistry_mx<Chemistry:
                Chemistry_mx=Chemistry
                Chemistry_topper=Student_name
            if Hindi_mx<Hindi:
                Hindi_mx=Hindi
                Hindi_topper=Student_name
        line=line+1

print("\n\nTopper in Maths is "+Maths_topper+"with %d marks"%Maths_mx)
print("Topper in Biology is "+Biology_topper+"with %d marks"%Biology_mx)
print("Topper in English is "+English_topper+"with %d marks"%English_mx)
print("Topper in Physics is "+Physics_topper+"with %d marks"%Physics_mx)
print("Topper in Chemistry is "+Chemistry_topper+"with %d marks"%Chemistry_mx)
print("Topper in Hindi is "+Hindi_topper+"with %d marks"%Hindi_mx)
print("\n  Top three student toppers are")
count=1
for i in sorted(students_total,reverse=True):
    print("\t\t\t\t"+students_total[i]+" with %d marks"%i)
    count=count+1
    if count>3:
        break

Before I divulge the answer, it is important to note that you should try , and document your process before code-dumping on stack overflow.

Looking at your code, it looks like you're scanning in a CSV and parsing it. All of the individual operations you do while parsing the input are O(1), so the time complexity of this section is O(n) where n is the size of your input. You can break this down further by number of rows and columns, but I'll leave that as an exercise to you. After that, you look at the dictionary of students in reverse lexicographical order. This collects the keys of the dictionary (O(n)), sorts them (O(n log n)), and reverses them(O(n)).

Overall, your code is O(n log n). I suggest next time, you break the code down statement-by-statement in terms of time complexity, and attempt to recombine them based on the decomposition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM