Skip to main content
Share Your Experience: Take the 2024 Developer Survey
53 votes
Accepted

FizzBuzz in Javascript

I'd like to see if this is a reasonable solution, or is just plain terrible. I wouldn't say it is "terrible" - mostly because it works and doesn't appear to be very inefficient. However, ...
Sᴀᴍ Onᴇᴌᴀ's user avatar
26 votes

FizzBuzz in Javascript

One of the problems is that the case where you check i % 15 (i.e. i is a multiple of 3 and 5) is unnecessary. You have the ...
Michael's user avatar
  • 778
22 votes

Tests for palindromes in C and C++

A special case length < 3 seems like a bug. A string aa is a palindrome, and one could successfully argue that a single-...
vnp's user avatar
  • 56.5k
22 votes
Accepted

Count Even Digits In Number

You should have a look at Loop like a native!. Explicitly iterating over the indices is usually not the best way to do something in Python. By directly iterating over the string representation of <...
Graipher's user avatar
  • 41k
16 votes
Accepted

Tests for palindromes in C and C++

There are some things to be said about your C version as well, but since you explicitly asked about the C++ version (and also because my C-knowledge is not that great), I will leave those for somebody ...
Ben Steffan's user avatar
  • 5,228
16 votes

FizzBuzz in Javascript

I think it's fine as is, though some folks like to return early instead of using a series of if..else. For example: ...
Paul's user avatar
  • 321
15 votes
Accepted

Elegantly exclude part of an operation for the last iteration in a loop

If you are just doing a single-shot printing of a container, just print the first element and then print delim-value pairs. This reduces the iterator requirements to being equality comparable, ...
Snowhawk's user avatar
  • 6,148
13 votes

Tests for palindromes in C and C++

I've seen a lot of answers here over-generalizing. You're dealing with a std::string, so you can just do: ...
David's user avatar
  • 1,038
13 votes

Count Even Digits In Number

As mentioned in another answer, iterating with indices in Python in most cases is a bad idea. Python has many methods and modules to avoid it. For your problem Python has powerful decimal module to ...
vurmux's user avatar
  • 1,355
12 votes

Shuffling multiple iterators in order

functools.singledispatch functools library includes the singledispatch() decorator. It lets you provide a generic function, but provide special cases based on the ...
RootTwo's user avatar
  • 10k
12 votes
Accepted

Generating sequential alphanumeric values that match a certain pattern

This is a fun problem. I've taken the liberty of writing an alternate implementation, which I will describe below. Note that I've not focused too much on performance at all, only on simplicity and ...
Reinderien's user avatar
  • 62.1k
12 votes
Accepted

Determine if it is EU summer time in Java

determine if a given date (year, month and day) falls within EU summer time or not This is already problematic for a few reasons: Year, month and day... at what time? Daylight savings changes at a ...
Reinderien's user avatar
  • 62.1k
11 votes
Accepted

Parallelized for loop in Bash

Some suggestions: The trailing slash in the mkdir command is redundant. $(…) is preferred over backticks for command ...
l0b0's user avatar
  • 8,927
11 votes
Accepted

Forming error messages from a multidimensional associative array

In my opinion, this code is all right. As any explicit code, these loops are quite straightforward to follow. And making it more concise will likely make it more cryptic as well. I would only make a ...
Your Common Sense's user avatar
10 votes
Accepted

Building XML elements from a list using LINQ and foreach

The best way to discuss performance is by measuring it. I suggest you use BenchmarkDotNet and never roll your own solution. Onto your question: it is safe to assume that LINQ will in most cases be ...
Jeroen Vannevel's user avatar
10 votes

Count Even Digits In Number

Since you are dealing with digits, you at some point must pick a base (in this case base 10). So converting the number to a string is not a bad way of getting the digits. I would keep it simple, and ...
spyr03's user avatar
  • 3,032
10 votes
Accepted

Restructuring JSON to create a new JSON where properties are grouped according to similar values

Scoping issues You are using an "immediately invoked function expression" (IIFE) here to scope the unique_dates, but in the process you forgot to use the ...
Vogel612's user avatar
  • 25.2k
9 votes

Forming error messages from a multidimensional associative array

I agree with what "Your Common Sense" said, but I would like to have better variable names. Names that make clear what a variable contains. Seen in isolation variable names like ...
KIKO Software's user avatar
9 votes

Generate random string to match the goal string with minimum iteration and control statements

Find my suggestions below. goalString = input() Let the user know what to do, input accepts a string as an argument: ...
Marc's user avatar
  • 5,604
9 votes

Given two vectors of ints, determine whether one vector is a prefix of the other

Embrace auto. Not only is it shorter, but avoids embarrassing bugs in choosing the wrong type. decltype() is generally much more ...
Deduplicator's user avatar
  • 19.3k
8 votes
Accepted

Remove all values duplicates from list

Iterating backwards was smart, but, yes, there is a much simpler way to do this using a list comprehension – this is how one typically filters a list in Python. ...
Jared Goguen's user avatar
8 votes

Restructuring JSON to create a new JSON where properties are grouped according to similar values

Names! JavaScript convention is to use camelCase for names rather than snake_case. Using a Hash Map Using Array.indexOf to ...
Blindman67's user avatar
  • 22.1k
8 votes

Count the number of mismatches between two arrays

At first I wasn't going to do this, in part because of the cross-posted version of this question to Stack Overflow being closed as duplicates of various pre-existing questions about counting bytes, ...
user555045's user avatar
  • 9,794
7 votes

Progress report for a long-running process using 'yield'

Use A generator as a callback function. A generator is absolutely perfect for this. Using your example I'll demonstrate ...
Roger Thomas's user avatar
7 votes

Biometric matching score calculator

Regarding the vectorized code: permute can be considered a "Zero-cost operation". Logical operations are performed fastest using ...
Stewie Griffin's user avatar
7 votes
Accepted

Yet another vowel counting program

You could use Enumerable#count: string.chars.count { |char| vowels.has_key?(char) } You very rarely need to declare a ...
Flambino's user avatar
  • 33k
7 votes
Accepted

Concise code to perform action on every element, when need index

linqObject.Where((obj, index) => { DoWork(obj, index); return true; }).ToArray(); Never ever! This is the worst possible abuse of an API. I 100% agree with ...
t3chb0t's user avatar
  • 44.3k
7 votes

Prime Factorisation in Swift

To address your "main need" first: Your algorithm starts with a single-element array, and then repeatedly calls primeFact() to compute a new array, until the array ...
Martin R's user avatar
  • 23.3k
7 votes
Accepted

A database of books, part 1

I would: Change make_list to return good_count, bad_count and ...
Peilonrayz's user avatar
  • 43.4k

Only top scored, non community-wiki answers of a minimum length are eligible