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, ...
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 ...
8
votes
"What To Do" App
The instance variables
var userText : String = " "
var randomNumber : Int = 0
are not needed: userText is only used within <...
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 ...
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 ...
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.) ...
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:
...
7
votes
Accepted
Leetcode 102: Binary tree level-order traversal in Swift
Simplifying your code
Let's start here:
...
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 ...
6
votes
Wrapping the iOS triple buffering procedure in a generic swift class
Your platform-dependent code
...
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. ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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:
...
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
...
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} \,
$$
...
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 ...
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 ...
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 ...
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 ...
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 ...
6
votes
Swift - Process file with key-value pairs
Nitpicking: let url = Bundle.main.path(forResource:"...") makes url an (optional) ...
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 ...
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 ...
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:
...
5
votes
Accepted
Downset calculation code
Naming
According to the Swift API Design Guidelines,
Names of types and protocols are UpperCamelCase. Everything else is ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
swift × 660ios × 215
performance × 53
programming-challenge × 45
swift3 × 43
beginner × 38
algorithm × 30
strings × 30
array × 29
object-oriented × 23
json × 21
uikit × 21
generics × 20
cocoa × 19
swiftui × 18
design-patterns × 15
functional-programming × 14
comparative-review × 14
protocols × 14
time-limit-exceeded × 13
mvc × 13
extension-methods × 13
image × 12
primes × 12
hash-map × 11