I am currently going through Apple Curriculum books and stumbled across a problem I can't figure out.
There is a structure:
struct Song {
let title: String
let artist: String
let duration: Int
var longestSong = false
}
and an array with 4 songs that have different duration: 90, 200, 150, 440.
The book asks to add a method that checks whether one song is longer than the other.
So, based on previous material I added the following method to my struct:
func isLongerThan(_ song: Song) -> Bool {
return duration > song.duration
}
}
Then the book asks to use a loop in order to find the longest song in the array and print it to the console
(Hint: use a variable to keep track of the longest song and initialize
it to the first one in the array.)
So, I came up with this loop:
for (index, _) in songs.enumerated() {
if index < songs.count-1{
if songs[index].isLongerThan(songs[index + 1]){
songs[index].longestSong = true
print("\(songs[index].title) is longer than \(songs[index+1].title)")
}
} else {
if songs[songs.count-1].isLongerThan(songs[0]){
songs[songs.count-1].longestSong = true
print("\(songs[songs.count-1].title) is longer than \(songs[0].title)")
}
}
}
The next step is to compare those songs with longestSong = true variable until only one song remains. But this isn't a good solution in my opinion.
Is there a better and more elegant way to solve this problem?
UPD
It seems that I have misread the instructions and have gone the wrong way.
With some help I managed to find this solution:
func searchForLongestSong(songs: [Song]) {
var lngstSongIndex = 0
for song in 0..<songs.count {
if songs[song].duration > songs[lngstSongIndex].duration {
lngstSongIndex = song
}
}
print("The longst song is \"\(songs[lngstSongIndex].title)\"")
}
searchForLongestSong(songs: songs)
Since it is an Array you can keep track of the "longest song" by saving the index of the longest song.
Create an Int var to save the index.
Loop through the indices for n in 0..<songs.count
if the duration of songs[n] is greater than songs[largestSongIdx]
Then update the longestSong variables for both accordingly
Related
iOS 14, Swift 5.x
I watched this excellent WWDC from 2018
https://developer.apple.com/videos/play/wwdc2018/223/
And I wrote a shapes editor... and have been trying to use partition as Dave in the video says you should. I got the first three to work, but the last one I had to use a loop- cannot for the life of me figure out how to get it to work with partition.
Can someone see how I might do this?
The first method moves the selected object to the end of the list, works perfectly.
func bringToFrontEA() {
let subset = objects.partition(by: { $0.selected })
let selected = objects[subset...]
let unselected = objects[..<subset]
let reordered = unselected + selected
objects = Array(reordered)
}
The second method moves the selected object to the front of the list. Works prefectly.
func sendToBackEA() {
let subset = objects.partition(by: { !$0.selected })
let selected = objects[subset...]
let unselected = objects[..<subset]
let reordered = unselected + selected
objects = Array(reordered)
}
The third method moves the element just one element back in the list. Works perfectly.
func sendBackEA() {
if let i = objects.firstIndex(where: { $0.selected }) {
if i == 0 { return }
let predecessor = i - 1
let shapes = objects[predecessor...].partition(by: { !$0.selected })
let slice = objects[predecessor...]
let row = objects[..<predecessor]
let selected = Array(slice[..<shapes])
let unselected = Array(slice[shapes...])
objects = row + selected + unselected
}
}
The last method moves the element forward in the list, works perfectly... but unlike the other methods it will not scale as described in the WWDC video.
func bringForwardEA() {
let indexes = objects.enumerated().filter { $0.element.selected == true }.map{$0.offset}
for i in indexes {
if objects[i+1].unused {
return
}
objects.swapAt(i+1, i)
}
}
Objects is an array of shapes with a property indicating if it is selected or not. I want to exchange the loop in the last method by using a partition as I did in the first three. It needs to work for one or more selected shapes.
Looking at the WWDC video, it appears that what you are calling sendBackEA is what WWDC calls bringForward, and what you are calling bringForwardEA is what WWDC calls sendBack.
Just like how you move the first selected element forward one index (index decreases) in sendBackEA, then move all the other selected elements to immediately after that first selected element. bringForwardEA should do the reverse: move the last selected element backward one index (index increases), then move all the other selected elements to immediately before the last selected element. (See circa 19:10 in the video)
You seem to have confused yourself by trying to increase the indices of all the selected index by 1. This obviously cannot be done with a partition in general.
Also note that partition(by:) already modifies the collection, you don't need to get each partition, then recombine.
Your 4 methods can be written like this:
func bringToFrontEA() {
objects.partition(by: { $0.selected })
}
func sendToBackEA() {
objects.partition(by: { !$0.selected })
}
func sendBackEA() {
if let i = objects.indices.first(where: { objects[$0].selected }) {
if i == 0 { return }
let predecessor = i - 1
objects[predecessor...].partition(by: { !$0.selected })
}
}
func bringForwardEA() {
if let i = objects.indices.last(where: { objects[$0].selected }) {
if i == objects.indices.last { return }
let successor = i + 1
objects[...successor].partition(by: { !$0.selected })
}
}
Notice the symmetry between sendBackEA and bringForwardEA.
I'm building a clone banking app at the moment, and one of the things I'm trying to do is add 'Split Transaction' (which can then be shared with a set of friends paying a given amount each).
Initially, the transaction is split equally amongst friends (unless it doesn't split equally, in which case the remainder gets added on to one unlucky friend). The user can then manually adjust the amount each pays, which then updates the others. If the user has manually adjust an amount for a friend, this friends 'split' doesn't get updated automatically if the user then adjust another friend's amount (i.e. if the user says friend1 pays £12, it will always be £12 until the user says otherwise).
I've been fiddling for a while trying to make the method as concise and 'swifty' as possible - but I'd really appreciate any feedback on my approach.
For the purposes here, I'm only trying split the money equally between people (but I still wanted to explain the 'user defined split' so the current code makes sense).
I'm using https://github.com/Flight-School/Money to represent the transaction value, all within a Transaction class. I need to round quite a bit to ensure the split and remainder stick to 2 decimal places. Here's the relevant code:
A struct to hold an amount along with if the user set it or not (needs to be custom object for codable reasons):
struct SplitTransactionAmount: Codable {
let amount: Money<GBP>
let setByUser: Bool
}
A dictionary to hold the friend names, along with their split, and if it's set by the user - also a namesOfPeopleSplittingTransaction array for easy display.
var splitTransaction: [String: SplitTransactionAmount]
var namesOfPeopleSplittingTransaction = [String]()
And here's the method to split the transaction:
private func splitTransaction(amount: Money<GBP>, with friends: [String]) -> [String: SplitTransactionAmount] {
//First we remove any duplicate names.
let uniqueFriends = friends.removingDuplicates()
//Create an empty dictionary to hold the new values before returning.
var newSplitTransaction = [String: SplitTransactionAmount]()
let totalAmountToSplitRounded = amount.rounded.amount
let numberOfSplitters = uniqueFriends.count
let eachTotalRaw = totalAmountToSplitRounded / Decimal(numberOfSplitters)
let eachTotalRounded = Money<GBP>(eachTotalRaw).rounded.amount
let remainder = totalAmountToSplitRounded - (Decimal(numberOfSplitters) * eachTotalRounded)
if remainder == 0 {
//If the amount to split each goes in to the total with no remainder, everyone pays the same.
for friend in uniqueFriends {
newSplitTransaction[friend] = SplitTransactionAmount(amount: Money(eachTotalRounded), setByUser: false)
}
} else {
for friend in uniqueFriends {
if friend == uniqueFriends.first! {
//Unlucky first friend has to pay a few pence more!
newSplitTransaction[friend] = SplitTransactionAmount(amount: Money(eachTotalRounded + remainder), setByUser: false)
} else {
newSplitTransaction[friend] = SplitTransactionAmount(amount: Money(eachTotalRounded), setByUser: false)
}
}
}
return newSplitTransaction
}
I think the problem I'm finding is the code makes perfect sense to me, but I'm not sure how clear it is to an outside reader. Any thoughts on my approach would be much appreciated (and sorry for the long question!). And I'd also love to know if there's anyway to write this more concisely!
Many thanks
IMO, Simplify into a single loop over two loops on different ifs . And reduce repeated code (e.g init of SplitTransactionAmount)
for (index, friend) in uniqueFriends.enumerated(){ //or if you don't prefer enumerated, use == on first index like before.
let money: Money<GBP>
if remainder != 0, index == 0 { //ensure remainder is only used if has value.
//Unlucky first friend has to pay a few pence more!
money = Money(eachTotalRounded + remainder)
} else {
money = Money(eachTotalRounded)
}
newSplitTransaction[friend] = SplitTransactionAmount(amount: money setByUser: false)
}
I have a list of prices and want to find the minimum price EXCLUDING the first element (this is a subset of another problem on HackerRank).
My version is too slow and times out. I suspect this is due to my ArraySlice.
Here is my (working) code:
func calculateMins(prices: [Int]) {
for j in 1..<prices.count {
let lowestPreviousPrice = prices[1...j].min()!
print (lowestPreviousPrice)
}
}
calculateMins(prices: [4,8,2,4,3])
Is there a better performing version of this, perhaps one that does not use an ArraySlice?
Just use dropFirst() function.
var array = [1,8,2,4,3]
var array2 = array.dropFirst() // Returns a subsequence containing all but the first element of the sequence.
array2.min() // 2
Why not keep it simple
func calculateMins(prices: [Int]) {
var min = Int.max
for i in 1..<prices.count {
if prices[i] < min { min = prices[i] }
}
print(min)
}
You have few options to solve this issue.
//Default way
prices.dropFirst().min()
//Functional way
prices.dropFirst().reduce(Int.max, { min($0, $1) })
You could also use suffix, which is quite same as dropFirst that this version could crash if in case array is empty.
array.suffix(from: 1).min()
While this may not be a good example, but as the question states, I wish to compare randomNo to the sets within numberSets. However, the moment one number is found I want to know if there is a way to skip to the next set.
In summary randomNo contains 2 numbers which can be found in the same set these are "6" and "9". I want to know if the moment I find "6" and can void the rest of the set and move onto the next set without cycling through the rest of the numbers in the set
init() {
let numberSet1 : Set<Int> = [1,2,3,4,5]
let numberSet2 : Set<Int> = [6,7,8,9,10]
let numberSet3 : Set<Int> = [11,12,13,14,15]
let randomNo = [3,6,9,11]
numberSets = [numberSet1,numberSet2,numberSet3]
}
func searchFor(){
for num in randomNo{
for set in numberSets{
if set.contains(num) {
print("The following number was found: ", num)
}
}
}
}
One way to do this is to continue the outer loop:
outer: for num in randomNo{
for set in numberSets{
if set.contains(num) {
print("The following number was found: ", num)
continue outer
}
}
}
Another way is to union all three sets:
let union = numberSet1.union(numberSet2).union(numberSet3)
print(randomNo.filter(union.contains))
First, I think it worth to mention that, in your example the code is not cycling through the sets, rather than arrays of sets (randomNo, numberSets).
If I get the problem right, you do not need to optimize looking up for element in set. Asking whether set contains element or not (a lookup), is not an expensive operation and has complexity of O(1).
If want to stop iterating through the numberSets once first number is found, just use break control flow statement:
func searchFor() {
for num in randomNo {
for set in numberSets {
if set.contains(num) {
print("The following number was found: ", num)
break
}
}
}
}
Hope it helps.
I'm new to programming and have been learning Swift by doing a number of online courses. In one of the courses we built a basic trivia game and I've been progressively trying to improve it by doing my own coding (best way to learn!).
Recently I came across what's called a Fisher-Yates shuffle and, after much trial and error (and with the help of the stack overflow community) was able to use GKRandomSource from Swift's Gameplaykit to shuffle my trivia questions around so that they were being asked randomly. This was an improvement on the original arc4random code I was using because the shuffle removed questions already asked from the overall pool of questions, thereby ensuring that they did not repeat (at least in iOS9).
This works well within the session, but once the user quits the app and relaunches it, the shuffle starts from scratch. So I was looking at a way to have the app 'remember' the questions already asked between sessions. My research led me to the idea of seeding and I've been trying to get this to work with my GKRandomSource code, but I'm obviously missing something.
Any advice etc would be most welcome - especially since I'm not entirely sure that this 'seeding' approach will achieve my ultimate aim of not repeating questions already asked in previous sessions of the app.
Below are what I believe to be the relevant bits of my revised code.
All questions and potential answer choices are stored in a .json file as such:
{
"id" : "1",
"question": "Earth is a:",
"answers": [
"Planet",
"Meteor",
"Star",
"Asteroid"
],
"difficulty": "1"
}
I use the following code to load the .json file:
func loadAllQuestionsAndAnswers()
{
let path = NSBundle.mainBundle().pathForResource("content", ofType: "json")
let jsonData : NSData = NSData(contentsOfFile: path!)!
allEntries = (try! NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)) as! NSArray
//println(allEntries)
}
And below is my most recent code for trying to achieve the shuffle of all questions and replicate it in future sessions):
var allEntries : NSArray!
var shuffledQuestions: [AnyObject]!
var nextQuestion = -1
var mySeededQuestions : [AnyObject]
loadAllQuestionsAndAnswers()
if #available(iOS 9.0, *) {
let lcg = GKLinearCongruentialRandomSource(seed: mySeededQuestions)
let shuffledQuestions = lcg.arrayByShufflingObjectsInArray(allEntries)
nextQuestion++
loadQuestion(nextQuestion)
// Fallback on earlier versions
}else{
let randomNumber = Int(arc4random_uniform(UInt32(allEntries.count)))
loadQuestionPreiOS9(randomNumber)
}
I know at the very least I have a problem with the above code, but I'm at a loss. I'm also thinking that maybe I'm missing a step in terms of storing the seed?
For the sake of completeness, I use a label to display the question and four images to display the potential answers, using the following code:
func loadQuestion(index : Int)
{
let entry : NSDictionary = shuffledQuestions[index] as! NSDictionary
let question : NSString = entry.objectForKey("question") as! NSString
let arr : NSMutableArray = entry.objectForKey("answers") as! NSMutableArray
//println(question)
//println(arr)
labelQuestion.text = question as String
let indices : [Int] = [0,1,2,3]
//let newSequence = shuffle(indices)
let newSequence = indices.shuffle()
var i : Int = 0
for(i = 0; i < newSequence.count; i++)
{
let index = newSequence[i]
if(index == 0)
{
// we need to store the correct answer index
currentCorrectAnswerIndex = i
}
let answer = arr.objectAtIndex(index) as! NSString
switch(i)
{
case 0:
buttonA.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 1:
buttonB.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 2:
buttonC.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 3:
buttonD.setTitle(answer as String, forState: UIControlState.Normal)
break;
default:
break;
}
}
buttonNext.hidden = true
// we will need to reset the buttons to reenable them
ResetAnswerButtons()
}
func loadQuestionPreiOS9(index : Int)
{
let entry : NSDictionary = allEntries.objectAtIndex(index) as! NSDictionary
let question : NSString = entry.objectForKey("question") as! NSString
let arr : NSMutableArray = entry.objectForKey("answers") as! NSMutableArray
//println(question)
//println(arr)
labelQuestion.text = question as String
let indices : [Int] = [0,1,2,3]
//let newSequence = shuffle(indices)
let newSequence = indices.shuffle()
var i : Int = 0
for(i = 0; i < newSequence.count; i++)
{
let index = newSequence[i]
if(index == 0)
{
// we need to store the correct answer index
currentCorrectAnswerIndex = i
}
let answer = arr.objectAtIndex(index) as! NSString
switch(i)
{
case 0:
buttonA.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 1:
buttonB.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 2:
buttonC.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 3:
buttonD.setTitle(answer as String, forState: UIControlState.Normal)
break;
default:
break;
}
}
buttonNext.hidden = true
// we will need to reset the buttons to reenable them
ResetAnswerButtons()
}
Finally, I use the following code to present the user with a 'Next' button after they've answered a question:
#IBAction func PressedButtonNext(sender: UIButton) {
print("button Next pressed")
if #available(iOS 9.0, *) {
nextQuestion++
loadQuestion(nextQuestion)
}else{
let randomNumber = Int(arc4random_uniform(UInt32(allEntries.count)))
loadQuestionPreiOS9(randomNumber)
}
I know my coding is probably quite verbose and unnecessary, but up until this latest improvement it's been working fine and I actually understand most of it (I think!)
There are really two questions here: what you're asking about and what you seem to want. They're both worth answering for different reasons, so...
How to seed a GK(Whatever)RandomSource
(All the GKRandomSource subclasses have seeds, even though the superclass GKRandomSource itself doesn't... that's because each class has its own data type for seeds. But the usage is the same.)
The critical bits of the code you've posted don't even compile due to a type mismatch: the seed/init(seed:) value for GKLinearCongruentialRandomSource is an integer, not an array of objects. The documentation for that value spells out what it's for (emphasis added) and how to use it:
Any two random sources initialized with the same seed data will generate the same sequence of random numbers. To replicate the behavior of an existing GKLinearCongruentialRandomSource instance, read that instance’s seed property and then create a new instance by passing the resulting data to the initWithSeed: initializer.
So, if you want to replicate a sequence of random numbers:
Create a random source with the plain initializer.
let source = GKLinearCongruentialRandomSource()
Save off that source's seed value.
let seed = source.seed // -> some UInt64 value
// write seed to user defaults, a file, a web service, whatever.
Use that random source for whatever.
Later, when you launch again and want the same sequence, read in the seed value and create a random source using the seed.
let seed = // read in seed value from wherever you saved it
let source = GKLinearCongruentialRandomSource(seed: seed)
This still doesn't get you what you're actually looking for, though: If source in step 1 produced the sequence 1, 6, 3, 9, 2, 7, source from step 4 will also produce the sequence 1, 6, 3, 9, 2, 7 — the seed doesn't record where you "left off" in a sequence. Or, since you're using it for an array shuffle, it'll produce the same shuffled ordering of the array as the first shuffle, but it doesn't remember what you did with the shuffled array thereafter.
How to use a shuffled ordering across multiple app launches
If you want to shuffle an array, walk through it in order, and then on a later run of your app continue walking through the same shuffled array from where you left off, you need to design around that requirement.
Shuffle on the first launch.
Record something about the ordering produced. (Say, a mapping of indices in the shuffle to indices in the original data.)
When walking through the shuffled array, record how far you've gone through it.
On later runs of the app, use the record of the ordering and the record of progress to decide where you are.
Here's a rough pass at that. (Note that I'm not touching your data model — this is a program design question, and SO is not a coding service. You'll need to think about how to flesh out this design to match your model and its use cases.)
struct Defaults {
static let lastQuestionIndex = "lastQuestionIndex"
static let questionOrder = "questionOrder"
}
let questions: [Question] // array of model objects, always in fixed order
func nextQuestion() -> Question {
let defaults = NSUserDefaults.standardUserDefaults()
if let lastIndex = defaults.integerForKey(Defaults.lastQuestionIndex) {
// we've run before, load the ordering
guard let shuffledOrder = defaults.arrayForKey(Defaults.questionOrder) as? [Int]
else { fatalError("save questionOrder with lastQuestionIndex") }
// advance the saved index so the next call to this function
// will get the next question
if lastIndex + 1 < count {
defaults.setInteger(lastIndex + 1, forKey: Defaults.lastQuestionIndex)
} else {
// ran out of shuffled questions, forget the order so we
// can reshuffle on the next call
defaults.removeObjectForKey(Defaults.questionOrder)
defaults.removeObjectForKey(Defaults.lastQuestionIndex)
}
// map lastQuestionIndex from sequential to shuffled
// and return the corresponding answer
let shuffledIndex = shuffledOrder[lastIndex]
return questions[shuffledIndex]
} else {
// first run, shuffle the question ordering (not the actual questions)
let source = GKRandomSource()
let sequentialOrder = Array(0..<questions.count)
let shuffledOrder = source.arrayByShufflingObjectsInArray(sequentialOrder)
// save the ordering, and the fact that we're asking the first question
defaults.setObject(shuffledOrder, forKey: Defaults.questionOrder)
defaults.setInteger(0, forKey: Defaults.lastQuestionIndex)
// return the first question in the shuffled ordering
let shuffledIndex = shuffledOrder[0]
return questions[shuffledIndex]
}
}
That's probably a bit pseudocode-ish (so you might need to worry about casting arrays to work with NSUserDefaults, etc), but as a general design it should be enough to give you some food for thought.
You can also use the following to drop off a certain amount of values so if you keep a roll count dropping that many on next start is as easy as :
arc4.dropValues(rollCount)