Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hygull/98144aad61b32db5dbbf63d6432c42e0 to your computer and use it in GitHub Desktop.

Select an option

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
"""
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