在具有任意起始范围或从1开始的唯一顺序列表中打印丢失的数字

发布于 2025-01-24 18:34:54 字数 744 浏览 2 评论 0原文

这个问题类似于我如何在Unix终端中的唯一和顺序列表中找到缺失的整数?

不同之处在于,我想知道是否可以指定列表的起始范围,

我注意到了以下提供的解决方案:

awk'{for(i = p+1; i< $ 1; $ 1; i ++)print i} {p = $ 1}'file1

and

perl -ne'对$ a+1 .. $ _ -1说。 $ a = $ _'

file1如下:

5
6
7
8
15
16
17
20

运行两个解决方案,它给出以下输出:

1
2
3
4
9
10
11
12
13
14
18
19

请注意,输出从1开始打印。

问题是如何传递任意启动/最低限度,如果什么都没有提供了,假设数字1是起始/最小数字?

9
10
11
12
13
14
18
19

是的,有时您会希望起始号码为1,但有时您将起始数字作为列表中最小的数字。

This question is similar to How can I find the missing integers in a unique and sequential list (one per line) in a unix terminal?.

The difference being is that I want to know if it is possible to specify a starting range to the list

I have noted the following provided solutions:

awk '{for(i=p+1; i<$1; i++) print i} {p=$1}' file1

and

perl -nE 'say for $a+1 .. $_-1; $a=$_'

file1 is as below:

5
6
7
8
15
16
17
20

Running both solutions, it gives the following output:

1
2
3
4
9
10
11
12
13
14
18
19

Note that the output start printing from 1.

Question is how to pass an arbitrary starting/minimum to start with and if nothing is provided, assume the number 1 as the starting/minimum number?

9
10
11
12
13
14
18
19

Yes, sometimes you will want the starting number to be 1 but sometimes you will want the starting number as the least number from the list.

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

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

发布评论

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

评论(4

方圜几里 2025-01-31 18:34:55

您可以使用awk脚本,稍作修改,然后将其传递给初始p带有> -v选项:

$ awk 'BEGIN{p=p<1?1:p} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
1
2
3
4
9
10
11
12
13
14
18
19
$ awk -v p=10 'BEGIN{p=p<1?1:p} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
10
11
12
13
14
18
19

开始/code>块初始化p至1,如果未指定或设置为0或负值。循环以p而不是p+1开始,最后一个块分配$ 1+1 p(而不是$ 1),当时且仅当p $ 1 。

假设默认值(1)是您想要的最小启动号。如果您想从0开始,甚至是从负数开始的,只需替换开始{p = p&lt; 1?1:p} by begin oken {p =(p ==“ p ==”“”? 1:p)}

$ awk -v p=-2 'BEGIN{p=(p==""?1:p)} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
-2
-1
0
1
...

You can use your awk script, slightly modified, and pass it an initial p value with the -v option:

$ awk 'BEGIN{p=p<1?1:p} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
1
2
3
4
9
10
11
12
13
14
18
19
$ awk -v p=10 'BEGIN{p=p<1?1:p} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
10
11
12
13
14
18
19

The BEGIN block initializes p to 1 if it is not specified or set to 0 or a negative value. The loop starts at p instead of p+1, and the last block assigns $1+1 to p (instead of $1), if and only if p is less or equal $1.

This assumes that the default (1) is the minimum starting number you would want. If you would like to start from 0 or even from a negative number just replace BEGIN{p=p<1?1:p} by BEGIN{p=(p==""?1:p)}:

$ awk -v p=-2 'BEGIN{p=(p==""?1:p)} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
-2
-1
0
1
...
怪我太投入 2025-01-31 18:34:55

这些单线的轻微变化包括一个起点:

awk

# Optionally include start=NN before the first filename
$ awk 'BEGIN { start= 1 }
       $1 < start { next }
       $1 == start { p = start }
       { for (i = p + 1; i < $1; i++) print i; p = $1}' start=5 file1
9
10
11
12
13
14
18
19
$ awk 'BEGIN { start= 1 }
       $1 < start { next }
       $1 == start { p = start }
       { for (i = p + 1; i < $1; i++) print i; p = $1}' file1
1
2
3
4
9
10
11
12
13
14
18
19

perl

# Optionally include -start=NN before the first file and after the --
$ perl -snE 'BEGIN { $start //= 1 }
             if ($_ < $start) { next }
             if ($_ == $start) { $a = $start }
             say for $a+1 .. $_-1; $a=$_' -- -start=5 file1
9
10
11
12
13
14
18
19
$ perl -snE 'BEGIN { $start //= 1 }
             if ($_ < $start) { next }
             if ($_ == $start) { $a = $start }
             say for $a+1 .. $_-1; $a=$_' -- file1
1
2
3
4
9
10
11
12
13
14
18
19

Slight variations of those one-liners to include a start point:

awk

# Optionally include start=NN before the first filename
$ awk 'BEGIN { start= 1 }
       $1 < start { next }
       $1 == start { p = start }
       { for (i = p + 1; i < $1; i++) print i; p = $1}' start=5 file1
9
10
11
12
13
14
18
19
$ awk 'BEGIN { start= 1 }
       $1 < start { next }
       $1 == start { p = start }
       { for (i = p + 1; i < $1; i++) print i; p = $1}' file1
1
2
3
4
9
10
11
12
13
14
18
19

perl

# Optionally include -start=NN before the first file and after the --
$ perl -snE 'BEGIN { $start //= 1 }
             if ($_ < $start) { next }
             if ($_ == $start) { $a = $start }
             say for $a+1 .. $_-1; $a=$_' -- -start=5 file1
9
10
11
12
13
14
18
19
$ perl -snE 'BEGIN { $start //= 1 }
             if ($_ < $start) { next }
             if ($_ == $start) { $a = $start }
             say for $a+1 .. $_-1; $a=$_' -- file1
1
2
3
4
9
10
11
12
13
14
18
19
俏︾媚 2025-01-31 18:34:55

使用Raku(以前称为Perl_6)

raku -e 'my @a=lines.map: *.Int; .put for (@a.Set (^) @a.minmax.Set).sort.map: *.key;'

样本输入:

5
6
7
8
15
16
17
20

样本输出:

9
10
11
12
13
14
18
19

这是在Raku中编码的答案,Raku是编程语言的Perl家族的成员。不,它没有解决OP的可定义起点的请求。取而代之的是上面的代码是一个通用解决方案,该解决方案计算输入的min imum int并从那里计数,返回任何丢失的int s uf-of- - 到输入的max imum int

真的需要用户定义的下限吗?尝试以下代码,该代码允许您设置$ init变量:

~$ raku -e 'my @a=lines.map: *.Int; my $init = 1; .put for (@a.Set (^) ([email protected]).Set).sort.map: *.key;' 
1
2
3
4
9
10
11
12
13
14
18
19

有关说明和较短的代码(包括单线返回和/或返回而无需排序),请参见下面的链接。

https://stackoverflow.com/a/72221301/7270649

https://raku.org

Using Raku (formerly known as Perl_6)

raku -e 'my @a=lines.map: *.Int; .put for (@a.Set (^) @a.minmax.Set).sort.map: *.key;'

Sample Input:

5
6
7
8
15
16
17
20

Sample Output:

9
10
11
12
13
14
18
19

Here's an answer coded in Raku, a member of the Perl-family of programming languages. No, it doesn't address the OP's request for a user-definable starting point. Instead the code above is a general solution that computes the input's minimum Int and counts up from there, returning any missing Ints found up--to the input's maximum Int.

Really need a user-defined lower limit? Try the following code, which allows you to set a $init variable:

~$ raku -e 'my @a=lines.map: *.Int; my $init = 1; .put for (@a.Set (^) ([email protected]).Set).sort.map: *.key;' 
1
2
3
4
9
10
11
12
13
14
18
19

For explanation and shorter code (including single-line return and/or return without sort), see the link below.

https://stackoverflow.com/a/72221301/7270649
https://raku.org

梦毁影碎の 2025-01-31 18:34:55

不像我希望的那样优雅:

< file | mawk '
         BEGIN {    _= int(_)^(\
               ( ORS = "")<_) 
         }  { ___[ __= $0 ] }
         END { 
            do { 
                print _ in ___ \
                      ? "" : _ "\n" 
            } while(++_ < __) }' \_=10          
10
11
12
13
14
18
19

not as elegant as i hoped :

< file | mawk '
         BEGIN {    _= int(_)^(\
               ( ORS = "")<_) 
         }  { ___[ __= $0 ] }
         END { 
            do { 
                print _ in ___ \
                      ? "" : _ "\n" 
            } while(++_ < __) }' \_=10          
10
11
12
13
14
18
19
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文