Finding duplicate words in a string python -
i trying make function locate duplicate words , if output should true or false depending on wether there duplicate words. example:
doubleword("cat") --> false . doubleword("catcat") --> true . doubleword("contour" * 2) --> true
so far have this:
def main(): word = input("enter string: ") half = len(word) >> 1 if word[:half] == word[half:]: print("true") else: print("false") return print(main())
if name == "main": main()
any appreciated. thought maybe using slicing make easier have no idea how implement in code. thanks!
you have compare first part second, can slicing this:
def doubleword(word): return word[len(word) // 2:] == word[:len(word) // 2]
Comments
Post a Comment