Write an algorithm for the insert_five() function that will insert the number "5" into each position of the integer num and return the largest integer. Do not support negative numbers.
Example input/output:Input into insert_five(): 20
Possible positions: 520, 250, 205 (this line not displayed to user)
Return: 520
def insert_five(num: int):# Your code heredef main():tests = [20, 9991, 2147483647, 1234567, 0, -1, 9]main()
for num in tests:print("Largest possible int from {num}: {result}.format(return
num=num, result=insert_five(num)))
Answer:
def insert_five(num: int):if (num < 0):def main():return "No negatives allowed"# Turn integer into string to
string = str(num)
# Break string number up into a list of each character
character = [char for char in string]
# Counter for while loop to determine each index we're in
i = 0
originalCount = len(character)
# Create a new empty list to append each modified version of character list to
alltogether = []
while i <= originalCount:# Make a temp copy of character for each iteration so as not to modify original and mess up future iterations
tmp = character[:]
# Insert '5' into each index of list
tmp.insert(i, '5')
# Turn each index from string back into an integer
ints = ''.join(tmp)
# Append each tmp list as an index in alltogether list
alltogether.append(ints)
# Print to see how we're doing :)
print(alltogether)
# Add 1 to i to move to next index for new iteration
i += 1
# We're here, babaaayy! Final iteration
if i > originalCount:# Turn each string index back into integer so we can sort
int_list = list(map(int, alltogether))
int_list.sort(reverse=True)
# Print both sorted list and the first index of list to make sure it's the largest
print("SORTED: ", int_list)
print("FINAL: ", int_list[0])
return int_list[0]tests = [20, 9991, 2147483647, 1234567, 0, -1, 9]main()
for num in tests:print("Largest possible int from {num}: {result}".format(return
num=num, result=insert_five(num)))