Question

Write a recursive function called abb pattern with a single parameter astr, which is a string....

Write a recursive function called abb pattern with a single parameter astr, which is a string. The function returns True if astr is a string of the form a nb 2n (n a-s followed by 2n b-s, where n is a positive integer) and False otherwise. For example, abb pattern("abb"), abb pattern("aabbbb"), and abb pattern("aaaabbbbbbbb") all return True, but abb pattern("") (parameter is an empty string), abb pattern("abbabb"), and abb pattern("aaabbbbb") all return False.

you may not use any built-in functions other than len, the index operator [] , the slice operator [:], and + (string or list concatenation). You also may not use any loops. Please note: The built-in function in is essentially a loop and may not be used in any of the functions in this question.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:-

#take input from the user.
astr=input()
def abb_pattern(astr):
#the string must have atleast 3 characters to have abb pattern
#base condition if string is empty or doesnt have 'abb' then no abb pattern exist
if(len(astr)<3):
return False
#base condition the string sliced is abb then there is a abb pattern
if(len(astr)==3 and astr=='abb'):
return True
#the first character can never be 'b'
#so it returns false
if(astr[0]=='b'):
return False
#comes here if first character is a it comes here
#if last 2 characters are bb we slice first one character and last 2 characters
#[-2:] slices last 2 characters of the string
if(astr[-2:]=="bb"):
#recursive call that slices first 1 'a' and last 2 'bb'
return abb_pattern(astr[1:-2])
#if not in the above conditions comes here and that means the string is not in the pattern
return False
#printing the value of the function
print(abb_pattern(astr))

CODE ATTACHMENTS:-

OUTPUT:-

We use the base conditions to write the recursion.

We use slice[:] to minimise the string and send the recursive call.

We use astr[-2:] to get the last 2 characters.

For any queries please do comment.

Please like it.

Thank you.

Add a comment
Know the answer?
Add Answer to:
Write a recursive function called abb pattern with a single parameter astr, which is a string....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a recursive function (static method) called removeCopies that takes a String as a parameter and...

    Write a recursive function (static method) called removeCopies that takes a String as a parameter and replaces any copies of the same character with a single occurrence of the character. Your function must be recursive (no loops). For example: removeCopies(“abccd”) => “abcd” removeCopies(“xxxxxxx”) => “x” removeCopies(“xxxyyzaaxxx”) => “xyzax”

  • 1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat,...

    1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat, a recursive function that takes two alphabetically-ordered strings and merges them, leaving the letters in alphabetical order. Note that the strings may be different lengths. a main method that inputs two ordered strings and calls the orderedConcat method to return a resulting merged, ordered string. Your main method should print the two input strings and the result. Note: You may not use any of...

  • Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function...

    Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function that takes in a string containing some combination of letters, numbers, and spaces, and return the starting index of the first instance of “waldo” contained in that string. If the string does not contain “waldo”, return -1. This function IS case sensitive, so “waldo” is not the same as “WALDO”. Code using string functions such as .index() and .find() that oversimplify the problem will...

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • Write a recursive function, take a String as input, return true, if the String is a...

    Write a recursive function, take a String as input, return true, if the String is a palindrome; false otherwise. For instance, if the input is: “A nut for a jar of tuna” Then the return value is true. Notice that the non English letters are ignored; the spaces are ignored; and it is NOT case sensitive. You must write recursive function. You shall turn in a complete program, including main function to use your function to test if a String...

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

  • Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified...

    Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified letter in a string. This function has to be recursive; you may not use loops!   CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them. For example: Test Result text = 'welcome' letter = 'e' result = freq_of(letter, text) print(f'{text} : {result} {letter}') welcome : 2 e and A list can be...

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • PYTHON 3.6 (Short and Simple Codes Please) Write a recursive function, reverse, that accepts a parameter...

    PYTHON 3.6 (Short and Simple Codes Please) Write a recursive function, reverse, that accepts a parameter containing a string value and returns the original string in reverse. For example, calling reverse('goodbye') would return 'eybdoog'. Reversing a string involves: No action if the string is empty or only has 1 character Concatenating the last character with the result of reversing the string consisting of the second through next-to-last character, followed by the first character

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT