[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Daily Programming Challenge

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 74
Thread images: 8

File: 1486453556217.png (289KB, 540x438px) Image search: [Google]
1486453556217.png
289KB, 540x438px
Write a program that generates directory tree in console.


Bonus points for ascii tree design.
Example:
tree SolidOak/
SolidOak/
├── build.rs
├── Cargo.lock
├── Cargo.toml
├── README.md
├── resources
│ ├── soak
│ ├── soakrc
│ ├── SolidOak.desktop
│ └── solidoak.svg
├── screenshot.png
├── src
│ ├── builders.rs
│ ├── ffi.rs
│ ├── main.rs
│ ├── projects.rs
│ ├── ui.rs
│ └── utils.rs
└── UNLICENSE
>>
cmon anon

@echo off

tree
>>
>>59057359
cmon anon
tree
>>
tree.sh
#!/bin/bash
tree
>>
>>59057340
>its another pajeet wants his homework done episode
>>
tree /a /f
>>
You said there would be a trig-related challenge today.
>>
>>59057340
How come the challenges aren't daily?
>>
>>59057568
probably a different anon who made this thread.
>>
bumping thread, may do this, and I don't want it to die.
>>
>>59057629
they are daily in spirit
>>
>>59057340
print """
/\
/\*\
/\O\*\
/*/\/\/\
/\O\/\*\/\
/\*\/\*\/\/\
/\O\/\/*/\/O/\
||
||
||
"""
>>
(import (ice-9 ftw) (ice-9 rdelim))

(define* (print-trees trees #:optional (depth 0))
(define (print-prefix)
(if (> depth 0)
(begin
(map (lambda x (display "│ ")) (iota (- depth 1)))
(if (null? (cdr trees))
(display "└── ")
(display "├── ")))))
(if (not (null? trees))
(begin
(print-prefix)
(display (caar trees)) (newline)
(print-trees (cddar trees) (+ depth 1))
(print-trees (cdr trees) depth))))

(set-port-encoding! (current-output-port) "UTF-8")
(print-trees (list (file-system-tree (read-line) (const #t))))
>>
>>59058595
is this racket?
>>
>>59058654
It's Guile.
>>
File: tree.png (39KB, 1448x846px) Image search: [Google]
tree.png
39KB, 1448x846px
org 0x100
use16
mov ah, 0x47
xor dx, dx
mov si, starting_dir
int 0x21
xor ch, ch
mov cl, [0x80]
test cx, cx
jz @f
mov al, 0x0d
mov di, 0x82
cld
repne scasb
mov byte [di-1], 0
mov ah, 0x3b
mov dx, 0x82
int 0x21
jc exit
@@: mov bx, 1
search:
mov ah, 0x4e
mov cx, 0x37
mov dx, search_pattern
int 0x21
cmp bx, 1
jbe @f
mov cx, bx
.s: mov ah, 0x4f
int 0x21
loop .s
jnc @f
cmp [level], 1
je exit
mov ah, 0x3b
mov dx, previous_dir
int 0x21
dec [level]
ret
@@: cmp byte [0x9e], '.'
je .n
mov dl, '-'
mov cx, [level]
shl cx, 1
mov ah, 0x02
@@: int 0x21
loop @b
inc bx
xor ax, ax
mov di, 0x9e
mov cx, 14
repne scasb
mov word [di-1], 0x0d0a
mov byte [di+1], '$'
mov dx, 0x9e
mov ah, 0x09
int 0x21
test byte [0x95], 0x10
jz .n
mov byte [di-1], 0
mov ah, 0x3b
mov dx, 0x9e
int 0x21
inc [level]
push bx
mov bx, 1
call search
pop bx
jmp search
.n: mov cx, 1
jmp .s
exit:
mov ah, 0x3b
mov byte [starting_dir-1], '\'
mov dx, starting_dir-1
int 0x21
mov ax, 0x4cff
int 0x21

level dw 1
search_pattern db "*.*",0
previous_dir db "..",0
starting_dir rb 64

I may add some pretty ascii later. or maybe not. that would involve a whole new level of complexity.
>>
>>59058694

Is that racket?
>>
>>59058767
No.
https://www.gnu.org/software/guile/
>>
#include <stdio.h>
#include <string>
#include <dirent.h>
#include <sys/types.h>
#include <string.h>

static void lsdir(const std::string &path, int level = 0)
{
DIR *dir = opendir(path.c_str());
struct dirent *dent;

if (!dir)
{
perror("opendir");
return;
}

while (dent = readdir(dir))
{
if (dent->d_name[0] == '.')
{
continue;
}

for (int i = 0; i < level - 1; i++)
{
printf("│ ");
}

if (level)
{
printf("├── ");
}

printf("%s\n", dent->d_name);

if (dent->d_type == DT_DIR)
{
lsdir(path + '/' + std::string(dent->d_name), level + 1);
}
}

closedir(dir);
}

int main()
{
lsdir(".");
}


Quick shitty attempt.
>>
>>59057340

I've done something like this before, and if I remember right, going three directories deep made things harder. At that point, there needs to be some white space between your symbols, so you can't just repeat '|' until you get up by the start of the names.

For example, if "solidoak.svg" was a directory with its own sub-directories, you wouldn't continue the line down between those sub-directories and the root directory line, because solidoak.svg is at the end of the list it belongs to.
>>
>>59059086
>C++
Why? You almost had proper, elegant C there, and then you go fuck it up with your STDs for no reason.
>>
>>59059664

Like this to make things clearer:

tree SolidOak/
SolidOak/
├── build.rs
├── Cargo.lock
├── Cargo.toml
├── README.md
├── resources
│ ├── soak
│ ├── soakrc
│ ├── SolidOak.desktop
│ └── solidoak
│ ├── item1.svg
│ ├── item2.svg
│ └── item3.svg
├── screenshot.png
├── src
│ ├── builders.rs
│ ├── ffi.rs
│ ├── main.rs
│ ├── projects.rs
│ ├── ui.rs
│ └── utils.rs
└── UNLICENSE
>>
#!/usr/bin/env powershell
Get-ChildItem -recurse
>>
>>59059805
>#!/usr/bin/env powershell
delet dis
>>
>>59059887

What's wrong with a shebang directive at the beginning of a powershell script? Powershell is available on Linux now. Might as well make it executable...
>>
Can't post an image, it's got color. OOOOH!
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Directories; use Ada.Directories;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
with Ada.Command_Line; use Ada.Command_Line;

procedure Filesystem is

Global_Indent : Unbounded_String := To_Unbounded_String("");

Current_Directory : String := Argument(1);

procedure Get_Files (Current_Directory : in String) is
Current_Search : Search_Type;
Dir_Entry : Directory_Entry_Type;
begin
Start_Search(Current_Search, Current_Directory, "");
while More_Entries(Current_Search) loop
Get_Next_Entry(Current_Search, Dir_Entry);
if Simple_Name(Dir_Entry) /= "." and
then Simple_Name(Dir_Entry) /= ".." and
then Kind(Dir_Entry) = Directory then
Put(Global_Indent);
Put_Line(Simple_Name(Dir_Entry));
if Length(Global_Indent) = 0 then
Global_Indent := Global_Indent & "|-->";
else
Global_Indent := "| " & Global_Indent;
end if;
Get_Files(Full_Name(Dir_Entry));
Delete(Global_Indent, 1, 4);
elsif Kind(Dir_Entry) = Ordinary_File then
Put(Global_Indent);
Put_Line(ASCII.esc & "[94m" & Simple_Name(Dir_Entry) & ASCII.esc & "[0m");
end if;
end loop;
end Get_Files;

begin

Get_Files(Current_Directory);

end Filesystem;
>>
>>59060005

Pretty dense with no comments, but it seems alright.
>>
#! /usr/bin/env racket
#lang racket
(require racket/cmdline)

;;; repeat a the given string 'n' times
(define (string-repeat s n)
(string-append* (build-list n (const s))))

;;; parse command line options
(command-line
#:args dirs
; traverse function
(define (trav dir [indent 0])
(printf "~a/\n" (last (explode-path dir)))
(let ([lst (directory-list dir)])
(for ([child (in-list lst)]
[i (in-naturals 1)]
; skip hidden files
#:unless (regexp-match #px"^\\." (path->string child)))
; build full path out of name of child
(let* ([fullpath (build-path dir child)]
[dir? (directory-exists? fullpath)])
; display prefix
(printf "~a~a\u2500\u2500 "
(string-repeat "\u2502 " indent)
(if (and (not dir?)
(= i (length lst)))
"\u2514" "\u251c"))
; recur if directory, or just show file name
(if dir?
(trav fullpath (add1 indent))
(displayln child))))))
; call on all arguments
(for-each trav
(if (empty? dirs)
; or, use cd if no arguments
'(".")
dirs)))
>>
>>59060061
>seems alright
it couldn't be cleaner, other than the necessary escape characters.
>>
>>59057340
>>59058741

>>>http://pastebin.com/SYy0ywJP
Now eligible for bonus points. But too long to fit in one post. Also fixed a bug where it would print a directory twice (see screenshot >>59058741)
As far as I can tell, there's no way to know ahead of time if the current entry will be the last one in the directory, so I ended up storing the previous entry and adjusting the prefix as needed.

pic won't upload for some reason
>>
>>59058595
+1 for Lisp
>>
>>59058767
This is racket: >>59060108
>>
>>59059689
std::string is far more elegant than the crap I would of had to write if it was in C.
>>
>>59060393
pic still won't upload
>>>http://i.imgur.com/r6s5Xak.png
>>
>>59059086
Faggot
>>
>>59061043
?
>>
>>59057340
Who is this semen demon?
>>
Needing to ask the OS for directory info means I don't get to use my own dumb language. Have one in befunge instead:
           v
8y&
9u4%d
%&^652f
op$!*is$1
a2*_!1%fAg^
ov<@0=-z@%(g*
6&>^3)(kgI93jf3
<385jakr$v;#$^af;
3|;jf485;<<;9196#b;
^y>2:*::+*5:+:*1+::2^
*1-+:2+;6n;vz849z9a31>7
=
@
#
|


There's a bug in my program. It can't handle directory paths with spaces in them. I only found the bug after going through the trouble to make it shaped like a tree so I'm going to leave it like that because I'm lazy.

If you need an interpreter, cfunge is probably your best bet. run it like
$ cfunge tree.bf ~/path/to/directory
>>
>>59061284
I uh either take advantage of some behavior of cfunge specifically or a use a hacky thing to get around a bug that at the very least doesn't work in pyfunge. Maybe it's cross platform. I'm a little confused about how the y command works to be honest, getting the command line args on to the stack was way trickier than I thought it would be
>>
const fs = require('fs');

const tree = (path = './', abs = '.', prevBranch = '', nextBranch = '') => {
console.log(prevBranch + nextBranch + path);

if (!fs.statSync(abs).isDirectory()) return;

fs.readdirSync(abs).forEach((file, idx, files) => tree(
file,
`${abs}/${file}`,
`${nextBranch ? `│ ${prevBranch}` : ''}`,
`${files.length - 1 === idx ? '└──' : '├──'}`
));
};

tree();
>>
File: pepe_classic.jpg (27KB, 600x600px) Image search: [Google]
pepe_classic.jpg
27KB, 600x600px
>wake up at 7am everday.
>had really good breakfast.
>then go to he libary
>learn new things with lots of books and tutorials
>mfw i understand everthing just so well and everyday is gettin better than the yesterday
>>
>>59062262
You're still a NEET
>>
File: stupid frog pictures.jpg (53KB, 800x331px) Image search: [Google]
stupid frog pictures.jpg
53KB, 800x331px
>>59062313
no i am not a neet
>>
File: 1457357325565.jpg (206KB, 800x1200px) Image search: [Google]
1457357325565.jpg
206KB, 800x1200px
>>59062262
wagecucks BTFO
>>
>>59062425
he didn't say he is self employed, from his post I would say he is a (uni?) student.
>>
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <ftw.h>

#define error(...) \
do { \
fprintf(stderr, __VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)

int node_print(const char *fpath, const struct stat *sb,
int typeflag, struct FTW *ftwbuf)
{
if (ftwbuf->level > 0) {
for (int i = 0; i < ftwbuf->level; i++)
printf("| ");

if (ftwbuf->level > 0)
printf("+-- ");
}

size_t len = strlen(fpath) + 1;
char filebuf[len];

strncpy(filebuf, fpath, len);
puts(basename(filebuf));

return 0;
}

int main(int argc, char **argv)
{
for (int i = 1; i < argc; i++) {
if (nftw(argv[i], node_print, 128, 0) < 0)
error("%s: error: %s: %s\n", basename(argv[0]), argv[i], strerror(errno));
}

return 0;
}


$ ./ftw ~/tmp/test
test
| +-- a
| | +-- a.txt
| | +-- c.txt
| | +-- b.txt
| +-- b
| | +-- a.txt
| | +-- c.txt
| | +-- b.txt
| +-- c
| | +-- a.txt
| | +-- c.txt
| | +-- b.txt
>>
>>59062764
Oops, ignore the the second if (ftwbuf->level > 0).

Changed it at the last second and forgot to remove it.
>>
>>59062425
>arial
not using helvetica
>>
> tree.py
Folder PATH listing for volume Data G4TB
Volume serial number is XXXX-XXXX
N:.
├───.hg
│ ├───cache
│ └───store
│ └───data
│ └───src
└───src
>>
>mfw g++ doesn't support the filesystem ts yet
>>
>>59060518
>>59060393
that is pretty cool
>>
File: trees.png (530KB, 1366x768px) Image search: [Google]
trees.png
530KB, 1366x768px
not perfect but whatever
>>
File: 2017-02-22-130237_492x606_scrot.png (29KB, 492x606px) Image search: [Google]
2017-02-22-130237_492x606_scrot.png
29KB, 492x606px
>>59061284
>>
>>59065262
Thanks, it's still not perfect though, suffers from the same problem mentioned here: >>59059664 >>59059758. But at least it happens less frequently since file-search syscalls in dos always return directories before files.
The only way to completely avoid that would require storing the entire tree in memory before printing, so you can iterate over it both ways and tell if the current directory has more files before recursing into a new directory.
>>
% mkdir -p a/b/c a/d a/e/f
% find a | sort | perl -lne '$f=(split("/"))[-1]; $n=tr/\///; print "--"x$n,$f'
a
--b
----c
--d
--e
----f

Meh.
>>
>>59062097
fix bug
add color
const fs = require('fs');

const red = str => `\x1b[31m${str}\x1b[0m`;
const blue = str => `\x1b[34m${str}\x1b[0m`;

const tree = (path = './', absPath = '.', prevBranch = '', nextBranch = '') => {
const which = !fs.statSync(absPath).isDirectory();
const color = which ? blue : red;

console.log(prevBranch + nextBranch + color(path));

if (which) return;

fs.readdirSync(absPath).forEach((file, idx, files) => tree(
file,
`${absPath}/${file}`,
`${nextBranch ? nextBranch === '└──'
? `${prevBranch} ` : `${prevBranch}│ `
: ''}`,
`${files.length - 1 === idx ? '└──' : '├──'}`
));
};

tree();
>>
>>59058315

At least make it with a loop
>>
This is from memory, so coud be off...

find -type f .
>>
>>59058315
>python2
>>
>>59061284
>Needing to ask the OS for directory info
Implement Erlang-style ports.
>>
import System.Directory

main = do
dir <- getCurrentDirectory
recDirs 0 dir

recDirs :: Int -> FilePath -> IO ()
recDirs i dir = do
files <- listDirectory dir
forM_ files $ \x -> do
b <- doesDirectoryExist x
if b
then putStrLn ("| " ++ (replicate i '-') ++ x) >> recDirs (i+2) x
else putStrLn $ ("| " ++ replicate i '-') ++ x
>>
>>59069605

import Control.Monad.State.Lazy
import System.Directory

addToList :: State [Int]()
addToList = modify (1:) >>= \x -> return x

add :: Int -> State [Int]()
add x = modify (x:)

multList :: Int -> State [Int] ()
multList x = get >>= \r -> put $ map (x*) r


main = do
dir <- getCurrentDirectory
recDirs 0 dir

recDirs :: Int -> FilePath -> IO ()
recDirs i dir = do
files <- listDirectory dir
forM_ files $ \x -> do
b <- doesDirectoryExist x
putStrLn $ ("| " ++ (replicate i '-') ++ x
when b $ recDirs (i+2) x
>>
>>59067646
>>59069605
>>59069761
>no screenshot
>>
proc tree path {
set xs $path
set offset {}
while {$xs ne {}} {
set x [lindex $xs 0]
set xs [lrange $xs 1 end]
switch $x {
BEGIN_DIR { lappend offset 1 }
END_DIR { set offset [lrange $offset 0 end-1] }
FINAL { lset offset end 0 }
default {
if {[file isdirectory $x]} {
set subdir [glob -nocomplain -directory $x *]
set xs [concat BEGIN_DIR \
[lrange $subdir 0 end-1] \
FINAL \
[lindex $subdir end] \
END_DIR \
$xs]
}
set line ""
foreach y [lrange $offset 0 end-1] {
if {$y} {
append line "| "
} else {
append line " "
}
}
if {$offset ne {}} {
append line "+-- "
}
puts $line[file tail $x]
}
}
}
}
tree [lindex $argv 0]


.
+-- build.rs
+-- Cargo.lock
+-- Cargo.toml
+-- README.md
+-- resources
| +-- soak
| +-- soakrc
| +-- SolidOak.desktop
| +-- solidoak.svg
+-- screenshot.png
+-- src
| +-- builders.rs
| +-- ffi.rs
| +-- foo
| | +-- bar
| +-- main.rs
| +-- projects.rs
| +-- ui.rs
| +-- utils.rs
| +-- www
| +-- grav
| | +-- smack
| +-- holygrail.gif
| +-- index.html
+-- tree.tcl
+-- UNLICENSE
>>
>UNLICENSE
stop being cucks and use the GPL
>>
>>59068930
yeah I rolled my eyes at that anon
>>

import os, sys

def tree(dir, level):
try:
for item in os.listdir(dir):
print(' ' * level + item)
tree(dir + '/' + item, level + 1)
except:
return

tree(sys.argv[1], 0)



No formatting but it's short and sweet
>>
find|sed 's@[^./]*/@ @g'
>>
>>59070546
>short and sweet
That's because it doesn't do the hard thing, which is drawing the lines correctly.
>>
>>59058595
Kono Rakketto desu?
>>
>>59070546
It's also wrong and retarded.
>>
File: Screenshot_2.png (72KB, 1919x1011px) Image search: [Google]
Screenshot_2.png
72KB, 1919x1011px
Goodnuff? Also, what the fuck am I supposed to do about the 3rd level?
>>
>actually doing these challenges
Christ if you do it at least don't post your code
All you're doing is helping Rajesh do his homework
>>
>>59072479
>Anyone's homework is this trivial
Thread posts: 74
Thread images: 8


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.