Live data from Hacker News

Subverting the software interview (2021)

nliu.net

51–60 of 80 posts

Re: Subverting the software interview (2021)

#51

Earlier quoted context omitted.

I worked with a guy who implemented map and reduce in C++ as a subtask of some other task. It was, of course, a header-only templated monstrosity. He called it "fun.hpp". "Fun" times, indeed, working with him.

You'll like the `std::ranges` library that's been somewhat implemented as of C++20 and is getting some more stuff in C++23. It's very Fun! #include #include #include #include int main() { std::vector vec = {1, 2, 3}; // map, using `std::views::transform`. Implemented in C++20 for(int elem : std::views::transform(vec, [](int x) { return x + 1; })) { std::cout iterator version, which was // implemented in C++20. std::c…

There's a nicer way still:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 

    int main() 
    {
        std::vector vec{1, 2, 3, 4, 5};
        auto plus_one{[](const auto x) { return x + 1; }};
        std::ranges::for_each(vec | std::views::transform(plus_one), [](const auto x) {
            std::cout ()) 
https://godbolt.org/z/84ePjfPhf

Re: Subverting the software interview (2021)

#54

Earlier quoted context omitted.

The name Naomi might have been a dead giveaway.

https://en.wikipedia.org/wiki/Naomi_Uemura This man has won the People's Honour Award for climbing the highest mountains on 5 continents and being the first man to reach the north pole solo. So Naomi is not necessarily a dead giveaway, you guys really split hairs and raise hackles over just about nothing worthwhile. Mistaking someone's sex is not a massive deal. I used to have on my team a guy named Joan (pronounced…

Does finding an instance of a male named Naomi really convince you that Naomi is not typically a female name?

Re: Subverting the software interview (2021)

#56
post #44

There is “no way” to subvert this FizzBuzz interview when writing code in Python 3 only. def inf_range(a): while True: yield a a += 1 def take(n, it): return [a for a, b in zip(it, range(n))] def fizzbuzz(): return (a + b or str(c) for a, b, c in zip( (a for _ in inf_range(1) for a in (("", "", "Fizz"))), (a for _ in inf_range(1) for a in (("", "", "", "", "Buzz"))), inf_range(1))) print(take(30, fizzbuzz())) For bon…

I always liked the version with no branching statements. In python it would be roughly:

    def fizz_str(n):
        return "FIZZ"


    def buzz_str(n):
        return "BUZZ"


    def fizz_buzz_str(n):
        return "FIZZBUZZ"


    def to_str(n):
        return str(n)


    indexes = [3, 0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0]


    def fizz_buzz(n):
        funcs = [
            to_str,
            fizz_str,
            buzz_str,
            fizz_buzz_str
        ]
        return funcs[indexes[n % 15]](n)


    for i in range(1, 101):
        print(fizz_buzz(i))

Re: Subverting the software interview (2021)

#57

> You have a tendency to overengineer things. Overengineering is an actual problem. For a tiny example, I'll see things like: enum MAGIC = 67; // explanation ... foo(MAGIC); The use of MAGIC is the only one, and is far removed. A better solution is: foo(67); // explanation because it improves locality. I also see things like an object fleshed out with all kinds of member functions that are never used.

In a code review, I’d given a junior programmer advice to avoid magic constants and use defines instead (c, not c++). Resubmission came back with: #define SEVENTEEN 17 Last I spoke with him, he was a Java instructor.

The truly brilliant thing to do would be

#define SEVENTEEN 16

Re: Subverting the software interview (2021)

#59
post #44

There is “no way” to subvert this FizzBuzz interview when writing code in Python 3 only. def inf_range(a): while True: yield a a += 1 def take(n, it): return [a for a, b in zip(it, range(n))] def fizzbuzz(): return (a + b or str(c) for a, b, c in zip( (a for _ in inf_range(1) for a in (("", "", "Fizz"))), (a for _ in inf_range(1) for a in (("", "", "", "", "Buzz"))), inf_range(1))) print(take(30, fizzbuzz())) For bon…

It seems like I'm missing a joke.

Is this style in homage to the linked-to page? I can't figure out why you're not using itertools nor re-using itertools names. Is it that translating from .. is that Haskell? .. to Python without idiomatic translation the subversion?

  from itertools import islice, cycle

  def fizzbuzz():
    return (a + b or str(c) for c, (a, b) in enumerate(zip(
       cycle(("", "", "Fizz")),
       cycle(("", "", "", "", "Buzz")),
       ), start = 1))

  >>> print(*islice(fizzbuzz(), 0, 30))
  1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
  16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz

Re: Subverting the software interview (2021)

#60
post #56
post #44

There is “no way” to subvert this FizzBuzz interview when writing code in Python 3 only. def inf_range(a): while True: yield a a += 1 def take(n, it): return [a for a, b in zip(it, range(n))] def fizzbuzz(): return (a + b or str(c) for a, b, c in zip( (a for _ in inf_range(1) for a in (("", "", "Fizz"))), (a for _ in inf_range(1) for a in (("", "", "", "", "Buzz"))), inf_range(1))) print(take(30, fizzbuzz())) For bon…

I always liked the version with no branching statements. In python it would be roughly: def fizz_str(n): return "FIZZ" def buzz_str(n): return "BUZZ" def fizz_buzz_str(n): return "FIZZBUZZ" def to_str(n): return str(n) indexes = [3, 0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0] def fizz_buzz(n): funcs = [ to_str, fizz_str, buzz_str, fizz_buzz_str ] return funcs[indexes[n % 15]](n) for i in range(1, 101): print(fizz_buzz(…

Here is another version of similar idea as oneliner. Additional feature - no loops and no conditions.

    fb = lambda n : n == 0 or not fb(n-1) or not print(['{}',"fizz","buzz","fizzbuzz"][((n%3)==0)|(((n%5)==0)*2)].format(n))

    fb(100)
Post reply on HN