HackerRank Skills Verification Test for Python

So, it's 8:44 p.m. and I just finished attempting a skills verification test in HackerRank for Python, this is 3 days after successfully failing the Problem Solving Test for the 3rd consecutive time, but scenario is different today... I have successfully passed all the Test Cases this time. 

With Python the things become quite easy, and the first problem just took me around 10 mins, but the 2nd one got a bit longer than I had expected, yah it wasn't so simple but I could have done it faster, I took around 24 mins for the 2nd problem statement, cause there was a catch with changing the temporary variables while looping in Python, and it became little messy at one point, then I had to print everything and then began the sinuous DEBUGGING process, at the end I got things working correctly and the test cases also passed without the everlasting TLE, the biggest headache of the Problem Solving Test. 

The first problem statement:-


Example for 1st Problem:-




The first problem was quite direct, I just had to make an avg func that took variable number of arguments as input and return the average of those arguments, it was also mentioned that there will be atleast 1 argument. We don't have a built in function to calculate the average in Python(Maybe I am wrong, I didn't check it), so I just made an average func and used it to return the average of the tuple passed to it. Simple!

The second problem statement:-


Example for 2nd Problem:-


The second problem is bit interesting, the main idea was to get words from a string, and then traverse all characters in each word and check for several conditions in them... and the best part to change the characters which can be a big problem if you don't know how to do that, cause strings in Python are immutable but we can use string slicing to do this, as you can see in my code. It was just this much, but the mistake I did in the starting was that I was changing the "word" variable which is just in the scope of the for loop and is temporary, but I should have changed the list "ls". To make the changes in the list "ls" I had to first index into the word in the list and then index for the particular desired character, which may look a bit cryptic at first sight but is fairly simple. For indexing into the word in the list "ls" I used the enumerate function in the for loop, all thanks to GFG for the article on "How to loop efficiently in Python", otherwise I would have to use an iterator variable which would have been another headache, and would increase the time and the space complexity of the code. 

The Code for the 2nd problem statement is below:-

def transformSentence(st):
    # Write your code here
    '''
    Input: str
    Output: str
    '''
    ls=st.split(" ")
    # print(ls)
    for ind,word in enumerate(ls):
        # print(word)
        for i in range(1,len(word)):
            '''
            1) Instead of changing 'word' variable change the list
            2) 'word' var is equivalent to ls[ind]
            3) ord() is used to get the ascii value 
            4) Making every char lowerCase cause we 
               had to check w.r.t. the order in alphabet
               not w.r.t. ascii value
            The comments used while Debugging to see the flow of the code
            '''
            # print(word[i],end=" ").
            prev_asc=ord(ls[ind][i-1].lower())
            curr_asc=ord(ls[ind][i].lower())
            # print(prev_asc,curr_asc)
            print("current char is",ls[ind][i])
            if(prev_asc<curr_asc):
                
                # print(f"ls[{ind}] changed to {word[:i]+word[i].upper()
                +word[i+1:]}")
                # print("badalna")
                ls[ind]=ls[ind][:i]+ls[ind][i].upper()+ls[ind][i+1:]
                # word=str(word[:i]+word[i].upper()+word[i+1:]) 
                # print(f"ls inside if cond. {ls}")
                
            elif(prev_asc>curr_asc):
                # print(f"ls[{ind}] changed to {word[:i]+word[i].lower()
                +word[i+1:]}")
                # print("changing")
                ls[ind]=ls[ind][:i]+ls[ind][i].lower()+ls[ind][i+1:]
                # word=str(word[:i]+word[i].lower()+word[i+1:])
            else:
                # print("kuch nhi")
                pass
            # print("Outside Cond.",ls)
        # print(ls)
    # print(st)
    # print("Printing ls",ls)
    return " ".join(ls)

The Code for the 1st problem statement is below:-

# write your code here
def average(ls):
    sum=0
    for elem in ls:
        sum+=elem
    return sum/len(ls)
def avg(*args):
    return float(average(args))  

Comments

Popular Posts