UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Extra spice for project 5

Forums > 100 Days of SwiftUI

Hey!

After completing project 5 (WordScramble) i wanted to improve it with showing the total amount of points you can achive with each word.

Iv been trying to loop through all the words using the rootword to get all "possible" words from the word array. So i would like the loop to put all possible words that can be created from the letters from a specific word to be put into an array. However i cannot get it to work.

func maximumScores() {
        for word in allWordsArray {
            // A loop that loops through each item in the "allWordsArray", which contains 12382 different words.
            var tempWord = rootWord
            // The word which the user shall use to try to create other words with.
            for letter in word {
                if let pos = tempWord.firstIndex(of: letter) {
                    tempWord.remove(at: pos)
                } else {
                    break
                }
                matchingWordsArray.append(word)
                // Its around here i have issues. I cannot figure out how to get the loop to work properly. As it is now it takes seemingly random words and put it into the array.
            }
        }
    }

This is how far i have come so far. I could need some tips on how to do this. =)

2      

@Bnerd  

I tried some for loops trying to remove words that don't include the rootword's characters, but the iterating between all words and for each character takes ages..moreover it was not very accurate..

3      

I can give you a function that will give you all possible permutations of a string, you can modify it for an array of String, you can give it a try i guess ...

func allPossible(str: String, current: String = "") -> [String] {
    var result: [String] = []

    if str.isEmpty  {
            return [current]
        } else {

                for i in 0..<str.count {
                    let left = String(Array(str)[0 ..< i])
                    let right = String(Array(str)[i+1 ..< str.count])

                    result += allPossible(str: left + right, current: current + String(Array(str)[i]))
                }

        }
        return result
}
var data = allPossible(str: "abc")
print(data)

["abc", "acb", "bac", "bca", "cab", "cba"]

4      

Amit's function is fantastic!

But only solves part of the problem!

Come for the SwiftUI lessons, get a bonus maths lesson free!

(Please double check my maths!)

An 8 letter word ("gasoline") will have 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 permutations. Roughly 40,320 ways to combine permutate all eight letters. Amit's function verifies this number! Nice. Next, you'll have to run all 40,320 of these throught the dictionary to determine if the word is valid or not. Most of those words will be rubbish, of course.

But the game allows you to create words of any size between 3 and 8 letters.

A 7 letter word will have 7 x 6 x 5 x 4 x 3 x 2 x 1 permutations. Roughly 5,038 ways to permutate seven letters.

How many ways can you combine seven letters from a set of eight letters? If you exclude one letter from an 8-letter word, you're left with seven letters. And there are 8 possible letters to exclude, so 8. There are 8 possible ways to pick seven letters from a set of 8.

-g asoline
-a gsoline
-s gaoline
-o gasline
-l gasoine
-i gasolne
-n gasolie
-e gasolin

8 x 7! = 40,320 additional unvalidated seven letter words missing from Amit's function.

Combinations and Permutations

How many ways can you permutate six letters from a set of eight letters? This is the k-permutations of n-elements problem.

I think that's about 20,000 6-letter permutations, each of which needs to be run throught Amit's function to find possible six letter words. For each of the 20,000 6-letter permutations, you'll get a list of 6 x 5 x 4 x 3 x 2 x 1 = 720 possible words. That's 720 x 20,000 more unvalidated six letter words you'll have to run through the dictionary.

Then you'll need to gronkulate the 5-letter, 4-letter, and 3-letter calculations.

Yikes! Someone can finish this problem. I have a headache!

PS: High probability (VERY high) I borked the concepts of permutations and combinations. Please correct me!

3      

Thanks for your replies! I will see if i can do something with this and come back.

What i have been trying to do before is to use the "isPossible" func from Paul to check each word in the array to the rootword and check if it is possible. And if the function returns true, the word, is added into an array, in my mind it should work, but it is not. x)

func isPossible(word: String) -> Bool {
        var tempWord = rootWord

        for letter in word {
            if let pos = tempWord.firstIndex(of: letter) {
                tempWord.remove(at: pos)
            } else {
                return false
            }
        }
        return true
    }

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.