Update all new Tasks

This commit is contained in:
Ingy döt Net 2015-02-20 09:02:09 -05:00
parent 00a190b0a6
commit 91df62d461
5697 changed files with 93386 additions and 804 deletions

View file

@ -0,0 +1,17 @@
Write a program that will list everything in the current folder, similar to the Unix utility “<tt>ls</tt>” [http://man7.org/linux/man-pages/man1/ls.1.html] (or the Windows terminal command “<tt>DIR</tt>”). The output must be sorted, but printing extended details and producing multi-column output is not required.
;Example output
For the list of paths:
<pre>/foo/bar
/foo/bar/1
/foo/bar/2
/foo/bar/a
/foo/bar/b</pre>
When the program is executed in `/foo`, it should print:
<pre>bar</pre>
and when the program is executed in `/foo/bar`, it should print:
<pre>1
2
a
b</pre>

View file

@ -0,0 +1,8 @@
# syntax: GAWK -f UNIX_LS.AWK * | SORT
BEGINFILE {
printf("%s\n",FILENAME)
nextfile
}
END {
exit(0)
}

View file

@ -0,0 +1,29 @@
with Ada.Text_IO, Ada.Directories, Ada.Containers.Indefinite_Vectors;
procedure Directory_List is
use Ada.Directories, Ada.Text_IO;
Search: Search_Type; Found: Directory_Entry_Type;
package SV is new Ada.Containers.Indefinite_Vectors(Natural, String);
Result: SV.Vector;
package Sorting is new SV.Generic_Sorting; use Sorting;
function SName return String is (Simple_Name(Found));
begin
Start_Search(Search, Directory => ".", Pattern =>"");
while More_Entries(Search) loop
Get_Next_Entry(Search, Found);
declare
Name: String := Simple_Name(Found);
begin
if Name(Name'First) /= '.' then
Result.Append(Name);
end if; -- ingnore filenames beginning with "."
end;
end loop; -- now Result holds the entire directory in arbitrary order
Sort(Result); -- nor Result holds the directory in proper order
for I in Result.First_Index .. Result.Last_Index loop
Put_Line(Result.Element(I));
end loop;
end Directory_List;

63
Task/Unix-ls/C/unix-ls.c Normal file
View file

@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
int cmpstr(const void *a, const void *b)
{
return strcmp(*(const char**)a, *(const char**)b);
}
int main(void)
{
DIR *basedir;
char path[PATH_MAX];
struct dirent *entry;
char **dirnames;
int diralloc = 128;
int dirsize = 0;
if (!(dirnames = malloc(diralloc * sizeof(char*)))) {
perror("malloc error:");
return 1;
}
if (!getcwd(path, PATH_MAX)) {
perror("getcwd error:");
return 1;
}
if (!(basedir = opendir(path))) {
perror("opendir error:");
return 1;
}
while ((entry = readdir(basedir))) {
if (dirsize >= diralloc) {
diralloc *= 2;
if (!(dirnames = realloc(dirnames, diralloc * sizeof(char*)))) {
perror("realloc error:");
return 1;
}
}
dirnames[dirsize++] = strdup(entry->d_name);
}
qsort(dirnames, dirsize, sizeof(char*), cmpstr);
int i;
for (i = 0; i < dirsize; ++i) {
if (dirnames[i][0] != '.') {
printf("%s\n", dirnames[i]);
}
}
for (i = 0; i < dirsize; ++i)
free(dirnames[i]);
free(dirnames);
closedir(basedir);
return 0;
}

View file

@ -0,0 +1,3 @@
(def files (sort (filter #(= "." (.getParent %)) (file-seq (clojure.java.io/file ".")))))
(doseq [n files] (println (.getName n)))

6
Task/Unix-ls/D/unix-ls.d Normal file
View file

@ -0,0 +1,6 @@
void main() {
import std.stdio, std.file, std.path;
foreach (const string path; dirEntries(getcwd, SpanMode.shallow))
path.baseName.writeln;
}

View file

@ -0,0 +1,12 @@
256 buffer: filename-buf
: each-filename { xt -- } \ xt-consuming variant
s" ." open-dir throw { d }
begin filename-buf 256 d read-dir throw while
filename-buf swap xt execute
repeat d close-dir throw ;
\ immediate variant
: each-filename[ s" ." postpone sliteral ]] open-dir throw >r begin filename-buf 256 r@ read-dir throw while filename-buf swap [[ ; immediate compile-only
: ]each-filename ]] repeat drop r> close-dir throw [[ ; immediate compile-only
: ls ( -- ) [: cr type ;] each-filename ;

View file

@ -0,0 +1,15 @@
: save-string ( c-addr u -- a )
dup 1+ allocate throw dup >r place r> ;
require ffl/car.fs
: sorted-filenames ( -- car )
0 car-new { a }
[: swap count rot count compare ;] a car-compare!
each-filename[ save-string a car-insert-sorted ]each-filename
a ;
: each-sorted-filename ( xt -- )
sorted-filenames { a } a car-execute [: free throw ;] a car-execute a car-free ;
: ls ( -- )
[: count cr type ;] each-sorted-filename ;

View file

@ -0,0 +1,24 @@
package main
import (
"fmt"
"log"
"os"
"sort"
)
func main() {
f, err := os.Open(".")
if err != nil {
log.Fatal(err)
}
files, err := f.Readdirnames(0)
f.Close()
if err != nil {
log.Fatal(err)
}
sort.Strings(files)
for _, n := range files {
fmt.Println(n)
}
}

View file

@ -0,0 +1,9 @@
import Control.Monad
import Data.List
import System.Directory
dontStartWith = flip $ (/=) . head
main = do
files <- getDirectoryContents "."
mapM_ putStrLn $ sort $ filter (dontStartWith '.') files

2
Task/Unix-ls/J/unix-ls.j Normal file
View file

@ -0,0 +1,2 @@
dir '*' NB. includes properties
> 1 dir '*' NB. plain filename as per task

View file

@ -0,0 +1 @@
Column[FileNames[]]

View file

@ -0,0 +1,3 @@
let () =
Array.iter print_endline (
Sys.readdir Sys.argv.(1) )

View file

@ -0,0 +1 @@
system("dir/b/on")

View file

@ -0,0 +1 @@
system("ls")

View file

@ -0,0 +1 @@
.say for sort ~«dir

View file

@ -0,0 +1,16 @@
>>> import os
>>> print('\n'.join(sorted(os.listdir('.'))))
DLLs
Doc
LICENSE.txt
Lib
NEWS.txt
README.txt
Scripts
Tools
include
libs
python.exe
pythonw.exe
tcl
>>>

1
Task/Unix-ls/README Normal file
View file

@ -0,0 +1 @@
Data source: http://rosettacode.org/wiki/Unix/ls

View file

@ -0,0 +1,3 @@
/*REXX program lists contents of current folder (ala mode UNIX's LS). */
'DIR /b /oN' /*use Windows DIR: sorts & lists.*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,20 @@
#lang racket/base
;; Racket's `directory-list' produces a sorted list of files
(define (ls) (for-each displayln (directory-list)))
;; Code to run when this file is running directly
(module+ main
(ls))
(module+ test
(require tests/eli-tester racket/port racket/file)
(define (make-directory-tree)
(make-directory* "foo/bar")
(for ([f '("1" "2" "a" "b")])
(with-output-to-file (format "foo/bar/~a"f) #:exists 'replace newline)))
(make-directory-tree)
(define (ls/str dir)
(parameterize ([current-directory dir]) (with-output-to-string ls)))
(test (ls/str "foo") => "bar\n"
(ls/str "foo/bar") => "1\n2\na\nb\n"))

View file

@ -0,0 +1 @@
Dir.foreach("./"){|n| puts n}

View file

@ -0,0 +1,17 @@
use std::os;
use std::io::fs;
fn main() {
let cwd = os::getcwd();
let info = fs::readdir(&cwd).unwrap();
let mut filenames = Vec::new();
for entry in info.iter() {
filenames.push(entry.filename_str().unwrap());
}
filenames.sort();
for filename in filenames.iter() {
println!("{}", filename);
}
}

View file

@ -0,0 +1 @@
puts [join [lsort [glob -nocomplain *]] "\n"]