Question
Python-Given a list of distinct integers from 0 to a value MAX_VALUE, write a function to produce a string that describes the ranges of numbers
Python-Given a list of distinct integers from 0 to a value MAX_VALUE, write a function to produce a string that describes the ranges of numbers missing from the list. The items in the result should be sorted in ascending order and separated by commas. When a gap spans only one number, the item is the number itself; when a gap is longer, the item comprises the start and the end of the gap, joined with a minus sign.
empty array is not possible - (note first entry could be other than 0)
more than one missing element
MAX_VALUE will be given as input
example
MAX_VALUE = 1000 VALUES = [0, 1, 2, 50, 52, 75] RESULT = "3-49,51,53-74,76-1000"
Code must start with these two line !
def missingElementStepIII(arr,max_value):
return_value= "";
arr.sort()
#setup start start = arr[0] end = arr[0]
#check for missing elements for i in range(1,len(arr)): if (arr[i] - arr[i-1] > 1): end = arr[i-1] if (end == start): return_value+=str(start) else: return_value+=str(start) + "-" + str(end) start = arr[i] #handle last element in the array if (start != max_value): if (start == arr[-1]): return_value+=","+str(start) else: return_value+=","+str(start)+"-"+str(max_value) return return_value
I get this when I run the code
Name | Arguments | Actual | Expected | |
---|---|---|---|---|
fail | missingElementStepIII | [0, 1, 2, 5, 6, 7, 8, 9], 10 | 000-25-26-27-28-2,9 | 3-4, 10 |
fail | missingElementStepIII | [0, 1, 2, 3, 6, 8, 9, 10], 11 | 0000-3666 | 4-5, 7, 11 |
fail | missingElementStepIII | [0, 1, 4, 5, 6, 7, 9], 9 | 00-14-15-16-17 | 2-3, 8 |
fail | missingElementStepIII | [4, 8, 9, 23, 24], 30 | 444-923-9,24 | 0-3, 5-7, 10-22, 25-30 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started