I
I am doing a swift curriculum. I need to find an assistant editor how it is shown on the page. But I can't find it. Please let me know if you know where is it. The first screenshot is how it should look and the second one is what I have now
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
editDistance() -> https://github.com/raywenderlich/swift-algorithm-club/tree/master/Minimum%20Edit%20Distance
It’s license -> https://github.com/raywenderlich/swift-algorithm-club/blob/master/LICENSE.txt
// Looks through the potentialMatches array to find the item that most closely matches the string in the first argument, and returns that string.
func closestMatch(for string: String, from potentialMatches: [String]) -> String {
// Initialize the best edit distance to the worst possible value
var bestEditDistance = Int.max
// Initialize the index of the best match to the first index
var bestMatchIndex = 0
for i in 0 ..< potentialMatches.count {
// Get the potential match at index i
// Get the edit distance from the string to the potential match
// If the distance calculated above is better than best edit distance,
// update the best edit distance and best match index
let potentialMatch = potentialMatches[i]
let distance = editDistance(from: string, to: potentialMatch)
if distance < bestEditDistance {
bestMatchIndex = i
bestEditDistance = distance
}
}
return potentialMatches[bestMatchIndex]
}
Could you please explain why we initialize bestEditDistance like Int.max ? I don't understand what does this instance mean.
I was doing an exercise about cleaning data. This is a previous code for searching data errors that works correctly:
// Create a Tabulator instance.
var tabulator = Tabulator()
// Loop through surveyData, incrementing the count for each response.
for data in surveyData {
tabulator.incrementCount(forValue: data)
}
// Loop through the tallied shows (stored in tabulator.values), printing the information from each one.
for i in tabulator.values {
tabulator.count(forValue: i)
}
showCatalog
print("\n\n***** FIRST CLEANING PASS *****\n\n")
// Print a header
print("\n\n***** TABULATION FOR VALID DATA ******\n\n")
showCatalog
// Loop through all tabulator values. Only print the count for those that are contained in showCatalog.
var k_normal = 0
var k_bad = 0
for a in tabulator.values {
if showCatalog.contains(a) == true {
k_normal += 1
} else {
k_bad += 1
print(a)
print(k_bad)
}
}
// Create a variable to keep a count of the errors.
// Print a header
print("\n\n***** DATA ERRORS ******\n\n")
// Loop through all tabulator values.
// If a value is not contained in showCatalog:
// - Increase the error count
// - Print it
// Print the error count.
Then I have a new exercise:
Some of the errors in the data seem to be simple capitalization mistakes. You've already seen a way to solve such problems in the QuestionBot app: Just convert the string to lowercase.
First, you'll need to have a lowercased version of your show catalog. Recall that you can create a lowercase string by using the lowercased() method.
Create a new catalog containing lowercased versions of all the shows.You should see the error count go from 13 down to just 3.
This is my next code that doesn't execute any correct or incorrect inputs. Please help me with that.
// Make a new array variable.
var newArray: [String] = []
// For all shows in showCatalog, add a lowercase version to the array.
for shows in showCatalog {
shows.lowercased()
newArray.append(shows.lowercased())
}
newArray
// Create a Tabulator instance.
var tabulator = Tabulator()
// Loop through surveyData. Make a lowercase version of each value, then increment its count.
for data in newArray {
tabulator.incrementCount(forValue: data)
}
// Loop through all tabulator values. Print only those that are contained in the lowercase version of the show catalog.
var k_normal = 0
var k_bad = 0
for a in tabulator.values {
if newArray.contains(a) == true {
k_normal += 1
} else {
k_bad += 1
print(a)
}
}
// Print a header
print(k_normal)
print(k_bad)
// Create a variable to keep a count of the errors.
// Loop through all tabulator values.
// If a value is not contained in the lowercase show catalog:
// - Increase the error count
// - Print it
// Print the error count.
Imagine you work for Streaming Plus, a video streaming service. The marketing team wants to know how to spend their advertising budget. To help them decide, the company has provided a survey to users about their favorite shows. The survey lets users type the names of shows rather than showing them a very long scrolling list.
One of your teammates at Streaming Plus has created a Tabulator type that you can use to process the survey data. A tabulator records the number of times it sees each unique String value you hand to it. It has the following properties and methods:
values: [String] A sorted array of the string values that have been tabulated.
func incrementCount(forValue value: String) Increments the count for the given value. If that value hasn't been seen before, it's added to the values array and its count is set to 1.
func count(forValue value: String) -> Int Returns the count for the given value. If that value has never been tabulated, the method will return 0.
On this page, you'll create an algorithm to tabulate the responses from this simulated survey and display the results. You'll use the same basic procedure in following pages.
There's a randomShowData array constant that simulates the results of a survey containing just 10 responses so you can test your code. It'll be different each time your code runs. (If you want, you can print the randomShowData to the console for verification.)
randomShowData
print(randomShowData)
Exercise: Using the comments as a guide, tally the survey results simulated in randomShowData.
This is my code but I am very not sure that I did it right
// Create a Tabulator instance.
var tabulator = Tabulator()
// Loop through the shows in randomShowData, incrementing the count for each one.
for i in 0 ... randomShowData.count - 1 {
print(tabulator.incrementCount(forValue: randomShowData[i] ))
}
// Loop through the tallied shows (stored in tabulator.values), printing the information from each one.
for b in tabulator.values {
print(tabulator.count(forValue: b))
}
Please correct me if I did something wrong :)
Hello! I am working with enums in the Playground now. I have an exercise:
_Comment out the final return statement to see an error. Uncomment it again, and try to change the value passed in to cookLunch so that the final else statement is called.
(Hint: How would you get an enum value that didn’t match anything in the if statement?)_
And I have this code:
enum LunchChoice {
case pasta, burger, soup
}
func cookLunch(choice: LunchChoice) -> String {
if choice == .pasta {
return "🍝"
} else if choice == .burger {
return "🍔"
} else if choice == .soup {
return "🍲"
} else {
return "Umm... how did we get here?"
}
}
cookLunch(.pizza)
I have tried to put a string value into the function, but then it was complaining about if statements with enums. Then I've tried to put a string value after an enum value, but it was complaining that by calling a function my first parameter with an enum wasn't mentioned. So I don't understand what to do
The exercise was:
For further practice, extend the Song struct below by adding a isLongerThan method. You can paste your code from page 6 as a starting point. Then use a loop 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.)
I've created this code to find out the longest song in my array:
struct Song {
let title: String
let artist: String
let duration: Int //seconds
/* Code from page 6 here, plus the new method */
var formattedDuration: String {
let minutes = duration / 60
// The modulus (%) operator gives the remainder
let seconds = duration % 60
return "\(minutes)m \(seconds)s"
}
}
let songs = [
Song(title: "Ooh yeah", artist: "Brenda and the Del-chords", duration: 90),
Song(title: "Maybe", artist: "Brenda and the Del-chords", duration: 200),
Song(title: "No, no, no", artist: "Fizz", duration: 150),
Song(title: "Makin' up your mind", artist: "Boom!", duration: 440)
]
var index = 0
var max = songs[0].duration
for i in 0 ... songs.count - 1 {
if songs[i].duration > max {
max = songs[i].duration
index = i
}
}
print("The longest song has a duration of \(max) seconds and this is a song number \(index) in the array")
print("This is " + songs[3].formattedDuration)
Tell me please if I did it right and are there any other options to do this exercise? :)
We're created a struct and a function that compares sizes of two rectangles:
struct Rectangle {
let width: Int
let height: Int
func isBiggerThan(_ rectangle: Rectangle) -> Bool {
let areaOne = width * height
let areaTwo = rectangle.width * rectangle.height
return areaOne > areaTwo
}
}
let rectangle = Rectangle(width: 10, height: 10)
let otherRectangle = Rectangle(width: 10, height: 20)
rectangle.isBiggerThan(otherRectangle)
otherRectangle.isBiggerThan(rectangle)
My exercise is:
Simplify the isBiggerThan method by creating a computed property named area for the rectangle struct and then using the computed property inside the isBiggerThan() method.
Could someone please explain how the previous code works and how to simplify this code by using a computed property
This was the first exercise:
Complete the missing code to process the parallel arrays from the first page of the playground.
And I've created a code:
let songTitles = ["Ooh yeah", "Maybe", "No, no, no", "Makin' up your mind"]
let artists = ["Brenda and the Del-chords", "Brenda and the Del-chords", "Fizz", "Boom!"]
let durations = [90, 200, 150, 440]
func songInformation(title: String, artist: String, duration: Int) -> String {
return "The song \(songTitles) by \(artists) has a duration of \(duration) seconds"
}
for i in 0 ... songTitles.count - 1 {
print(songInformation(title: songTitles[i], artist: artists[i], duration: durations[i]))
}
Then is the next exercise:
Below, use the Song struct from the previous page to simplify your code.
This is a struct:
struct Song {
let title: String
let artist: String
let duration: Int
}
And this is my code that doesn't work correctly:
/* Create the array of songs here */
let songs = ["Diamonds", "Animals", "Summer", "Now or never"]
/* Declare the songInformation function here */
func songInformations(song: String) -> String{
return "I love a song \(songs)"
}
/* Write a for...in loop here */
for a in 0 ... songs.count - 1 {
let song1 = Song(title: songs[a], artist: "Rihanna", duration: 100)
print(songInformations(song: song1))
}
Please help me to do this exercise. I don't understand what is wrong
This is an exercise:
Create an array literal with 20 to 25 items of sample data for your daily activity. It may be something like let milesBiked = [3, 7.5, 0, 0, 17 ... ] Feel free to make up or embellish the numbers, but make sure you have entries that are above, below and exactly at the goal you've thought of. Hint: Make sure to choose the right kind of array for your data, whether [Double] or [Int].
Write a function that takes the daily number as an argument and returns a message as a string. It should return a different message based on how close the number comes to your goal. You can be as ambitious and creative as you'd like with your responses, but make sure to return at least two different messages depending on your daily progress!
I've created an array:
let dailySteps = [10000, 332, 9485, 212, 5765, 3423, 9535, 5466, 2133, 9999, 13134, 7674, 9128, 12122, 12121, 100, 27183, 10004, 6521, 7318]
And I've created this function:
func myProgress(number: Int) {
for distance in number {
if distance > 10000 {
print("Yor are better then I thought!")
} else if distance == 10000 {
print("You have reached your goal!")
} else {
print("You need to walk more")
}
}
}
myProgress(number: dailySteps)
But I get errors like:
<For-in loop requires 'Int' to conform to 'Sequence'> in the second line of my function
<Cannot convert value of type '[Int]' to expected argument type 'Int'> when I try to call my function in the last line
I don't understand what mistakes I've just done
Could someone please explain me my mistake and explain how to make my code to work correctly? :)