Post A41dIFsTuptOyB2oZU by unclechu@mastodon.social
 (DIR) More posts by unclechu@mastodon.social
 (DIR) Post #A41XyUdgp901BlsoXQ by hyperrealgopher@fosstodon.org
       2021-02-07T07:47:11Z
       
       0 likes, 0 repeats
       
       I saw this problem on HackerNews:Most candidates cannot solve this interview problem:  * Input: "aaaabbbcca"  * Output: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]Write a function that converts the input to the outputI ask it in the screening interview and give it 25 minutesHow would you solve it?Here's my solution(s) written in #Haskell: https://gist.github.com/hyperrealgopher/19730725804ba8825d50d96f75b88a96
       
 (DIR) Post #A41Y4EcbI1enFU6yGW by hyperrealgopher@fosstodon.org
       2021-02-07T07:48:14Z
       
       0 likes, 0 repeats
       
       By the way, here's the post on HackerNews: https://news.ycombinator.com/item?id=26052529
       
 (DIR) Post #A41Yk30eqx1POhImci by hyperrealgopher@fosstodon.org
       2021-02-07T07:55:47Z
       
       0 likes, 0 repeats
       
       I updated my solution again to use &&& which I almost totally forgot about!
       
 (DIR) Post #A41Z4mokFZbxG9cQb2 by brad@toot.cafe
       2021-02-07T07:59:31Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher I saw this and thought it looked a bit fun too. `map (head &&& length) . group` is what I had in mind. Where (&&&) comes from Control.Arrow, and head and group both come from Data.List.NonEmpty. You could also use the Applicative instance for (->) instead of (&&&), like: map ((,) <$> head <*> length) . group.
       
 (DIR) Post #A41ZVR3PH4vda1BSnw by Spaceface16518@fosstodon.org
       2021-02-07T08:04:21Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher this is kinda run-length encoding, right? i always use fold(l) for this one but group is a better option, i didn’t think of that.
       
 (DIR) Post #A41acbcmTJ4yLSiGGW by brad@toot.cafe
       2021-02-07T08:16:36Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher ❤️ Haskell :)
       
 (DIR) Post #A41d7vWij2D0xLwOnY by unclechu@mastodon.social
       2021-02-07T08:44:55Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher Within 10 mins:foldl (\acc x -> maybe acc ((acc <>) . pure . fmap (succ . length)) $ uncons x) [] (group "aaaabbbcca")
       
 (DIR) Post #A41dIFsTuptOyB2oZU by unclechu@mastodon.social
       2021-02-07T08:46:47Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher Within 10 mins:foldl (\acc x -> maybe acc ((acc <>) . pure . fmap (succ . length)) $ uncons x) [] (group "aaaabbbcca")And I wouldn’t use `head` since it’s a partial function.
       
 (DIR) Post #A41ep8klWcIQNrgDoG by zleap@qoto.org
       2021-02-07T09:03:19Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher This is probably wrong I found a way to count characters in a string on stackoverrflow then adapted it for this#https://gist.github.com/hyperrealgopher/19730725804ba8825d50d96f75b88a96string="aaaabbbccd"char="a"echo "character count for a" awk -F"${char}" '{print NF-1}' <<< "${string}"char="b"echo "charcter count for b"awk -F"${char}" '{print NF-1}' <<< "${string}"char="c"echo "character count for c"awk -F"${char}" '{print NF-1}' <<< "${string}"char="d"echo "character count for d"awk -F"${char}" '{print NF-1}' <<< "${string}"
       
 (DIR) Post #A41etKoIdhbuw7KiPo by hyperrealgopher@fosstodon.org
       2021-02-07T09:04:42Z
       
       0 likes, 0 repeats
       
       @zleap Close, but no cigar! Very interesting solution, but if you look at the output closer, you'll notice that 'a' at the end!
       
 (DIR) Post #A41fA70fCdRz3d7H4i by unclechu@mastodon.social
       2021-02-07T08:52:08Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher Another one without a fold:(fmap . fmap) (succ . length) . catMaybes . fmap uncons . group $ "aaaabbbcca"
       
 (DIR) Post #A41fA7YLBQOAk4sAZE by hyperrealgopher@fosstodon.org
       2021-02-07T09:07:44Z
       
       0 likes, 0 repeats
       
       @unclechu I love your solutions! Thanks for sharing!
       
 (DIR) Post #A41fgyDAw2oBKJdWsq by zleap@qoto.org
       2021-02-07T09:13:37Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher Good point and you want me to separate the count for the first set of a's and the last one not give a total.
       
 (DIR) Post #A41hcPzmPKbnxQKPDM by jmcs@social.tchncs.de
       2021-02-07T09:35:15Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher I use a similar question in interviews. Most people that fail do it because they try to show off and use fancy tricks instead of being pragmatic (which is what I'm actually trying to check with that question 🤫).
       
 (DIR) Post #A41i8QftPx9ZKZI3Iu by hyperrealgopher@fosstodon.org
       2021-02-07T09:41:02Z
       
       0 likes, 0 repeats
       
       @jmcs Ha, ha! That's great insight. Thanks for replying!
       
 (DIR) Post #A46xV51gXh11x6XeVc by raichoo@chaos.social
       2021-02-09T22:26:49Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher `map (head &&& length) . group` I'm pretty sure this would be considered "cheating" in the interview ^^
       
 (DIR) Post #A47W5GX4dX9QVMeLUe by hyperrealgopher@fosstodon.org
       2021-02-10T04:54:21Z
       
       0 likes, 0 repeats
       
       @raichoo That's what `startSolution4` is for.
       
 (DIR) Post #A47zKz6y4ZgXmKHpPk by raichoo@chaos.social
       2021-02-10T10:22:08Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher I have to admit that I didn't look at your solution (I was pretty sure you that you are perfectly capable of solving that issue ^^). I'm wondering what kind of solutions would have been accepted by the interviewer and what would be considered as "cheating".
       
 (DIR) Post #A47zO9nyloY9oTuwfQ by raichoo@chaos.social
       2021-02-10T10:22:44Z
       
       0 likes, 0 repeats
       
       @hyperrealgopher I have to admit that I didn't look at your solution (I was pretty sure that you are perfectly capable of solving that issue ^^). I'm wondering what kind of solutions would have been accepted by the interviewer and what would be considered as "cheating".
       
 (DIR) Post #A4828trC5B8ZrypzNo by hyperrealgopher@fosstodon.org
       2021-02-10T10:53:35Z
       
       0 likes, 0 repeats
       
       @raichoo thanks! I was told after that initial solution to try doing a version "without any functions." I had to explain that part of the beauty of Haskell is that its full of wonderful higher order functions
       
 (DIR) Post #A482AbrBye7ScfeU64 by hyperrealgopher@fosstodon.org
       2021-02-10T10:53:53Z
       
       0 likes, 0 repeats
       
       @raichoo thanks! I was told after that initial solution to try doing a version "without any functions." I had to explain that part of the beauty of Haskell is that its full of extremely good higher order functions.
       
 (DIR) Post #A4IkPz9FkreJJwRU9o by borko@mastodon.social
       2021-02-15T13:00:15Z
       
       0 likes, 0 repeats
       
       @raichoo why do you think it would be cheating?
       
 (DIR) Post #A4IkPzZ8CeLicCY9Ue by Ninjatrappeur@social.alternativebit.fr
       2021-02-15T14:56:28.033516Z
       
       0 likes, 0 repeats
       
       @borko @raichoo I assume because you'll end up allocating 2 times as much tuples as necessary on runtime. If I remember correctly (and I might be wrong), the default (&&&) is implemented with a split. I also don't think (&&&) is that used. I hardly see any arrows in the codebases I had to work on so far.But I agree, in terms of wow effect, it's pretty good.I'd probably rather go with `fmap (x->(head x, length x)) . group`, less magic involved.
       
 (DIR) Post #A4IkXgyTD12jGv4ciG by Ninjatrappeur@social.alternativebit.fr
       2021-02-15T14:57:52.900123Z
       
       0 likes, 0 repeats
       
       @borko @raichoo https://hackage.haskell.org/package/base-4.14.1.0/docs/src/Control.Arrow.html#%26%26%26there's an extra tuple on top of the split. 3 times as much tuples as necessary are allocated in the end.