模式解码二

发布于 2024-12-26 05:55:06 字数 2079 浏览 0 评论 0原文

可能的重复:
模式解码

我对上一篇有关模式解码的文章有一些新问题:

我有几乎相同的数据文件,但是有两个空行,在解码时必须考虑到。 因此,双空行意味着存在街道/灌浆(有关定义,请参阅上一篇文章:模式解码 ),其中有零 (0) 个房子,但我们也必须计算这些类型的模式。 (是的,你可能会认为,这绝对是错误的说法,因为没有一条街道没有至少一栋房子,但这只是一个类比,所以请照原样接受它。)

这是< em>新数据文件,带有双行:

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 

我需要将其转换为一种优雅的方式,就像以前所做的那样,但在这种情况下,我们也必须考虑这些新组,并以这种方式指示它们,如下Kent 例如:roupIdx houseIdx numberOfRooms,其中 houseIdx 等于 0 houseIdx = 0numberOfRooms 也等于 0 numberOfRooms = 0。所以,我需要得到这种输出,例如:

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

我们可以用这种方式调整前面的代码吗?

更新:新的第二个空行表示一个新组。如果空行后面还有一个空的新行,在这种情况下,

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< /代码>。请参阅上面所需的输出!

Possible Duplicate:
Pattern decoding

I have some new question concerning to the previous post about pattern decoding:

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 

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

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情何以堪。 2025-01-02 05:55:06

尝试:

$ 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

Try:

$ 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"
    }
}

Command:

awk -f houses.awk houses

Output:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文