At Nordic Embedded, a microcontroller receives a stream of sensor flags where each reading is either 0 or 1. Because RAM is limited, write code that packs every 8 flags into one byte and returns the packed result using only constant extra working memory beyond the output array.
Implement a function pack_sensor_flags(flags).
flags of integers, where each value is either 0 or 1[0, 255]0.Example 1
flags = [1,0,1,0,0,0,0,1][161]10100001, which equals 161.Example 2
flags = [1,1,1,1,1,1,1,1, 0,0,0,0][255, 0]11111111 = 255. The remaining 4 flags become 00000000 after padding.0 <= len(flags) <= 10^5flags[i] is either 0 or 1O(1) extra working memory, excluding the returned output listflags = [1,0,1,0,0,0,0,1]Output[161]WhyThe 8 bits form `10100001`, which is `161` in decimal.flags = [1,1,1,1,1,1,1,1,0,0,0,0]Output[255, 0]WhyThe first byte is `11111111 = 255`. The remaining four zeros are padded to `00000000 = 0`.flags = [1,0,1]Output[160]WhyThe bits `101` are padded to `10100000`, which equals `160`.0 <= len(flags) <= 10^5flags[i] is either 0 or 1The earliest flag in each group becomes the most significant bitUse O(1) extra working memory, excluding the returned output listdef pack_sensor_flags(flags):