0

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.

Abhishek Tirkey
  • 435
  • 1
  • 6
  • 12

1 Answers1

1

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)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • Thanks, but I tried making the change and it still shows the same error: Traceback (most recent call last): File "", line 1, in main() File "/Volumes/Macintosh HD 2/Academic work/CST - 501 (Coding Assignments)/Coding/Problems/isAnagram.py", line 52, in main "Operation required %10.7f seconds" % isAnagram1(str1, str2) TypeError: not all arguments converted during string formatting – Abhishek Tirkey Jun 10 '15 at 20:29
  • @ÀbhìshékTìrkey you use one value in the formatting string and return a tuple of 2 values from `isAnagram1` – Peter Wood Jun 10 '15 at 20:33
  • @PeterWood How is that, since I'm using two format strings in the statement: print "Is Anagram1: %b, Operation required %10.7f seconds" % isAnagram1(str1, str2) – Abhishek Tirkey Jun 10 '15 at 20:37
  • @ÀbhìshékTìrkey `"Operation required %10.7f seconds"` only has one. – Peter Wood Jun 10 '15 at 21:11