Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works:
- This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times, then the function will calculate n*10+x and adds it to the list.
Example input: “6743672316” In the above string:
- 6 is repeated 3 times. Then the corresponding number to be added to the list is 3*10+6 = 36
- 7 is repeated 2 times: The number to be added to the list 2*10+7 = 27
write a python program
- 4 is repeated once: The number to be added to the list is 1*10+4 = 14
- 3 is repeated 2 times: The number to be added to the list is 2*10+3 = 23
- 2 is repeated once: The number to be added to the list is 1*10+2 = 12
- 1 is repeated once: The number to be added to the list is 1*10+1 = 11
I WROTE THE CODE ALONG WITH THE COMMENTS
CODE:
def convert(word):
list2=[] #empty list
list1=[int(d) for d in word] # take each digit into list1.
list1.sort(); #sort the list1.
i=0
while i<len(list1): #condition i < length of list1.
item=list1[i]
count=1
for j in range(i+1,len(list1)): # for loop used to same
elements.
if(item==list1[j]): #condition same elements.
count =count+1
result=count*10+item #calculate result.
list2.append(result) #add to the list2/
i = i+count
return list2
list2=[]
word=input("Enter word: ") #scan input.
list2=convert(word) #calling function.
print(list2) #print result.
OUTPUT:
SCREENSHOT OF THE CODE:
Get Answers For Free
Most questions answered within 1 hours.