Hot answers tagged

16 votes

Dot product, ported from C to Swift

You can remove the size parameter as arrays know their own size in Swift, and use the zip, ...
Robert's user avatar
  • 261
14 votes
Accepted

Extending CGPoint to conform to Hashable

I can see no advantage of computing the hash value from Int(self.x) and Int(self.y). As you already noticed, truncating the ...
Martin R's user avatar
  • 23.1k
13 votes
Accepted

Mocking UserDefaults in Swift

So first, as CAD97 rightly points out, if you simply define your protocol as identically matching the methods from UserDefaults you intend to use, then making ...
nhgrif's user avatar
  • 25.2k
12 votes
Accepted

Fibonacci in linear time by using an extra pointer

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,...
Martin R's user avatar
  • 23.1k
11 votes
Accepted

On Knuth's "Algorithm L" to generate permutations in lexicographic order

Optimising the functional approach Iterative vs functional approach: Is it possible to use the (existing) functional methods without losing performance? I don't think it's possible to use the ...
Hamish's user avatar
  • 423
11 votes
Accepted

Prime Number Generator in Swift

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 ...
Martin R's user avatar
  • 23.1k
10 votes
Accepted

Reading bytes from packet

One things for certain... an array of tuples which is basically being used as a glorified dictionary is never going to suffice as an acceptable data model. Moreover, an array of bytes isn't really ...
nhgrif's user avatar
  • 25.2k
9 votes
Accepted

Swift HackerRank Balanced Brackets

readStrings() is better named readCharacters() because that is what it does. I prefer ...
Martin R's user avatar
  • 23.1k
9 votes
Accepted

Sequence-based enumeration of permutations in lexicographic order

Performance I'm not particularly surprised that the version of permutations(of:) which uses AnySequence and ...
Hamish's user avatar
  • 423
8 votes
Accepted

Chop off trailing newline for Swift's NSAttributedString

Your method works correctly, as far as I can see. For a read-only computed property, you don't need to enclose the getter method in get { }: ...
Martin R's user avatar
  • 23.1k
8 votes
Accepted

Sqlite.swift -- Selecting all rows and returning them in an array of tuples

Don't use tuples like this. The purpose of tuples is to make simple, short lived collections of values. For a use case like this, it is preferable to use a struct ...
Alexander's user avatar
  • 494
8 votes
Accepted

Project Euler problem 14 (longest Collatz sequence) in Swift 3

The main performance bottleneck of your code was already pointed out in the other answers: building lots of arrays (without need). I'll like to point out how some Swift techniques can be used to ...
Martin R's user avatar
  • 23.1k
8 votes

"What To Do" App

The instance variables var userText : String = " " var randomNumber : Int = 0 are not needed: userText is only used within <...
Martin R's user avatar
  • 23.1k
8 votes
Accepted

Showing a welcome screen once per day

There is some code duplication for the cases “greeting date has never been set” and “last greeting was yesterday or earlier.” This can be avoided if you use the nil-coalescing operator ...
Martin R's user avatar
  • 23.1k
8 votes
Accepted

Debounce and throttle tasks using Swift Concurrency

The problem is the use of a nonisolated function to initiate an asynchronous update of an actor-isolated property. (I'm surprised the compiler even permits that.) ...
Rob's user avatar
  • 2,427
7 votes
Accepted

Star Rating in Swift

You could create a helper routine getStarImage:forRating: and use that to set up each star: ...
vacawama's user avatar
  • 406
7 votes
Accepted

Type to byte array conversion in Swift

Using a dedicated enumeration type ByteOrder is a good idea. CFByteOrder is (ultimately) a type alias for ...
Martin R's user avatar
  • 23.1k
7 votes
Accepted

Hex String to Bytes (NSData)

First note that your code does not detect invalid input data. For example, the string "XX" is just converted to a zero byte. Detecting invalid input with ...
Martin R's user avatar
  • 23.1k
7 votes
Accepted

Project Euler solution #2 using Swift 3

This post from Martin R is a good step in the right direction toward generalizing your problem. The helper function at the top of the post is already what you need for making a more generalized loop: ...
nhgrif's user avatar
  • 25.2k
7 votes

DFT (Discrete Fourier Transform) Algorithm in Swift

I think your code is doing DFT (Discrete Fourier Transform) and not FFT. You are doing \$O(n^{2})\$ operations in the 2 for loops. The FFT is supposed to be \$n*\...
lichi101's user avatar
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.1k
7 votes

Finding the number of perfect squares in an array

Mathematical correctness The used approach is incorrect for very large numbers, here is an example: ...
Martin R's user avatar
  • 23.1k
7 votes
Accepted

Minimum window substring - LeetCode challenge

Performance The main culprit for exceeding the time limit is here: while end < s.count { ... } A Swift string is a ...
Martin R's user avatar
  • 23.1k
6 votes
Accepted

Updating number of votes with a "Like" button

Formatting On the whole, there are several indentation problems through out your code. Here is made the most obvious example: ...
nhgrif's user avatar
  • 25.2k
6 votes
Accepted

Implementing the Hashable Protocol in Swift with the DJB hash function

The Hashable protocol has only one single requirement: Axiom: x == y implies ...
Martin R's user avatar
  • 23.1k
6 votes

Swift replacement for C's for-loop

Python also has no C-style for-loop. A lot of Python novices write clumsy while-loops as a result. However, once you learn the Pythonic idioms for looping, you'll wonder why other languages aren't ...
200_success's user avatar
6 votes

Music player in Swift

Let's talk very generally about AVAudioPlayer. There are a few relevant methods to discuss here: play ...
nhgrif's user avatar
  • 25.2k
6 votes
Accepted

Music player in Swift

class ViewController: UIViewController { I noticed that your class is called ViewController. There are two things to comment ...
nhgrif's user avatar
  • 25.2k
6 votes
Accepted

Classification tree in Swift

So, let's focus on your Node class at the top. I started off, without looking at anything else, and just swiftlinting it. You have a 55 line file with 20 ...
nhgrif's user avatar
  • 25.2k
6 votes
Accepted

Safe init using failable guarded initializer

The separation into a non-failable and a failable init method is fine. The forced casts with as! are safe in your code, because it is verified before that the ...
Martin R's user avatar
  • 23.1k

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