Impromptu testing a junior's mettle part 4



  • What an awful solution! Assuming line endings rather than asking the OS for them? Bad, bad developer...



  • @Arantor said:

    Probably, I wasn't attempting to play golf with mine.

    But you could!


  • SockDev

    In PHP?!

    OK, fine.



  • @flabdablet said:

    ```bash
    (head -n1; awk '$3 >= 50' | sort -rnk2) <products.txt

    
    That's pretty short... but I can shorten it
    
    ```bash
    awk 'NR>1&&$3>=50' products.txt|sort -rnk2
    


  • Meanwhile some of us are writing code humans can read!



  • @blakeyrat said:

    Pfft. It's a 1-liner in LINQ.

    It's a one liner in C++ too! http://cpp.sh/2x4ic


  • SockDev

    PHP golfish edition; this time without pretty printing the result.

    <?php
    
    $file = array_filter(array_map(function($line) {
    	list ($product, $price, $qty) = preg_split('/\s+/', trim($line));
    	return [$product, (float) $price, (int) $qty];
    }, file(__DIR__ . '/file.txt')), function($line) {
    	return $line[2] >= 50;
    });
    usort($file, function($a, $b) {
    	return $a[1] > $b[1] ? -1 : ($a[1] == $b[1] ? 0 : 1);
    });
    
    var_dump($file);
    


  • Details, details.



  • More of you horrible 'only unix may look upon my files' people are appearing all the time! Seriously, what kind of idiot assumes line endings?



  • If I was reading the file as opposed to having it hard-coded (a necessity to test it online) `` fstream would have turned the \r\n (or whatever the OS uses as line endings) into a \n before it reached me (I could read it in binary mode and get the raw bytes, but converting line endings makes handling it easier). So the code is portable across OSs.



  • God I always have hated SQL Server's string manipulation.

    IF OBJECT_ID('tempdb..#data') IS NOT NULL DROP TABLE #data;
    
    CREATE TABLE #data(stringData nvarchar(max));
    
    BULK INSERT #data FROM 'Q:\File.txt' WITH (FIRSTROW = 2, TABLOCK);
    
    with q as (
    SELECT s1.product, s3.price, s5.quantity from #data
    cross apply (select SUBSTRING(stringData, 1, CHARINDEX(' ', stringData, 1) - 1)) s1(product)
    cross apply (select LTRIM(SUBSTRING(stringData, len(s1.product) + 1, len(stringData)))) s2(f)
    cross apply (select SUBSTRING(s2.f, 1, CHARINDEX(' ', s2.f, 1) - 1)) s3(price)
    cross apply (select LTRIM(SUBSTRING(s2.f, len(s3.price) + 1, len(s2.f)))) s4(f)
    cross apply (select LTRIM(RTRIM(s4.f))) s5(quantity)
    )
    select * from q
    where quantity >= 50
    order by price desc 
    
    IF OBJECT_ID('tempdb..#data') IS NOT NULL DROP TABLE #data;
    

    0 / 10 would use a format file, ssis, or anything else instead.



  • match(/.+/g) doesn't care what kind of line endings they are. It'll work just as well if they're \r or \r\n. That's why I used it instead of trying to split on some combination of those characters.



  • I'm kinda bored too. I think it should be a prerequisite for the code to handle a file with reordered columns, to throw off all the hardcoded indices. :stuck_out_tongue:

    #!/usr/bin/env python3
    import re
    
    def split(s):
        return re.split(r'\s+', s.strip())
    
    def try_convert(x):
        convert = float if '.' in x else int
    
        try:
            return convert(x)
        except ValueError:
            return x
    
    with open('data-in-a-really-shitty-format.txt', 'r', encoding = 'utf-8') as fp:
        header = split(next(fp))
        rows   = []
    
        for row in fp:
            row = split(row)
            row = map(try_convert, row)
            row = zip(header, row)
            rows.append(dict(row))
    
    for row in sorted(rows, key = lambda x: x['PRICE'], reverse = True):
        if row['QUANTITY'] < 50:
            continue
        print(row)
    


  • Using the standard library I would agree but any interview I've attended included the caveat you can't use the standard library. Writing a linked list usually throws a spanner in the works.



  • @PleegWat said:

    That's pretty short... but I can shorten it

    Your shorter version doesn't show the header line on output.



  • @DogsB said:

    Using the standard library I would agree but any interview I've attended included the caveat you can't use the standard library.

    What the fuck are you interviewing for, rowing a galley?

    Also, how are you supposed to read the file without using the standard library? I guess in C/C++ you can abuse int 13h or something like that... but in Java or C#?



  • Fuck you for making me instinctively try to wipe the smudge from my screen...



  • @CatPlusPlus said:

    I think it should be a prerequisite for the code to handle a file with reordered columns, to throw off all the hardcoded indices.

    I did indeed YAGNI that but if you want it here it is:

    (read h; echo "$h"; read $h x <<<`seq 9`; awk "\$$QUANTITY >= 50" | sort -rnk2) <products.txt
    


  • @PleegWat said:

    I think so far the bash solution won the golf contest.

    That's because bash is a very-high-level text-processing language, and this kind of thing is its exact domain of applicability.



  • Hm, there's that. And I don't think awk can sort without using a lot of code to do it.



  • gawk has inbuilt array sorting functions but no stream sort AFAIK. I think you'd be going some to beat the conciseness of sort -rnk2.



  • @RevCurtisP said:

    I would have to look up the functions online though, which feels like cheating.

    It isn't, I can't remember anything but the most basic .NET namespaces after years of working with the framework.



  • @cartman82 said in Impromptu testing a junior's mettle part 4:

    Also, third world level salary.

    I get Third world level salary for .NET. Maybe I'm TRWTF for living in the third world in the first place. I don't know.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.