반응형
Description:
A stream of data is received and needs to be reversed.
Each segment is 8 bits long, meaning the order of these segments needs to be reversed, for example:
11111111 00000000 00001111 10101010
(byte1) (byte2) (byte3) (byte4)
should become:
10101010 00001111 00000000 11111111
(byte4) (byte3) (byte2) (byte1)
The total number of bits will always be a multiple of 8.
The data is given in an array as such:
[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0]
Note: In the C and NASM languages you are given the third parameter which is the number of segment blocks.
Solution:
1. 배열을 8로 나눈 수만큼 반복한다.
2. 뒤에서부터 8개 요소씩 배열로 만든다.
3. 하나의 배열로 합쳐 반환한다.
def data_reverse(data):
return sum([data[i*-8:] if i == 1 else data[i*-8:(i-1)*-8] for i in range(1, len(data)//8+1)],[])
Best Practices:
def data_reverse(data):
return sum([data[i:i+8] for i in range(len(data)-8, -1, -8)],[])
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
Sum of Digits / Digital Root (0) | 2022.03.23 |
---|---|
Dashatize it (0) | 2022.03.22 |
Persistent Bugger (0) | 2022.03.20 |
자료구조 _ Hash table (0) | 2022.03.20 |
자료구조 _ Queue 그리고 Stack (0) | 2022.03.20 |