Calculator is not taking the Average - swift

I was coding a calculator app on swift, and I am very new to swift. So I am lost with the syntax and everything. When I debug my code I get and error of on the code sayign division by 0. I have debugged and everything but I have no idea how to solve it any help would be greatly appreciated, I am just starting out swift and iOS. The application I am making right is for the mac terminal so my program takes the the users input from string and then converts it to an int.
This is the code I am working with
var average = 0;
let count = nums.count - 1
for index in 0...nums.count - 2 {
let nextNum = Int(nums[index])
average += nextNum!
}
return average / count
}

You are subtracting one from the array elements count, I assume due to the idea that zero based numbering affects it, but there is no need in this case.
You should check for an empty array since this will cause a division by zero. Also you can use reduce to simply sum up an array of numbers then divide by the count.
func average(of nums: [Float]) -> Float? {
let count = nums.count
if (count == 0) { return nil }
return nums.reduce(0, +) / Float(count)
}

There might be some reason for divisor be 0. As #MartinR said if there is only 1 object in nums then count = nums.count -1 would be zero and 1 / 0 is an undefined state.
One more issue I found that you are looping as 0...nums.count - 2 but it should be 0...nums.count - 1. You can also write it with less than condition as
0..<nums.count or 0..<count
Use,
var average = 0;
let count = nums.count
for index in 0..<count {
let nextNum = Int(nums[index])
average += nextNum!
}
return average / count
You can use the swift high-order functions for the optimised solution which will return 0 as average even if you do not have any number in your nums array. As:
let count = nums.count
let avg = nums.reduce(0, +) / count

Let try this:
var sum = 0;
let count = nums.count
for index in 0...nums.count - 1 {
let nextNum = Int(nums[index])
sum += nextNum!
}
return count != 0 ? sum/count : 0

Related

Swift Mini-Max Sum One Test Case Failed - HackerRank

Before anything else, I checked if this kind of question fits Stackoverflow, and based on one similar question (javascript) and from this question: https://meta.stackexchange.com/questions/129598/which-computer-science-programming-stack-exchange-sites-do-i-post-on -- it does.
So here it goes. The challenge is pretty simple, in my opinion:
Given five positive integers, find the minimum and maximum values that
can be calculated by summing exactly four of the five integers. Then
print the respective minimum and maximum values as a single line of
two space-separated long integers.
For example, . Our minimum sum is and our maximum sum is . We would
print
16 24
Input Constraint:
1 <= arr[i] <= (10^9)
My solution is pretty simple. This is what I could do best:
func miniMaxSum(arr: [Int]) -> Void {
let sorted = arr.sorted()
let reversed = Array(sorted.reversed())
var minSum = 0
var maxSum = 0
_ = sorted
.filter({ $0 != sorted.last!})
.map { minSum += $0 }
_ = reversed
.filter({ $0 != reversed.last!})
.map { maxSum += $0 }
print("\(minSum) \(maxSum)")
}
As you can see, I have two sorted arrays. One is incrementing, and the other one is decrementing. And I'm removing the last element of the two newly sorted arrays. The way I remove the last element is using filter, which probably creates the problem. But from there, I thought I could get easily the minimum and maximum sum of the 4 elements.
I had 13/14 test cases passed. And my question is, what could be the test case in which this solution will likely to fail?
Problem link: https://www.hackerrank.com/challenges/mini-max-sum/problem
Here
_ = sorted
.filter({ $0 != sorted.last!})
.map { minSum += $0 }
your expectation is that all but the largest element are added. But that is only correct it the largest element is unique. (And similarly for the maximal sum.)
Choosing an array with all identical errors makes the problem more apparent:
miniMaxSum(arr: [1, 1, 1, 1, 1])
// 0 0
A simpler solution would be to compute the sum of all elements once, and then get the result by subtracting the largest respectively smallest array element. I'll leave the implementation to you :)
Here is the O(n) solution:
func miniMaxSum(arr: [Int]) {
var smallest = Int.max
var greatest = Int.min
var sum = 0
for x in arr {
sum += x
smallest = min(smallest, x)
greatest = max(greatest, x)
}
print(sum - greatest, sum - smallest, separator: " ")
}
I know this isn't codereview.stackexchange.com, but I think some clean up is in order, so I'll start with that.
let reversed = Array(sorted.reversed())
The whole point of the ReversedCollection that is returned by Array.reversed() is that it doesn't cause a copy of elements, and it doesn't take up any extra memory or time to produce. It's merely a wrapper around a collection, and intercepts indexing operations and changes them to immitate a buffer that's been reversed. Asked for .first? It'll give you .last of its wrapped collection. Asked for .last? It'll return .first, etc.
By initializing a new Array from sorted.reversed(), you're causing an unecessary copy, and defeating the point of ReversedCollection. There are some circumstances where this might be necessary (e.g. you want to pass a pointer to a buffer of reversed elements to a C API), but this isn't one of them.
So we can just change that to let reversed = sorted.reversed()
-> Void doesn't do anything, omit it.
sorted.filter({ $0 != sorted.last!}) is inefficient.
... but more than that, this is the source of your error. There's a bug in this. If you have an array like [1, 1, 2, 3, 3], your minSum will be 4 (the sum of [1, 1, 2]), when it should be 7 (the sum of [1, 1, 2, 3]). Similarly, the maxSum will be 8 (the sume of [2, 3, 3]) rather than 9 (the sum of [1, 2, 3, 3]).
You're doing a scan of the whole array, doing sorted.count equality checks, only to discard an element with a known position (the last element). Instead, use dropLast(), which returns a collection that wraps the input, but whose operations mask the existing of a last element.
_ = sorted
.dropLast()
.map { minSum += $0 }
_ = reversed
.dropLast()
.map { maxSum += $0 }
_ = someCollection.map(f)
... is an anti-pattern. The distinguishing feature between map and forEach is that it produces a resulting array that stores the return values of the closure as evaluated with every input element. If you're not going to use the result, use forEach
sorted.dropLast().forEach { minSum += $0 }
reversed.dropLast().forEach { maxSum += $0 }
However, there's an even better way. Rather than summing by mutating a variable and manually adding to it, instead use reduce to do so. This is ideal because it allows you to remove the mutability of minSum and maxSum.
let minSum = sorted.dropLast().reduce(0, +)
let maxSum = reversed.dropLast().reduce(0, +)
You don't really need the reversed variable at all. You could just achieve the same thing by operating over sorted and using dropFirst() instead of dropLast():
func miniMaxSum(arr: [Int]) {
let sorted = arr.sorted()
let minSum = sorted.dropLast().reduce(0, +)
let maxSum = sorted.dropFirst().reduce(0, +)
print("\(minSum) \(maxSum)")
}
Your code assumes the input size is always 5. It's good to document that in the code:
func miniMaxSum(arr: [Int]) {
assert(arr.count == 5)
let sorted = arr.sorted()
let minSum = sorted.dropLast().reduce(0, +)
let maxSum = sorted.dropFirst().reduce(0, +)
print("\(minSum) \(maxSum)")
}
A generalization of your solution uses a lot of extra memory, which you might not have available to you.
This problem fixes the number of summed numbers (always 4) and the number of input numbers (always 5). This problem could be generalized to picking summedElementCount numbers out of any sized arr. In this case, sorting and summing twice is inefficient:
Your solution has a space complexity of O(arr.count)
This is caused by the need to hold the sorted array. If you were allowed to mutate arr in-place, this could reduce to `O(1).
Your solution has a time complexity of O((arr.count * log_2(arr.count)) + summedElementCount)
Derivation: Sorting first (which takes O(arr.count * log_2(arr.count))), and then summing the first and last summedElementCount (which is each O(summedElementCount))
O(arr.count * log_2(arr.count)) + (2 * O(summedElementCount))
= O(arr.count * log_2(arr.count)) + O(summedElementCount) // Annihilation of multiplication by a constant factor
= O((arr.count * log_2(arr.count)) + summedElementCount) // Addition law for big O
This problem could instead be solved with a bounded priority queue, like the MinMaxPriorityQueue in Google's Gauva library for Java. It's simply a wrapper for min-max heap that maintains a fixed number of elements, that when added to, causes the greatest element (according to the provided comparator) to be evicted. If you had something like this available to you in Swift, you could do:
func miniMaxSum(arr: [Int], summedElementCount: Int) {
let minQueue = MinMaxPriorityQueue<Int>(size: summedElementCount, comparator: <)
let maxQueue = MinMaxPriorityQueue<Int>(size: summedElementCount, comparator: >)
for i in arr {
minQueue.offer(i)
maxQueue.offer(i)
}
let (minSum, maxSum) = (minQueue.reduce(0, +), maxQueue.reduce(0, +))
print("\(minSum) \(maxSum)")
}
This solution has a space complexity of only O(summedElementCount) extra space, needed to hold the two queues, each of max size summedElementCount.
This is less than the previous solution, because summedElementCount <= arr.count
This solution has a time complexity of O(arr.count * log_2(summedElementCount))
Derviation: The for loop does arr.count iterations, each consisting of a log_2(summedElementCount) operation on both queues.
O(arr.count) * (2 * O(log_2(summedElementCount)))
= O(arr.count) * O(log_2(summedElementCount)) // Annihilation of multiplication by a constant factor
= O(arr.count * log_2(summedElementCount)) // Multiplication law for big O
It's unclear to me whether this is better or worse than O((arr.count * log_2(arr.count)) + summedElementCount). If you know, please let me know in the comments below!
Try this one accepted:
func miniMaxSum(arr: [Int]) -> Void {
let sorted = arr.sorted()
let minSum = sorted[0...3].reduce(0, +)
let maxSum = sorted[1...4].reduce(0, +)
print("\(minSum) \(maxSum)"
}
Try this-
func miniMaxSum(arr: [Int]) -> Void {
var minSum = 0
var maxSum = 0
var minChecked = false
var maxChecked = false
let numMax = arr.reduce(Int.min, { max($0, $1) })
print("Max number in array: \(numMax)")
let numMin = arr.reduce(Int.max, { min($0, $1) })
print("Min number in array: \(numMin)")
for item in arr {
if !minChecked && numMin == item {
minChecked = true
} else {
maxSum = maxSum + item
}
if !maxChecked && numMax == item {
maxChecked = true
} else {
minSum = minSum + item
}
}
print("\(minSum) \(maxSum)")
}
Try this:
func miniMaxSum(arr: [Int]) -> Void {
let min = arr.min()
let max = arr.max()
let total = arr.reduce(0, +)
print(total - max!, total - min!, separator: " ")
}

Basic code that counts the number of digits in a given number, but why is it working?

I wrote this function but, while I'm happy it works, I don't seem to understand why.
This function just give us the number of digits in a given number passed in it. My question is:
Since I'm only dividing a number by 10, shouldn't the while loop be infinite ? Since it will always be greater than 0. Example: if I pass in 250, it should be:
25; counter = 1
then
2.5 ; counter = 2
then
0.25; counter = 3
then
0.025; counter = 4
etc...
func count(_ num: Int) -> Int {
var counter = 0
var number = num
while number > 0 {
number = number / 10
counter += 1
}
return counter
}
It wont be an infinite loop because you are using an Int. Therefore, there is no rest of the division, that means that when you divide for example 2/10 , the result would be 0.
If you are doing it in Swift, why don't you simply write,
let number = 250
let str = String(number)
print(str.count) //3
After all you want to count the number of digits in a given Int value right?

Division of array of doubles

I have array of Doubles:
var dates: [Double] = [1542412800000,
1542499200000,
1543017600000,
1543708800000,
1544659200000,
1547164800000,
1550880000000]
(yes, it's actually date timestamps). What I want is to transform it to an array of percentages. For example, if I had an array of [5, 20, 25], I want an output of [0.20, 0.8, 1], percentages of current values. I ended up with:
let percentages: [Double] = dates
.map{$0 - dates.first!}
.map{$0/dates.last!}
percentages.forEach{ it in
print(it)
}
But output is:
0.0
5.5710306406685235e-05
0.00038997214484679665
0.0008356545961002785
0.0014484679665738162
0.003064066852367688
0.005459610027855153
Second value kind of weird. How to solve this?
When dividing every value by the last value in the last map statement, you are ignoring the fact, that everything has already been subtracted by the first value.
To fix this, you should divide by the range of values (the difference between the maximum value and the minimum value).
Assuming your array is sorted, this will be:
guard let maxValue = dates.last, let minValue = dates.first else {
return
}
let percentages = dates
.map {$0 - minValue}
.map {$0 / (maxValue - minValue)}
This will normalize all values such that the first value is 0 and the last value is 1 and everything else is in between.
If you do not want to normalize the first value to 0 (but keep everything between 0 and 1), you can omit the subtraction step:
let percentages = dates.map {$0 / maxValue}
If your array is not sorted, you can use the .min() and .max() functions of your array:
guard let maxValue = dates.max(), let minValue = dates.min() else {
return
}
You can do it this way :
let dates: [Double] = [1542412800000,
1542499200000,
1543017600000,
1543708800000,
1544659200000,
1547164800000,
1550880000000]
let sorted = dates.sorted()
guard let first = sorted.first,
let last = sorted.last,
last != first
else {
fatalError()
}
let denominator = last - first
let percentages = sorted.map { ($0 - first)/denominator }
print(percentages) //[0.0, 0.01020408163265306, 0.07142857142857142, 0.15306122448979592, 0.2653061224489796, 0.5612244897959183, 1.0]
Using your code style you should use something like this:
var dates: [Double] = [1542412800000,
1542499200000,
1543017600000,
1543708800000,
1544659200000,
1547164800000,
1550880000000]
let intermediate: [Double] = dates
.map{$0 - dates.first!}
let percentages = intermediate
.map{$0/intermediate.last!}
percentages.forEach{ it in
print(it)
}
The real problem that you divide each element by 'initial' maximum value (not shifted by minimum value).

index(of: Int) method of Range in swift return incorrect value

When using index(of: Int) method on a half open range object, it always returns an incorrect value if the range does not start at 0. See the code below.
let range = (3 ..< 10)
let indexOfRange = range.index(of: 5) // return 5
let array = Array(5 ..< 10)
let indexOfArray = array.index(of: 5) // returns 0
I don't understand why such result is produced. Can anyone please explain?
Indices are opaque objects. If it's not an array, you shouldn't assume they are zero-based or even that they are integers (for an example see String.Index). To get a zero-based integer index, you need to get the distance from the startIndex:
let range = (3 ..< 10)
let opaqueIndex = range.index(of: 5)
let integerIndex = range.distance(from: range.startIndex, to: opaqueIndex!)
print(integerIndex) // 2
However, for Int ranges that's basically the same as:
let integerIndex = 5 - range.lowerOffset
The interesting part is that it seems ranges cannot be subscripted (ambiguous definition), therefore there is probably no point to get the index in the first place.

What's the most efficient way to determine if a Swift string is shorter than a certain length

s is a native Swift string consisted of ASCII characters only. It could be arbitrary long. What's the most efficient way to figure out if s is short than a certain length (say, 100k)?
if countElements(s) < 100_000 is not the most efficient, as countElements is O(n) complexity and s could have billions of characters.
If you're sure you don't need to worry about anything other than ASCII, you can use the utf16Count property (which is the length property of the bridged NSString):
let stringLength = superLongString.utf16Count
If you want to be able to handle Unicode you need to walk the string, you just don't want to walk the whole string. Here's a function to count just up to your limit:
func lengthLessThanMax(#string: String, maximum max: Int) -> Bool {
var idx = string.startIndex
var count = 0
while idx < string.endIndex && count < max {
++count
idx = idx.successor()
}
return count < max
}
lengthLessThanMax(string: "Hello!", maximum: 10)
// true
lengthLessThanMax(string: "Hello! Nice to meet you!", maximum: 10)
// false
just chose what your want:
var emoji = "👍"
countElements(emoji) //returns 1
emoji.utf16Count //returns 2
emoji.bridgeToObjectiveC().length //returns 2
find from Get the length of a String