Question

Write a recursive function block_count(string) to return the number of blocks consisting of the same letter...

Write a recursive function block_count(string) to return the number of blocks consisting of the same letter in the given input string. Example.

>>> block_count ('AABBCCBBDDD')

5

>>>block_count ('GGABAA')

4

Homework Answers

Answer #1

Code in python (code to copy) with explanation in comments

def block_count(string):
    # base case
    if(len(string)<=1):
        return len(string)
    # recursive case
    # If the first character is same as the second character
    # then the answer to this string is same as the answer to 
    # this string being read from index 1
    if(string[0]==string[1]):
        return block_count(string[1:])
    # If the above is false then the answer is 1 plus the answer to 
    # the string being read from index 1
    else:
        return 1 + block_count(string[1:])

# Print test results to console
print("block_count ('AABBCCBBDDD') = ", block_count('AABBCCBBDDD'))
print("block_count ('GGABAA') = ", block_count('GGABAA'))

python code screenshot

Output Screnshot

Know the answer?
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
Similar Questions
Please write a function three(string) that return a string that all three-letter words are replaced by...
Please write a function three(string) that return a string that all three-letter words are replaced by ***.   For example: three("submit only one question per post.  ") return: "submit only *** question *** post. " Please using Python!
Please code in Java Write a recursive method that takes a string and return a string...
Please code in Java Write a recursive method that takes a string and return a string where every character appears twice. For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it.
Write a recursive method to return all possible k permutations of the given String non-zeros number...
Write a recursive method to return all possible k permutations of the given String non-zeros number Sample input : "123" , 2 output : "1-2", "1-3", "2-3", "2-1", "3-1", "3-2" ** Please provide -PSEUDO CODE -UML DIAGRAM
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. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
C++: Write a function that receives a pointer to a character string consisting of only alphabets...
C++: Write a function that receives a pointer to a character string consisting of only alphabets a-z and returns the character with the maximum number of repetitions. If more than one-character repeats same number of times, return the character with smallest alphabetical order. For example, the string “Mississippi” has three repeated characters (i-4, s-4, p-2). Both ‘i’ and ‘s’ repeated 4 times but ‘i’ is alphabetically smaller hence the function should return ‘i’. Do not count repeated blanks, special characters,...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
4. Create a function, which takes as input a string and a letter. Determine whether or...
4. Create a function, which takes as input a string and a letter. Determine whether or not the letter is present in the string. The function should return a logical true/false if the given letter is present/absent. HINT: use find and isempty. Please anyone helping me with this question can you code using Matlab software, please.
Write a recursive function count_digits that counts all the digits in a string. Program: C
Write a recursive function count_digits that counts all the digits in a string. Program: C
Write a recursive method that takes as input a string s and returns a list containing...
Write a recursive method that takes as input a string s and returns a list containing all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For example, the word ‘binary’ is an anagram of ‘brainy’. Note that the output list should not contain duplicates. Java
Write the recursive version of the function decimal which takes in n, a number, and returns...
Write the recursive version of the function decimal which takes in n, a number, and returns a list representing the decimal representation of the number. def decimal(n): """Return a list representing the decimal representation of a number. >>> decimal(55055) [5, 5, 0, 5, 5] >>> decimal(-136) ['-', 1, 3, 6] """
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Your company is thinking of introducing a Bring Your Own Device (BYOD) policy. You have been...
    asked 24 seconds ago
  • Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
    asked 2 minutes ago
  • Suppose the number of cars in a household has a binomial distribution with parameters n =...
    asked 5 minutes ago
  • HR needs some information on the new interns put into a database. Given an id, email,...
    asked 26 minutes ago
  • Problem solving strategies Questions years = input("Enter a number of years and I'll tell you how...
    asked 30 minutes ago
  • Calculate ?Hrxn for the following reaction: CH4(g)+4Cl2(g)?CCl4(g)+4HCl(g) Use the following reactions and given ?H?s. C(s)+2H2(g)?CH4(g)?H=?74.6kJC(s)+2Cl2(g)?CCl4(g)?H=?95.7kJH2(g)+Cl2(g)?2HCl(g)?H=?184.6kJ Express...
    asked 37 minutes ago
  • ASCII (American Standard Code for Information Interchange) has an encoding for every character of the alphabet,...
    asked 51 minutes ago
  • Is home confinement with electronic monitoring a deterrent? Are there negatives to being confined to one’s...
    asked 1 hour ago
  • Social hostility can have severe lasting effects of interperpersonal relationship during our adolescence years, which if...
    asked 1 hour ago
  • - A series RLC circuit has R=15 ?, L=1.5 H, and C=15 ?F. (a) For what...
    asked 1 hour ago
  • TV Circuit has 30 large-screen televisions in a warehouse in Erie and 60 large-screen televisions in...
    asked 1 hour ago
  • Charges q1, q2, q3, and q4 are placed in sequential order at the corners of a...
    asked 1 hour ago