Python Forum
What is the run time complexity of this code and please explain?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the run time complexity of this code and please explain?
#1
set1=set(input().split())#TAKING INPUT OF SETS
set2=set(input().split())
l=[]#CREATING EMPTY LIST FOR STORING THE COMMON DATA OF SETS
flag=0
#LOOP FOR CHECKING COMMON VALUE IN SETS 
for i in set1:
    for j in set2:
        if i==j:
            flag=1
            l.append(i)
if flag==0:#CHECKING FLAG VALUE IF IT IS ZERO THEN IT MEANS THERE IS NO COMMON DATA
    print("NULL")
l1=[]#LIST FOR STORING ALPHABATIC DATA
l2=[]#LIST FOR STORING NUMERIC DATA
#SEPARATING ALPHABATIC AND NUMERIC DATA
for i in l:
    i=str(i)
    if i.isalpha():
        l1.append(i)
    elif i.isdigit():
        l2.append(i)
l1=sorted(l1)#SORTING ALPHABATIC DATA
l2=sorted(l2)#SORTING NUMERIC DATA
s=''
#STORING THE ALPHABATIC AND NUMERIC DATA IN SINGLE VARIABLE
for i in l1:
    s=s+i
for j in l2:
    s=s+j
print(' '.join(s))#JOINING ALPHABATIC AND NUMERIC DATA
Reply
#2
What exactly is your question?
Reply
#3
What is the run time complexity of this code and please explain?

O(n^2). The code in lines 6-10 determine how many operations are required. If we assume len(set1) == len(set2) == N, then len(l) == N^2, and the length of list "l" controls how many times all the operations in lines 16-21 occur. There are a couple of sorts, but I am going to assume Python sort is better than O(n^2).

Even if one of the sets contained only 1 element I would still say the complexity is O(n^2) because of the sorts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Explain the python code in this definition Led_Zeppelin 1 749 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,019 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Explain the python code in this definition Led_Zeppelin 1 1,106 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,147 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,251 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  Could you explain each part of the code? Tsushida 2 1,522 Mar-20-2022, 08:19 AM
Last Post: Larz60+
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,271 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,149 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Code taking too much time to process ErPipex 11 4,969 Nov-16-2020, 09:42 AM
Last Post: DeaD_EyE
  poplib - parsing message body, could somebody please help explain this code t4keheart 2 2,305 Oct-12-2020, 01:59 PM
Last Post: t4keheart

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020