3

I am trying to make my function locate duplicate words and if so the output should be True or False depending on wether there are duplicate words. For example:

doubleWord("cat") --> False .      
doubleWord("catcat") --> True .   
doubleWord("contour" * 2) --> True

So far I have this:

def main():

    word = input("Enter a string: ")

    half = len(word) >> 1
    if word[:half] == word[half:]:
        print("True")
    else:
        print("False")

    return
    print(main())

if name == "main": main()

Any help would be greatly appreciated. I thought maybe using slicing would make it easier but I have no idea how to implement that in my code. Thanks!

2
  • 7
    What exactly is it you're trying to do? In your example you say that you think "catcat" should return True. But in your code you're splitting the input where there's a space, so "catcat" would be considered a single word. Commented Nov 5, 2016 at 21:45
  • @Batman I am trying to make it recognize that there are repeated words/letters. Commented Nov 5, 2016 at 21:49

1 Answer 1

2

You just have to compare the first part with the second, you can do this with slicing like this:

def doubleWord(word):
    return word[len(word) // 2:] == word[:len(word) // 2]
Sign up to request clarification or add additional context in comments.

2 Comments

nice one liner. I would optimize it to return False if len(word) is odd.
So it works until the input contour * 2. Any ideas why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.