Want a function / statement, to check whether all the values of mylist
are sequential or not, which is hexadecimal list.
For example:
def checkmylist(mylist):
#code returns True or False
mylist1 = ['03', '04', '05', '06', '07', '08', '09', '0a', '0b', '0c','0d', '0e', '0f']
mylist2 = ['03', '05', '06', '07', '08', '09', '0a', '0b', '0c','0d', '0e', '0f']
checkmylist(mylist1)
#expected to returns pass
checkmylist(mylist2)
#expected to returns fail
Approach: The approach is based on the idea that all the elements of a hexadecimal number lie between characters A to F or between integers 0 to 9. Below are the steps to solve the problem: Iterate over the given string. Check if each character of the string is between characters A to F or between 0 and 9.
Print Yes if it represents a hexadecimal number. Otherwise, print No. Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: The approach is based on the idea that all the elements of a hexadecimal number lie between characters A to F or between integers 0 to 9.
Approach: The approach is based on the idea that all the elements of a hexadecimal number lie between characters A to F or between integers 0 to 9. Below are the steps to solve the problem:
In the below programs we find out if among the elements of Alist, there are any consecutive numbers. The sorted function will rearrange the elements of the list in a sorted order. We then apply the range function taking the lowest and highest numbers form the list using min and max functions.
def checkmylist(mylist):
it = (int(x, 16) for x in mylist)
first = next(it)
return all(a == b for a, b in enumerate(it, first + 1))
With the first statement we convert the hex numbers to a generator of integers. With next(it)
we take the first element of the generator. Then we enumerate the rest of the elements starting the numbering from first + 1
. We conclude that we have a sequential list if the numbering of each element is the same as the element itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With