Martin R
  • Member for 7 years, 11 months
  • Last seen this week
  • Germany
Stack implementation using Swift
Accepted answer
28 votes

Let's start with some general remarks: Swift does not require semicolons after statements (but they are allowed). From what I have seen since Swift was introduced last year, most people do not write ...

View answer
Sieve of Eratosthenes implementation is running slowly
24 votes

There is some room for improvement in your code, and I will try to explain that in several steps. But please note that I am not a C# person, so my review refers only to the algorithm and performance ...

View answer
Codewars: Reduce strings to one character
Accepted answer
22 votes

First I would choose a different representation of the colors during the computation, to avoid the “expensive” COLORS-{a, b}).pop() operation, which is executed $$ (n-1) + (n-2) + \ldots + 1 = \...

View answer
Ultimate FizzBuzz
19 votes

Your code looks good to me, and I have only some minor remarks and suggestions. An Optional has an implicit initial value of nil, so var retnValue: String? = nil can be simplified to var ...

View answer
Simplify a fraction
Accepted answer
18 votes

Correctness Your function reduces 0/0 to 0, but the result should be "undefined" or "invalid". Too many special cases Your function distinguishes far too many cases. The process of simplifying a ...

View answer
Converting seconds to hours, minutes and seconds
18 votes

Your usage of auxiliary functions is not consistent: Some computations are done inline, e.g. int hours = totalSeconds / MINUTES_IN_AN_HOUR / SECONDS_IN_A_MINUTE; and other (equally simple) ...

View answer
Prime Factorization
17 votes

Your algorithm tries to find the largest factors first. This is ineffective because you have to test each possible factor whether it is a prime number or not. It is much more effective to start with ...

View answer
Project Euler #2 (Even Fibonacci numbers) in Swift
Accepted answer
16 votes

The following is similar to Flambino's approach, but creates a Swift SequenceType so that you can use the Swift library functions filter() and reduce() to iterate over the elements: struct ...

View answer
Extending CGPoint to conform to Hashable
Accepted answer
14 votes

I can see no advantage of computing the hash value from Int(self.x) and Int(self.y). As you already noticed, truncating the floating point numbers to integers loses information and therefore causes ...

View answer
C++ determinant calculator - follow-up
14 votes

if(dimension == 0) { return 0; } Mathematically, that is not correct. The determinant of an empty (i.e. zero-dimensional) matrix is one, see for example What is the determinant of []? on ...

View answer
Project Euler Problem 12 - triangle number with 500 divisors
Accepted answer
14 votes

There is an error in your countDivisors() function, the result is one too large for a perfect square, i.e. countDivisors(25) returns 4 instead of 3. Also the loop variable i should be a long to avoid ...

View answer
Python π = 1 + (1/2) + (1/3) + (1/4) - (1/5) + (1/6) + (1/7) + (1/8) + (1/9) - (1/10) ...1748 Euler
Accepted answer
13 votes

What you are approximating is $$ \pi =\sum_{m=1}^{\infty}\frac{(-1)^{s(m)}}{m},$$ where \$s(m)\$ counts the number of appearances of primes of the form \$4k+1\$ in the prime decomposition of \$m\$, ...

View answer
Elegant solution to find a pair of Strings in a List
12 votes

iA and iB can only take the values 0 and 1, so that if ((1 == iA || 1 == iB) && (1 != iA || 1 != iB)) { ... } can be simplified to if (iA != iB) { ... } Boolean variables would be more ...

View answer
Random IP Address Generator
12 votes

Validate the input: At present, your program aborts with IndexError if called without arguments, aborts with ValueError if called with a non-integer argument, prints None if called with an integer ...

View answer
Fibonacci in linear time by using an extra pointer
Accepted answer
12 votes

Your function uses global variables, which is bad for several reasons: The variables must be reset before the function can be called again. The variables can be modified from outside of your function,...

View answer
PrimeFactorize method for int
Accepted answer
12 votes

First of all, the PrimeFactorize() does never terminate for number = 1 because the check IntUtils.IsPrime(number) will never return true. The call PrimeFactorize(1) should return an empty list. To ...

View answer
Hexadecimal command line file viewer
Accepted answer
12 votes

If a program fails for some reason then it should Write a diagnostic message to standard error (not standard output), so that it can not be mistaken for normal output, e.g. when the program runs in a ...

View answer
Prime Number Generator in Swift
Accepted answer
11 votes

That is not the Sieve of Eratosthenes The Sieve of Eratosthenes computes multiples of each found prime to mark subsequent composite numbers in the sieve. Your algorithm computes the remainder of all ...

View answer
A function to find temperatures closest to zero
Accepted answer
11 votes

Bugs The loop for (let i = 0; i < tempArray.length - 1 ; i++) { omits the last array element, it should be for (let i = 0; i < tempArray.length ; i++) { If all array elements have the same ...

View answer
Look! Smoke! Who do we blame?
Accepted answer
11 votes

Spacing The usage of (horizontal) space is not consistent, you have a space after if and switch, but not after else and while. I would generally use more whitespace to increase the readability, in ...

View answer
Odd divisor summation
11 votes

First I would suggest to separate the code into two functions: One which computes the sum of odd divisors of a single number, and another which adds the odd divisors of all numbers in a list. Also, ...

View answer
Finding the maximum sum of any 2 elements in an array of integers
Accepted answer
11 votes

Your code tries all \$ n (n+1)/2 \$ combinations of array elements to find the combination with the largest sum, so the complexity is \$ O(n^2) \$. A better solution would be to find the two largest ...

View answer
BOOL declaration
Accepted answer
11 votes

Using a property for the boolean attribute is fine. Properties have many advantages: they encapsulate an objects value (i.e. the actual implementation is hidden from the outside, it need not be an ...

View answer
Backup with Hanoi Strategy
10 votes

Do not silently ignore invalid arguments Your script allows to specify the source directory (and other locations). Here is the relevant part in function parseArgs(): -s|--src) ...

View answer
Sort characters in a Python string by frequency
10 votes

The “set or increment dictionary value” logic freq = {} for i in s: if i in freq: freq[i] +=1 else: freq[i] = 1 can be simplified using a defaultdict: from collections ...

View answer
Efficiently counting rooms from a floorplan (version 2)
10 votes

Unfortunately, it does not work correctly. The output is 0 (zero rooms) for the floor plan 4 6 ###### #.#..# #....# ###### After parsing the second row, the room count is two and the tracker vector ...

View answer
Calculate highest prime factor very slow
Accepted answer
10 votes

You want to compute the largest prime factor of a given number, but it is actually more efficient to look for the smallest prime factors first (and I am just repeating my arguments from my answer). If ...

View answer
Hacker Rank Challenge : Find count of substrings which are special palindrome
Accepted answer
9 votes

This #include <bits/stdc++.h> using namespace std; is given by the submission template from HackerRank (so it is not your fault), but note that both lines are generally considered as bad ...

View answer
Hacker rank 30 days of code Maximum Sum of Hourglass
9 votes

Reading the input data in a loop arr = [] for _ in range(6): arr.append(list(map(int, input().rstrip().split()))) can be done with list comprehension, also rstrip() is not needed here because ...

View answer
Getting all divisors from an integer
Accepted answer
9 votes

Some general remarks: Variables should be declared explicitly with var, otherwise you create a global variable. The indent amount is different between the first level and the deeper levels. The ...

View answer
1
2 3 4 5
13