Formatting a time value

414 views Asked by At

In the following program I am returning a boolean value and a value representing the execution time in a tuple:

import time


# O(n) time
def isAnagram1(s1, s2):
    start = time.time()
    letterlist1 = [0] * 26
    letterlist2 = [0] * 26

    for i in xrange(0, len(s1)):
        pos = ord(s1[i]) - ord('a')
        letterlist1[pos] = letterlist1[pos] + 1

    for i in xrange(0, len(s2)):
        pos = ord(s2[i]) - ord('a')
        letterlist2[pos] = letterlist2[pos] + 1

    for i in xrange(0, 26):
        if letterlist1[i] != letterlist2[i]:
            end = time.time()
            return False, end-start

    end = time.time()
    return True, end-start
    pass


def main():
    str1 = input("Enter string 1: ")
    str2 = input("Enter string 2: ")

    print "Is Anagram1: %s, "
    "Operation required %10.7f seconds" % isAnagram1(str1, str2)

However, while invoking this function I am required to convert the time value into a floating point value in order to print it out appropriately. If not, it gives the error: TypeError: not all arguments converted during string formatting. I guess I have to find a way to get the single value representing the execution time converted.

Any help would be appreciated.

1

There are 1 answers

4
John La Rooy On BEST ANSWER

You are missing "\" for the line continuation. So you have a print statement which will just print the literal "Is Anagram1: %s, ". The next line is a separate expression that Python tries to evaluate.

Also you need to use raw_input since this is Python2

Here is the fixed main function

def main():
    str1 = raw_input("Enter string 1: ")
    str2 = raw_input("Enter string 2: ")

    print "Is Anagram1: %s, " \
    "Operation required %10.7f seconds" % isAnagram1(str1, str2)