如何理解这个表达?

huangapple go评论122阅读模式
英文:

How to understand this expression?

问题

  1. error = (parityList.mapIndexed { i, parity -> parity.toInt() * (2.0.pow(i)).toInt() }).sum()

例如:我输入数字101

变量的值如下所示:

  1. parityList = [1, 0]
  2. i = 1
  3. error = 2

error = 2 是如何计算的呢?可以使用以下示例来解释执行顺序:

程序代码:

  1. val input = "101"
  2. val data = input.map { it.toString().toInt() }.toMutableList()
  3. val cList = mutableListOf<Int>()
  4. var c = 0
  5. val h = mutableListOf<Int>()
  6. val parityList = mutableListOf<Int>()
  7. for (k in data.indices) {
  8. val p = 2.0.pow(c).toInt()
  9. h.add(data[k])
  10. if (p == (k + 1)) {
  11. c++
  12. }
  13. }
  14. for (parity in h.indices) {
  15. val ph = 2.0.pow(parity.toDouble()).toInt()
  16. if (ph == (parity + 1)) {
  17. val startIndex = ph - 1
  18. var i = startIndex
  19. val toXor = mutableListOf<Int>()
  20. while (i < h.size) {
  21. val block = h.subList(i, i + ph)
  22. toXor.addAll(block)
  23. i += 2 * ph
  24. }
  25. for (z in 1 until toXor.size) {
  26. h[startIndex] = h[startIndex] xor toXor[z]
  27. }
  28. parityList.add(h[parity])
  29. }
  30. }
  31. parityList.reverse()
  32. val error = parityList.mapIndexed { i, parity -> parity * (2.0.pow(i)).toInt() }.sum()
  33. println("parityList = $parityList")
  34. println("parityList = ${parityList.reversed()}")
  35. println("i = $i")
  36. println("error = $error")
  37. if (error == 0) {
  38. println("在收到的Hamming码中没有错误")
  39. } else if (error >= data.size) {
  40. println("错误无法检测到")
  41. } else {
  42. println("错误位于第 $error 位")
  43. if (data[error - 1] == 0) {
  44. data[error - 1] = 1
  45. } else if (data[error - 1] == 1) {
  46. data[error - 1] = 0
  47. println("修正Hamming码后: - ")
  48. }
  49. data.reverse()
  50. val correctedData = data.joinToString("").toInt()
  51. println(correctedData)
  52. }

我明白 for i, parityList in enumerate(parityList[::-1]) 函数是如何工作的,但是如何添加 int(parityList) * (2 ** i)

  1. <details>
  2. <summary>英文:</summary>
  3. I am converting Hamming code from python to kotlin. And I almost did everything, but there is one expression that I do not understand.

error=sum(int(parity_list) * (2 ** i) for i, parity_list in enumerate(parity_list[::-1]))

  1. For example: I enter the number 101
  2. The value of the variables are obtained:

parity_list = [1,0]

i = 5

error = 2

  1. How come `error = 2`? Can you explain the execution sequence with an example?
  2. Program code:

coding=windows-1251

from traceback import print_tb

print('Введите полученный код Хэмминга')
d=input()
data=list(d)
data.reverse()
c,ch,j,r,error,h,parity_list,h_copy=0,0,0,0,0,[],[],[]

for k in range(0,len(data)):
p=(2**c)
h.append(int(data[k]))
h_copy.append(data[k])
if(p==(k+1)):
c=c+1

for parity in range(0,(len(h))):
ph=(2**ch)
if(ph==(parity+1)):

  1. startIndex=ph-1
  2. i=startIndex
  3. toXor=[]
  4. while(i&lt;len(h)):
  5. block=h[i:i+ph]
  6. toXor.extend(block)
  7. i+=2*ph
  8. for z in range(1,len(toXor)):
  9. h[startIndex]=h[startIndex]^toXor[z]
  10. parity_list.append(h[parity])
  11. ch+=1

parity_list.reverse()

print('parity_list = ', parity_list)
print('parity_list = ', parity_list[::-1])
print('i = ', i)
error=sum(int(parity_list) * (2 ** i) for i, parity_list in enumerate(parity_list[::-1]))
print("error = ", error)

if((error)==0):
print('В полученном коде Хэмминга нет ошибок')

elif((error)>=len(h_copy)):
print('Ошибка не может быть обнаружена')

else:
print('Error is in',error,'bit')

  1. if(h_copy[error-1]==&#39;0&#39;):
  2. h_copy[error-1]=&#39;1&#39;
  3. elif(h_copy[error-1]==&#39;1&#39;):
  4. h_copy[error-1]=&#39;0&#39;
  5. print(&#39;После исправления код Хэмминга: - &#39;)
  6. h_copy.reverse()
  7. print(int(&#39;&#39;.join(map(str, h_copy))))
  1. I understand how the function `for i, parity_list in enumerate(parity_list[::-1])` works, but how to add this `int(parity_list) * (2 ** i)`?
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. ```python
  6. # 定义列表
  7. parity_list = [1, 0]
  8. # 反转列表
  9. reveresed_parity_list = parity_list[::-1]
  10. # 将错误输出定义为0
  11. error = 0
  12. # 遍历每个元素,获取索引和值,其中i是索引,parity_list_value是值
  13. for i, parity_list_value in enumerate(reveresed_parity_list):
  14. # 如果i = 0,那么2^0 = 1,按照任何值的0次方的规则
  15. error += int(parity_list_value) * (2 ** i)
英文:
  1. # Define the list
  2. parity_list = [1, 0];
  3. # reverse the list
  4. reveresed_parity_list = parity_list[::-1]
  5. # define error output as 0
  6. error = 0
  7. # loop through each element, getting the index and the value where i is the index and parity_list_value is the value
  8. for i, parity_list_value in enumerate(reveresed_parity_list):
  9. # if i = 0 then 2^0 = 1 as the rule for any value to the exponent of 0
  10. error += int(parity_list_value) * (2 ** i)

huangapple
  • 本文由 发表于 2023年2月27日 01:32:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定