Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Help coding simple python function?

I'm supposed to write a recursive function called count_xx(str) that counts the number of times "xx" appears in the input string. The "xx" substrings must not overlap.

1 Answer

Relevance
  • 5 years ago

    Hi Audrey. This is one solution:

    def count_xx(str):

    ... count = 0

    ... found_x = False

    ... for ch in str:

    ... ... if ch=='x':

    ... ... ... if found_x:

    ... ... ... ... count += 1

    ... ... ... ... found_x = False

    ... ... ... else:

    ... ... ... ... found_x = True

    ... ... else:

    ... ... ... found_x = False

    ... return count

    def main():

    ... print (count_xx ("axbxxcxxxx"))

    main()

    #Note: I've used "... " to show indentation. Replace dots with tabs/spaces.

Still have questions? Get your answers by asking now.