模式解码II [英] Pattern decoding II
问题描述
可能重复:结果
模式解码
我就到previous后约模式解码了一些新的问题:
I have some new question concerning to the previous post about pattern decoding:
我具有几乎相同的数据文件,但也有双空(空白)线,其中,必须考虑到在解码。
因此,双空行的意思是,有一个街/水泥浆(其定义见previous后:模式解码),其中有零(0)的房子,但我们必须得指望这些种模式。 (是的,你可能会认为,这是绝对错误的说法,因为如果没有至少有一个家没有街道,但是这仅仅是一个比喻,所以请,只是接受它,因为它是。)
I have almost the same data file, BUT there are double empty (blank) lines, which have to be taken into account in the decoding. So, the double empty lines mean that there was a street/grout (for definitions see the previous post: Pattern decoding) in which there was zero (0) house, but we have to count these kind of patterns too. (Yes, you may think, that this is absolutely wrong statement, because there is no street without at least one house, but this is just an analogy, so please, just accept it as it is.)
下面是在的新的的的数据文件,用双行:
Here is the new data file, with the double lines:
0 0 # <--- Group 1 -- 1 house (0) and 1 room (0)
0 0 # <--- Group 2 -- 2 houses (0;1) and 3,2 rooms (0,1,2;0,1)
0 1
0 2
1 0 # <--- house 2 in Group 2, with the first room (0)
1 1 # <--- house 2 in Group 2, with the second room (1)
0 0 # <--- Group 3
0 1 # <--- house 1 in Group 3, with the second room (1)
0 2
0 0 # <--- Group 4
1 0 # <--- house 2 in Group 4, with one room only (0)
2 0
3 0 # <--- house 4 in Group 4, with one room only (0)
0 0 # <--- Group 5
# <--- Group 6 << ---- THIS IS THE NEW GROUP
0 0 # <--- Group 7
# <--- Group 8 << ---- THIS IS THE NEW GROUP
0 0 # <--- Group 9
0 0 # <--- Group 10
我需要将其转换成因为它之前已经做了一个优雅的方式,但在这种情况下,我们必须考虑到这些新的群体也并指示他们这样,下面的的肯特 例如: roupIdx houseIdx numberOfRooms
,其中 houseIdx
让等于零的 houseIdx = 0
和 numberOfRooms
让等于零过的 numberOfRooms = 0
。所以,我需要这样的输出例如:
I need to convert this into an elegant way as it has been done before, but in this case we have to take into account these new groups too, and indicate them in this way, following Kent for example: roupIdx houseIdx numberOfRooms
, where the houseIdx
let equal to zero houseIdx = 0
and the numberOfRooms
let equal to zero too numberOfRooms = 0
. So, I need to get this kind of output for example:
1 0 1
2 0 3
2 1 2
3 0 3
4 0 1
4 1 1
4 2 1
4 3 1
5 0 1
6 0 0
7 0 1
8 0 0
9 0 1
10 0 1
我们能不能调整previous code这样?
Can we tune the previous code in this way?
更新:新的第二个空行表示新的组的。如果在这种情况下,出现了空行后的附加空新的生产线,如
UPDATE: the new second empty line indicates a new group. If there was an additional empty new line after the empty line, as in this case
0 0 # <--- Group 5
# <--- Group 6 << ---- THIS IS THE NEW GROUP
0 0 # <--- Group 7
# <--- Group 8 << ---- THIS IS THE NEW GROUP
我们只把新的空行(第二个在2空行)作为一个新的群体,并指示他们为 GROUP_INDEX 0 0
。见上所需的输出
we just treat the new empty line (the second one in the 2 blank lines) as a new group, and indicate them as group_index 0 0
. See the desired output above!
推荐答案
尝试:
$ cat houses.awk
BEGIN{max=1;group=1}
NF==0{
empty++
if (empty==1) group++
next
}
{ max = ($1 > max) ? $1 : max
if (empty<=1){
a[group,$1]++
} else {
a[group,$1]=-1
}
empty=0
}
END{for (i=1;i<=group;i++){
for (j=0;j<=max;j++){
if (a[i,j]>=1)
print i , j , a[i,j]
if (a[i,j]==-1)
print i, j, 0
}
printf "\n"
}
}
命令:
awk -f houses.awk houses
输出:
1 0 1
2 0 3
2 1 2
3 0 3
4 0 1
4 1 1
4 2 1
4 3 1
5 0 1
6 0 0
7 0 0
8 0 1
这篇关于模式解码II的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!