简体   繁体   中英

Using recursion to reverse a list, and count the number of occurances of an element within the list, using python

I'm trying to practice using recursion and lists, by writing a function which takes in a string, splits it into a list, and then uses recursion to produce a reversed version of the string, and count the number of times a search term occurs in the list. In other words, if the user inputs "The quick brown fox", and wants to know how often "duck" occurs in that sentence, the program will out put "fox brown quick The", followed by the number of times "duck is found (in this case, 0). It appears as though my code is working at the moment, however, it won't print out the results of the REVERSE and COUNT functions. Can anyone explain to me why, please?

this function reverses the order of the elements in a string

def REVERSE(strng):
    new_list=''
#base case
    if len(strng)==0:
        return new_list+""
#recursive call
#returns the first element in the string, and adds it to the end of the rest of the string called recursively from the second element
    else:
        new_list+=REVERSE(strng[1:]) + strng[0]
        return new_list
    print (new_list)

function to count the number of occurances of a substring within a string

def COUNT(strng,srch):
        count=0
    #base case
        if len(strng)==0:
            return count
    #recursive call in event search term found in the first element of the list
        elif strng[0]==srch:
            count+=1
            return COUNT(strng[1:],srch)
    #recursive call in event search term not found in first element of list
        else:
            count+=0
            return COUNT(strng[1:],srch)   
        print ("The term" + srch + "occurs" + count + "times")

this is the program which calls on these two functions. I'm keeping them in separate files to practise importing etc

from functions import *
def main():
        terms = input ("Enter the list of terms:\n").split(" ")
    query = input("Enter a query term:\n")
    print("List in reverse order:")
    REVERSE(terms)
    print()
    COUNT(terms, query)
main()

Both REVERSE and COUNT will never execute those print statements - they will always return before execution reaches the print s.

You may want to remove the useless print s from REVERSE and COUNT , and print their return values in main instead:

def main():
terms = input ("Enter the list of terms:\n").split(" ")
query = input("Enter a query term:\n")
print("List in reverse order:")
print(REVERSE(terms))
print()
print(COUNT(terms, query))

So far it seems to me the following may help you.

#!/usr/bin/python

def REVERSE(strng):
    new_list=''
    #base case
    if len(strng)==0:
        new_list+=""
    #recursive call
    #returns the first element in the string, and adds it to the end of the rest of the string called recursively from the second element
    else:
        new_list+=REVERSE(strng[1:]) + strng[0] + " "
    return new_list


def COUNT(strng,srch):
        count=0
        #base case
        if len(strng)==0:
            count = 0
        #recursive call in event search term found in the first element of the list
        elif strng[0]==srch:
            count+=1
            COUNT(strng[1:],srch)
        #recursive call in event search term not found in first element of list
        else:
            count+=0
            COUNT(strng[1:],srch)
        return count

if __name__ == '__main__':
    terms = input ("Enter the list of terms:\n").split(" ")
    query = input("Enter a query term:\n")
    print("List in reverse order: %s" % REVERSE(terms))
    print ("The term '%s' occurs %d times." % (query, COUNT(terms, query)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM