DEV Community

Cover image for Daily Coding Puzzles - Oct 29th - Nov 2nd
Ali Spittel
Ali Spittel

Posted on • Edited on

Daily Coding Puzzles - Oct 29th - Nov 2nd

Every day on Twitter, I post coding puzzles. These are quick coding challenges that increase in difficulty across the span of the week -- with Monday being the most beginner friendly and Friday being super tough. I love seeing other people's solutions as well, and so people post their solutions as well to the problem in any programming language.

Here's more about them.

I wanted to try posting these here. I'm going to post each question from this week as a comment below, and then we will thread answers under those questions. Please feel free to post your solutions to the ones you are interested in below! Then, you can comment with insights into people's solutions below that! I will also add a meta thread if you have advice on how to format this in the future!

Excited to see your solutions!

Latest comments (63)

Collapse
 
thejessleigh profile image
jess unrein

You've got me on a go kick, and now I'm going back and doing previous exercises you've posted! (Sorry for the large volume of comments on your posts today.

Collapse
 
aspittel profile image
Ali Spittel

Haha there are worse problems out there

 
aspittel profile image
Ali Spittel

the positive integer shouldn't have a leading zero I don't think. It's at least up to interpretation.

 
aspittel profile image
Ali Spittel

true -- I'm on Python 3 though and the test cases are all small

 
dance2die profile image
Sung M. Kim

Ah ha. Tuple being a value type, it is better optimized.
Thanks for the tips there 👊

 
joshcheek profile image
Josh Cheek • Edited

Oh holy shit! I was looking at it for several minutes and it started making sense! Maybe because I thought through problem with @aspittel , or maybe this APL is more sensible (the last one I saw, someone had spilled a bag of unicode across its source).

I'm using Ruby's comment syntax because IDK how to do it in APL

# Haskell uses arrows for assignment in `do` blocks.
# Whitespace delimited numbers are a thing in lisp.
# The high bar looks a lot like a minus sign, so:

A←                 # set into the variable A
A←1 2 3 ¯4 ¯5 ¯6   # the list of numbers: [1, 2, 3, -4, -5, -6]


# Haskell iplements + as a function that you can pass around
# The slash looks like and is used like a shell pipeline.
# Tilde is logical negation in many syntaxes.
# `A` in `A<0` is like SQL, which uses the
# table name to refer to that table's current row.

X←             # set into the variable X
X←+/           # the sum of
X←+/(~         # the numbers which aren't
X←+/(~A<0)/    # negative
X←+/(~A<0)/A   # from the list A

Y←            # set into the variable Y
Y←+/          # the sum of
Y←+/(~        # the variables that aren't
Y←+/(~A>0)/   # positive
Y←+/(~A>0)/A  # from the list A

O←X Y    # set X and Y as the output
 
dance2die profile image
Sung M. Kim

Thanks for the suggestion there, @asparallel.

Here is the updated C# code (with the same logic as JavaScript one) using .Aggregate.

using System.Linq;
using System;

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] a)
    {
        return (a == null || a.Length == 0) 
          ? new int[0]
          : a.Aggregate(new [] {0, 0}, (acc, n) => {
              if (n > 0) acc[0]++;
              else acc[1] += n;
              return acc;
            });
    }
}
Enter fullscreen mode Exit fullscreen mode
 
dance2die profile image
Sung M. Kim • Edited

987654321 is how I expect it to work though without the 0 in the beginning as you should return a positive integer.

 
gypsydave5 profile image
David Wickes

I will one day be able to look at APL without my brain leaking out of my ears.

Today is not that day.

Collapse
 
gypsydave5 profile image
David Wickes

Common Lisp FTW!