lundi 21 janvier 2019

Need help writing test case for this code

Kindly help me to write test case for this code without using any external library.

Python program to reverse string of the input while keeping the special characters at the same place

    #Returns true if x is an alphabetic character, false otherwise
    def isAlphabet(x):
        return x.isalpha()

    def reverse_string(input_str):
        LIST = toList(input_str)

        #Initialize left and right pointers
        r = len(LIST) - 1
        l = 0

        #Traverse LIST from both ends until 'l' and 'r'
        while l < r:


            #Ignore special characters
            if not isAlphabet(LIST[l]):
                l += 1
            elif not isAlphabet(LIST[r]):
                r -= 1

            #Both LIST[l] and LIST[r] are not special 
            else:
                LIST[l], LIST[r] = swap(LIST[l], LIST[r])
                l += 1
                r -= 1

        return toString(LIST)

    # Utility functions 
    def toList(input_str): 
        List = [] 
        for i in input_str: 
            List.append(i) 
        return List

    def toString(List): 
        return ''.join(List) 

    def swap(a, b): 
        return b, a 

    # Driver code 
    input_str = "adfc_#sin*"
    print "Input string: " + input_str
    input_str = reverse_string(input_str) 
    print "Output string: " + input_str


    input_str = "hon()lo&"
    print "Input string: " + input_str
    input_str = reverse_string(input_str) 
    print "Output string: " + input_str

Hi, can you help me to write unit test for this code. Regards, Aditya

Aucun commentaire:

Enregistrer un commentaire