Skip to main content

Posts

Showing posts from 2020

Coding Exercise

Coding Exercise

In this excercise we asked two developers to create solution for the very same problem. There is an input array of number of votes (eg. [3, 5, 6, 2, 0]) and they need to provide a whole(!) percentages of each vote considering the total number of votes (eg. [Int]), the sum of the percentages (obviously) need to to 100 at the end of the day – which requires them to cheat mathematics a bit, as rounding the percetages to whole number may have a loss or an overflow. We got two Swift developers from different levels and experience to complete the task on their own chosen way. Here are the results, which you can run in Playgound in Xcode, anytime (copy paste). Developer #1 func responsePercentages1(fromVotes votes: [Int]) -> [Int] { let totalResponseCount = votes.reduce(0, +) var percentages = [Double]() for vote in votes { if totalResponseCount == 0 { percentages.append(0.0) } else { percentages.append(Double(vote) / Double(totalResp...