Created
January 10, 2017 10:49
-
-
Save hygull/98144aad61b32db5dbbf63d6432c42e0 to your computer and use it in GitHub Desktop.
Write a recursive code to print the number of occurrences of substring "hi" that is not preceded by substring "x" created by hygull - https://repl.it/FEx4/0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| coded_on : 10 January 2017. | |
| aim : Write a recursive code to print the number of occurrences of substring "hi" that is not preceded by substring "x" | |
| coded_by : Rishikesh Agrawani. | |
| """ | |
| def abcd(s): | |
| try: | |
| index=s.index("hi") | |
| if index==0: | |
| return 1+abcd(s[index+1:]) | |
| else: | |
| if s[index-1]!="x": | |
| return 1+abcd(s[index+1:]) | |
| else: | |
| return abcd(s[index+1:]) | |
| except: | |
| return 0 | |
| #Testing with different cases | |
| print abcd("hixhifhjhi") #2 | |
| print abcd("xhixhi") #0 | |
| print abcd("hixhix") #1 | |
| print abcd("hxhixh") #0 | |
| print abcd("hiabcdhiandxhiandchi") #3 | |
| print abcd("dfgxhigopher") #0 | |
| print abcd("optxhixhixhixhiandhihello") #1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment