Live data from Hacker News

Why it’s hard for programmers to write a program to flatten a list?

shekhargulati.com

91–100 of 128 posts

Re: Why it’s hard for programmers to write a program to flatten a list?

#91
As nobody seems to have posted a Java solution yet:

  import java.util.List;
  import java.util.stream.Collectors;
  import java.util.stream.Stream;
  import static java.util.Arrays.*;
  
  public class Flatten {
  
      public static void main(String[] args) {
          List nested = asList(1, asList(2, 3), asList(4, asList(5,6)));
          List flat = flatten(nested);
          System.out.println(nested);
          System.out.println(flat);
      }
  
      public static List flatten(List nested) {
          return nested.stream()
                  .flatMap(x -> x instanceof List ? flatten((List) x).stream() : Stream.of(x))
                  .collect(Collectors.toList());
      }
  }

I would say it's a bit of an odd question if you mainly target Java as it's very uncommon to encounter such a construct there, alone because the type system can't handle it properly.

Re: Why it’s hard for programmers to write a program to flatten a list?

#92
post #34

Earlier quoted context omitted.

Doesn't stop people from asking these questions of people at all levels. I still get asked these coding questions pretty much every interview I have had, and I have almost a decade of programming experience, and have even been lead programmer on some projects (without the title). I've been asked a barrage of these questions for senior positions, even. Especially sucks because I don't usually program terribly fast or…

Have you ever needed to flatten a list in your career outside exams and interviews?

> Have you ever needed to flatten a list in your career outside exams and interviews?

Yes, multiple times in multiple languages. C, C++, Javascript... sometimes "npm install" is not the answer.

Re: Why it’s hard for programmers to write a program to flatten a list?

#93
post #34

Earlier quoted context omitted.

Doesn't stop people from asking these questions of people at all levels. I still get asked these coding questions pretty much every interview I have had, and I have almost a decade of programming experience, and have even been lead programmer on some projects (without the title). I've been asked a barrage of these questions for senior positions, even. Especially sucks because I don't usually program terribly fast or…

Have you ever needed to flatten a list in your career outside exams and interviews?

> Have you ever needed to flatten a list in your career outside exams and interviews?

Yes, multiple times in multiple languages. C, C++, Javascript... sometimes "npm install" is not the answer.

Re: Why it’s hard for programmers to write a program to flatten a list?

#94
post #23

Earlier quoted context omitted.

It does not really test potential because a junior trained in Java has poor tools for flattening a list because idiomatic Java would not represent hierarchical data structure as a nested list. Idiomatic Java would use tree and node objects. Idiomatic Java also prefers arrays over lists for sequential data. An experienced programmer might look at the problem and choose a better tool: a different language or call a ser…

> An experienced programmer might look at the problem and choose a better tool: a different language Come on, you are not going to call Haskell or Python to flatten a list in your Java. > or call a service I know we have "micro" services now, but really? Sending serialized data to ListFlatteningService to get deserialized FlattenedList? > or rewrite the offending code Something may produce it because it makes sense i…

[deleted]

Re: Why it’s hard for programmers to write a program to flatten a list?

#95

I think a lot of this is based on your experience with languages. If you have exposure to a list/iterator native language like python, you come up with solution in a few seconds, even if you aren't even remotely a programmer. Other languages might not lend themselves to so obvious a solution. The perl example cited here kind of blows my mind compare to the trivial python approach: def flatten(lst): rlst=[] for x in l…

[deleted]

Re: Why it’s hard for programmers to write a program to flatten a list?

#96
post #67

I think a lot of this is based on your experience with languages. If you have exposure to a list/iterator native language like python, you come up with solution in a few seconds, even if you aren't even remotely a programmer. Other languages might not lend themselves to so obvious a solution. The perl example cited here kind of blows my mind compare to the trivial python approach: def flatten(lst): rlst=[] for x in l…

You perhaps missed that there were two solutions (+ test infrastructure) in that Perl post. You Python code is the moral equivalent of the three-line "flatten" function in the Perl example. The long "flatten2" function was doing it without recursion.

The part that was/is challenging/mind blowing for me to grok was:

   sub flatten {
        my @f;
        push @f, (ref $_ ? flatten( $_ ) : $_ ) for @{ $_[0] };
        return @f;
    }

The implicit $_, and whatever @{ $_[0]} does, is challenging enough that I bet I could find people who had been using perl to do work for years who wouldn't be able to explain what was happening here at first glance, even if they could eventually work it out/write it themselves with a bit of effort.

That's what I meant by native iterative/sequence languages like python lend themselves to easy solutions for this type of question, even for people who aren't programmers, whereas you really need to be a programmer to come up with something as powerful as the above.

Put another way, doing a complex joined query might be a trivial question for someone who knows even the slightest amount of SQL, but would be beyond most novice python developers (without of course just using a SQL select statement...)

I will admit the (dense) perl solution certainly has its own beauty that the more drawn out python solution is lacking, though I'm guessing it could be written in a pythonic way itself...

Re: Why it’s hard for programmers to write a program to flatten a list?

#97
post #34

Earlier quoted context omitted.

Doesn't stop people from asking these questions of people at all levels. I still get asked these coding questions pretty much every interview I have had, and I have almost a decade of programming experience, and have even been lead programmer on some projects (without the title). I've been asked a barrage of these questions for senior positions, even. Especially sucks because I don't usually program terribly fast or…

Have you ever needed to flatten a list in your career outside exams and interviews?

Yes. Something like:

List> records;

We would send a request off to multiple services, wait for all the responses and then the front end would sometimes need all of the lists and other times need it flatten.

How would I do it?

var allitems = records.SelectMany(b => b).ToList();

Re: Why it’s hard for programmers to write a program to flatten a list?

#98
post #67

Earlier quoted context omitted.

You perhaps missed that there were two solutions (+ test infrastructure) in that Perl post. You Python code is the moral equivalent of the three-line "flatten" function in the Perl example. The long "flatten2" function was doing it without recursion.

The part that was/is challenging/mind blowing for me to grok was: sub flatten { my @f; push @f, (ref $_ ? flatten( $_ ) : $_ ) for @{ $_[0] }; return @f; } The implicit $_, and whatever @{ $_[0]} does, is challenging enough that I bet I could find people who had been using perl to do work for years who wouldn't be able to explain what was happening here at first glance, even if they could eventually work it out/write…

> I bet I could find people who had been using perl to do work for years who wouldn't be able to explain what was happening here

Don't jump to conclusions. You will not find such people, because those are very basics. Understanding of $_[0] is necessary to use arguments in Perl functions and understanding of @{...} is necessary to use array references as lists.

Re: Why it’s hard for programmers to write a program to flatten a list?

#100
post #34

Earlier quoted context omitted.

Doesn't stop people from asking these questions of people at all levels. I still get asked these coding questions pretty much every interview I have had, and I have almost a decade of programming experience, and have even been lead programmer on some projects (without the title). I've been asked a barrage of these questions for senior positions, even. Especially sucks because I don't usually program terribly fast or…

Have you ever needed to flatten a list in your career outside exams and interviews?

Exactly once. But I needed to know recursion, typechecking, and modern language features almost every day.
Post reply on HN