Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a list of hexadecimal values is sequential or not

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
like image 647
WEshruth Avatar asked Mar 05 '15 18:03

WEshruth


People also ask

How do you find all the elements of a hexadecimal number?

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.

Is it possible to print a hexadecimal number?

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.

What is the approach to hexadecimal numbers?

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:

How to find any consecutive numbers among elements of alist?

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.


1 Answers

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.

like image 97
JuniorCompressor Avatar answered Sep 27 '22 18:09

JuniorCompressor