Skip to main content
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
  • 24.2k
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
  • 24.2k
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
  • 24.2k
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
  • 24.2k
8 votes
Accepted

Changing the color of some pixels in an image in Swift

There are two issues. The process of converting to and from UIColor objects is very expensive. The process of calling contains ...
Rob's user avatar
  • 2,657
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,657
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
  • 24.2k
7 votes
Accepted

Leetcode 102: Binary tree level-order traversal in Swift

Simplifying your code Let's start here: ...
Martin R's user avatar
  • 24.2k
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
  • 24.2k
6 votes

Wrapping the iOS triple buffering procedure in a generic swift class

Your platform-dependent code ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Ring Timer Animation with Chasing Tail using CoreAnimation

There is one problem with your approach: Additional sublayers are added in draw() and in animateTimerReset() and never removed. ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Grouping Date Objects by year and month

I'll focus first on static func sortDateByMonth(dateArray:[Date]) -> [[Date]] because that contains the main logic, then mention some other points, and ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Leetcode 38: The “count-and-say” sequence

Your iterative solution This is generally fine, with the only exception that it operates on an array of strings –  I'll come back to that later. Variables should be defined in the narrowest scope ...
Martin R's user avatar
  • 24.2k
6 votes

Leetcode 38: The “count-and-say” sequence

Iterative vs Recursive For code efficiency, I personally always prefer the iterative approach. According to LeetCode : You iterative code clocked at 12ms on Leetcode, being 3 times as fast as your ...
ielyamani's user avatar
  • 889
6 votes
Accepted

MVVM in Swift iOS

A couple of thoughts. The presence of init(viewModel:) in the view controller (with its associated comment about using this during testing) seems to suggest that ...
Rob's user avatar
  • 2,657
6 votes
Accepted

LinkedList Swift Implementation

Nested types All “dependent” types are defined within the scope of LinkedList, which is good. To reference those types from within ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Parse data into an array of structs

This let filePath = Bundle.main.path(forResource: "test", ofType: "dat") guard filePath != nil else { return } let fileURL = URL(fileURLWithPath: filePath!) is ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Reducing a list of points into a list of pairs of points

Let's start with the first version: A loop var i = 0 while i < points.count { // ... i += 2 } can be written as a for-loop over a stride: ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Working with Arrays in Swift

Handle edge cases If the given numbers array is empty then the first loop will do nothing, but ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Function to generate an Int from a series of bits

Improving the code Your code computes $$ a_0 2^{n-1} + a_1 2^{n-2} + \ldots a_{n-2} 2 + a_{n-1} \, , $$ that is the polynomial $$ P(x) = a_0 x^{n-1} + a_1 x^{n-2} + \ldots a_{n-2} x + a_{n-1} \, $$ ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Swift-function, which checks if all letters in a string are unique

This let arr = str.split(separator: "") creates an array of one-element substrings of the original string. It is simpler to create an array of ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Is Palindrom-function in Swift

Nitpicking: In English it is “palindrome” and not “palindrom”, so the function name should be isStringPalindrome. The parameter name ...
Martin R's user avatar
  • 24.2k
6 votes

Bubble Sort as extension to Swift-Arrays

The existing sort functions in the Swift standard library come in two flavors: A mutating method sort() which sorts a collection in-place and does not return a ...
Martin R's user avatar
  • 24.2k
6 votes
Accepted

Caesar Cipher in Swift (iOS-app)

My main point of criticism is that the encryption logic is in the view, actually in two views: the ContentView contains the code to shift one index forward or ...
Martin R's user avatar
  • 24.2k
6 votes

Showing an audio waveform from sample audio data with user interaction for zoom in/out

How can I implement this functionality more efficiently for smooth interaction? Pre-compute at different resolutions. maxValue = slice.max() Side note: it's not ...
J_H's user avatar
  • 42.2k
6 votes

Swift - Process file with key-value pairs

Nitpicking: let url = Bundle.main.path(forResource:"...") makes url an (optional) ...
Martin R's user avatar
  • 24.2k
5 votes
Accepted

Enum with associated value as stored property in Swift extension

First note that your var connectionState is computed property, not a stored property. It is computed using the get and ...
Martin R's user avatar
  • 24.2k
5 votes
Accepted

Computing the integer square root of large numbers

Your function is rather unnecessary. When converted to double, all 64-bit integers retain 1 sign bit and at least 52 precision bits. So the error is at most 11 bit; which means for any number N, the ...
W. Chang's user avatar
  • 379
5 votes
Accepted

Remove duplicates from array of non-equatable objects

First note that there is no need to make the SearchResult properties (implicitly unwrapped) optionals, as they are always initialized: ...
Martin R's user avatar
  • 24.2k
5 votes
Accepted

Downset calculation code

Naming According to the Swift API Design Guidelines, Names of types and protocols are UpperCamelCase. Everything else is ...
Martin R's user avatar
  • 24.2k

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