Question

Use the format to solve: def count_consecutive(input_string): output_list = [] #Write your code here return output_list...

Use the format to solve:

def count_consecutive(input_string):
output_list = []
#Write your code here
return output_list

#You may modify the code below for testing
input_string='occurred'
output_list = count_consecutive(input_string)
print(output_list)

Write a Python program which takes an input_string as input parameter and returns an output_list as per the logic below –

  • For each character in the input_string
  • If the character is occurring consecutively,
    • Count the number of consecutive occurrences of the character
    • Concatenate the character and the count of the consecutive occurrences of the character in input_string and add the result to the ouput_list

Example 1: If the input_string is "occurred", the count of consecutive occurrence of characters 'c' and 'r' are 2. Hence the output_list would be ['c2', 'r2']

Example 2: If the input_string is "possessive", the count of consecutive occurrence of character 's' occurring at positions 2 and 5 are 2. Hence the output_list would be ['s2', 's2']

  • If there is no consecutive occurrence of any character in the given input_string add "X" (upper case) to the output_list

Example: In the input_string "Madam", there is no character occurring consecutively. So the output_list would be ["X"]

  • Return output_list

Assumptions:

  • The input_string would have at least one character
  • All the characters in the input_string would be alphabets in lowercase

Note: No need to validate assumptions

Sample Input and Output

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

def count_consecutive(input_string):
   output_list = []
   if(len(input_string)==1):
       output_list.append("X")
       return output_list
   i=0
   while(i<len(input_string)):
       j=i
       count=0
       s=""
       while(j<len(input_string)):
           if(input_string[i]==input_string[j]):
               count=count+1
               s=input_string[i]
               j=j+1
           else:
               break          
       if(count>1):
           s=s+str(count)
           output_list.append(s)
       i=i+1
   if(output_list==[]):
       output_list.append("X")
   return output_list

print(count_consecutive("ouccurred"))
print(count_consecutive("possessive"))
print(count_consecutive("p"))
print(count_consecutive("posesve"))

Add a comment
Know the answer?
Add Answer to:
Use the format to solve: def count_consecutive(input_string): output_list = [] #Write your code here return output_list...
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 your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE...

    Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE MUST BE RECURSIVE. Do not use loops (while, do/while, or for). Do not declare any variables outside of a method. You may declare local variables inside a method. Complete the following method: public static String decompress(String compressedText): Decompress the input text, which has been compressed using the RLE algorithm (previous hw assignment): Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase,...

    A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase, one digit and white space is given as the code down below. So, simiiar to those rules, I need the code for the following questions post in pictures such as no more three consecutive letters of English alphabets, password should not contain User name and so on. //GOAL: To learn how to create that make strong passwords //Purpose: 1)To learn some rules that makes...

  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc"....

    Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

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
ADVERTISEMENT