Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The challenge is to detect missing integer sequences of files or directories. You have a directory filled with files/directories that are named as integers.

The files/directories are generated from multiple threads yet the job did not complete - there are therefore gaps in the sequence.

The input is two integers a start and an end, and your task is detect the starting integer of the next missing sequences. You may presume that all files and directories in the directory where run have only integer named files or directories.

Acceptable answer forms: functions, code snippets - they must run on the command line.
Acceptable start/end input: included on the command line, env variables/argv are okay, parameters to functions, user input is okay.
Shortest code wins.

Update -- Although I managed to squeeze out this one, there were many interesting answers. The idea in apricotboy's Bash answer was used in part to help me design my 35 Byte Bash answer. Best of luck on the next one.

E.g. Presume files 1,2,3,4,7,8,9,10,18 are present, start is 1, end is 20: 

The output should be: 
5
11
19
share|improve this question
2  
Can I input an array instead of reading my own files? – Leaky Nun Jul 26 at 2:16
5  
It seems a rather pointless extra requirement to me. – Leaky Nun Jul 26 at 2:18
4  
This is absolutely a chameleon challenge - the requirement to take input as file names and/or directories makes this challenge more about working with the filesystem than actually filling in the holes. – Mego Jul 26 at 2:53
14  
You all complain too much whenever there's a challenge requiring any functionalities other than shuffling integers or strings around. – feersum Jul 26 at 2:56
4  
@feersum How is finding missing integers in sequence related to finding files in directory? – Leaky Nun Jul 26 at 4:52

Python 2, 101 bytes

2 bytes thanks to @xnor.

import os
t=-1
for n in range(input(),input()+1):
 if~-os.path.isfile(str(n)):
  if~t+n:print n
  t=n
share|improve this answer
    
I detected 2 trailing spaces after your import os that you should take out. – Value Ink Jul 26 at 5:36
    
@KevinLau-notKenny Thanks. – Leaky Nun Jul 26 at 5:37
    
I think if~-n!=t: can be if~t+n:. – xnor Jul 26 at 5:42
    
That gets the job done, pretty tight for Python. – A.Danischewski Jul 26 at 7:07

Dyalog APL, 25 24 or 36 bytes

Prompts for lower bound, then upper bound.

It seems from comments to other answers that the OP wanted as short sequences as possible.

{⍵/⍨~⍵∊⍨⍵-1}((⍳⎕)~⍳⎕-1)~⍎¨⎕SH'dir/b'

{
    ⍵/⍨ those where it is
    ~ not true
    ⍵∊⍨ that the set contains
    ⍵-1 their predecessor
} of
(
    (⍳⎕) of the integers until n
    ~ except
    ⍳⎕-1 integers until n-1
)~ except
⍎¨ the evaluation of each of
⎕SH'dir/b' the bare list of names in the current directory


Old answer which returns length-1 sequences:

(⍕¨(⍳⎕)~⍳⎕-1)~⎕SH'dir/b'

(
     string representation
    ¨ of each
    (⍳⎕) of the integers until n
    ~ except
    ⍳⎕-1 integers until n-1
)~ except
⎕SH'dir/b' the bare list of files in current directory

Only works on Windows. A cross platform solution:

(⍕¨(⍳⎕)~⍳⎕-1)~0⎕NINFO⍠1⊢'*'

0 just the filename(s)
⎕NINFO of the Native file(s) INFOrmation
⍠1 using wildcards
⊢'*' on all files

share|improve this answer
    
I'm getting (⍕¨(⍳⎕)~⍳⎕-1)~0⎕NINFO⍠1⊢'*' Unknown APL character: ⍠ (U+2360) Non-APL character – A.Danischewski Jul 26 at 6:16
    
@A.Danischewski Which APL are you using? – Adám Jul 26 at 6:17
    
GNU APL version 1.5 – A.Danischewski Jul 26 at 6:19
2  
@A.Danischewski Dyalog APL – Adám Jul 26 at 6:21
2  
@A.Danischewski Specifically version 15.0+, which is free, and downloadable with the link I provided. – Adám Jul 26 at 6:22

Perl 6, 49 bytes

{my $c;.say if .IO.e??($c=0)!!!$c++ for $^a..$^b}

Tried to use flipflops. Didn't manage to :P.

share|improve this answer
    
Can you provide an example of how you call this on the command line? It looks like you may have an extra parenthesis after ($c=0). I managed to get it running without parameters/placeholders: {my $c;my $a=1;my $b=20;.say if .IO.e??($c=0)!!!$c++ for $a..$b}; – A.Danischewski 2 days ago
    
this is a function. just put parentheses with the arguments after the closing }. – ven 2 days ago
    
Okay got it, I don't often use Perl: perl6 -e '{my $c;.say if .IO.e??($c=0)!!!$c++ for $^a..$^b}(1,20)' – A.Danischewski 2 days ago
    
yep :-). hoping to see more people using Perl 6 everywhere, even in golfing! – ven 2 days ago

Ruby, 74 60 bytes

Input is in command line, run it like ruby f.rb 0 20. Only works in current directory.

-1 byte from unpacking the ARGV into variables, and -13 bytes from replacing the select and grep with a set subtraction.

a,b=$*
f=[*a..b]-Dir.glob(?*)
puts f-f.map{|e|"#{e.to_i+1}"}
share|improve this answer
    
That's in-bounds, well done! – A.Danischewski Jul 26 at 3:16

PHP, 64 bytes

<?=implode("\n",array_diff(range($argv[1],$argv[2]),glob("*")));

Run like this:

php -f golf.php 1 20

Note:

  • Only does the current directory.

  • No trailing newline on the output.

  • This requires the <?= to be allowed in php.ini. Which I think is default but I'm not sure.

Bash, 31 bytes

a(){(seq $@;ls)|sort|uniq -u;}

Run as a 1 20. Again, only does the current dir.

Can I submit two? Hope so. This is my first post to Code Golf so I'm not too sure of the etiquette. Hope I'm counting my bytes correctly, too.

share|improve this answer
    
Well done, the Bash answer is uniq-ue! =) – A.Danischewski Jul 26 at 7:05
2  
You can submit multiple solutions, but unless they are trivial derivatives of each other, each solution should be in a separate answer. – Mego Jul 26 at 7:06
    
You're only supposed to print the first element of each missing range, but the PHP code looks like it would print all of the missing numbers, is this correct? – feersum Jul 26 at 7:07
    
Hey apricot boy, it's a decent answer but feersum is correct your code is printing all the missing sequences, the challenge is to print the first of the missing sequences. This challenge simulates a requirement to retry a job that ended prematurely, the next start sequences need to be printed. – A.Danischewski 2 days ago
    
Ah, my bad. I misunderstood it and just aimed to get the output in the question, without reading into it enough. Should I take my submission down? – apricot boy 2 days ago

PowerShell v4+, 62 bytes

param($x,$y)$x..$y|?{$_-notin($a=(ls ".\").Name)-and$_-1-in$a}

Save as a script in the desired directory and call it locally (see example below). Takes input $x and $y and constructs a range .., then pipes that to a Where-Object (the |?{...}) which is basically a filter. Here we're only selecting items where the current element $_ is -notin the .Name collection of the current directory, but the previous element is -in that collection (i.e., only the start of a missing range).

The ls is an alias for Get-ChildItem and is basically what you'd expect. Requires v4 for the encapsulation-select of .Name, otherwise you'd need $a=ls ".\"|select Name.

Example

PS C:\Tools\Scripts\golfing\misd> ls .\

    Directory: C:\Tools\Scripts\golfing\misd

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/26/2016   7:48 AM          8 1
-a---         7/26/2016   7:48 AM         10 10
-a---         7/26/2016   7:54 AM          0 18
-a---         7/26/2016   7:48 AM          8 2
-a---         7/26/2016   7:48 AM          8 3
-a---         7/26/2016   7:48 AM          8 4
-a---         7/26/2016   7:48 AM         10 7
-a---         7/26/2016   7:48 AM          8 8
-a---         7/26/2016   7:48 AM          8 9
-a---         7/26/2016   8:18 AM        365 misd.ps1

PS C:\Tools\Scripts\golfing\misd> .\misd.ps1 1 20
5
11
19
share|improve this answer
up vote -2 down vote accepted

Bash, 71, 70, 68, 66 Bytes

Input is by env variables, starting int s and ending int e, to process 0 to 20: s=0,e=20

for((a=$s;a<=$e;a++)){ [ -d $a ]&&v=$a||{(($a==$v+1))&&echo $a;};}

Bash, 57 Bytes

Input is by env variable s: s=$(seq 1 20)

for a in $s;{ [ -d $a ]&&v=$a||{(($a==$v+1))&&echo $a;};}

Bash, 41, 40, 35 Bytes

Input is by env variable s: s=$(seq 1 20|grep -vxf<(ls))&&e=echo

for a in $s;{((v+1-(v=a)))&&$e $a;}
share|improve this answer
2  
This isn't valid by the rules of your challenge, since it doesn't take a start and end as input. Also, it only handles directories, not files. – Doorknob Jul 26 at 2:50
2  
Even if it were valid, I find it good etiquette to wait a day or so to post a solution to your own challenge, so as not to discourage those who want to participate in the same language. – xnor Jul 26 at 2:58
    
Its files or directories and it doesn't literally have to ask for input, that's what the input to the program is however it gets there. The case above is fine the code could be written anyhow you want as long as it is executable on the command line. – A.Danischewski Jul 26 at 3:04
3  
How do you even grab input with those variables? Standard inputs are things like STDIN, command line, or function arguments, and I don't think this is any of those. Better to use command line inputs $1 and $2 instead and it should probably work for zero extra cost. – Value Ink Jul 26 at 3:32
1  
Putting aside the fact that your second version doesn't even work (it shows all missing files, not just the starts of ranges), that's still not a valid input format. – Doorknob 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.