Given a string containing uppercase characters (A-Z), compress the string using Run Length encoding. Repetition of character has to be replaced by storing the length of that run.


Write a python function which performs the run length encoding for a given String and returns the run length encoded String.

Provide different String values and test your program

Sample Input

Expected Output

AAAABBBBCCCCCCCC

4A4B8C

AABCCA

2A1B2C1A


def encode(message):
output=''
privious=message[0]
i=1
count=1
while len(message)>i:
if privious==message[i]:
count+=1
else:
output+=str(count)+privious
count=1
privious=message[i]
if i==len(message)-1:
output += str(count) + privious
i+=1
if len(message)==1:
output += str(count) + privious
return output
#Provide different values for message and test your program
encoded_message=encode("ABBBBCCCCCCCCAB")
print(encoded_message)

A teacher is conducting a camp for a group of five children. Based on their performance and behavior during the camp, the teacher rewards them with chocolates.

Write a Python function to

1. Find the total number of chocolates received by all the children put together.
Assume that each child is identified by an id and it is stored in a tuple and the number of chocolates given to each child is stored in a list.

2. The teacher also rewards a child with few extra chocolates for his/her best conduct during the camp.

  • If the number of extra chocolates is less than 1, an error message "Extra chocolates is less than 1", should be displayed.

  • If the given child Id is invalid, an error message "Child id is invalid" should be displayed. Otherwise, the extra chocolates provided for the child must be added to his/her existing number of chocolates and display the list containing the total number of chocolates received by each child.

child_id=(10,20,30,40,50)
chocolates_received=[12,5,3,4,6]
def calculate_total_chocolates():
sum = 0
for choco in chocolates_received:
sum = sum + choco
return sum
def reward_child (child_id_rewarded,extra_chocolates):
if extra_chocolates < 1:
print("Extra chocolates is less than 1")
elif child_id_rewarded not in child_id:
print("Child id is invalid")
else:
length = len(child_id)
for num in range(length-1):
if child_id_rewarded == child_id[num]:
chocolates_received[num] = chocolates_received[num] + extra_chocolates
print(chocolates_received)
print(calculate_total_chocolates())
#Test your code by passing #different values for child_id_rewarded,extra_chocolates
reward_child(20,2)