Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Programming Challenge: Are You a Swift Ninja? Part 1

Do you consider yourself a Swift Ninja? Take our programming challenge! Beginners are also welcome to follow through and learn the craft.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2329-programming-challenge-are-you-a-swift-ninja-part-1

Your solution of the challenge 2 doesn’t really answer the point “If no parameters pass to the function, it will return the string “none”.” but more “If the sum of the 2 strings is empty then return none”

Would it be more correct to write this ?

func flexStrings(s1 s1: String = “none”, s2: String = “”) -> String {
return s1 + s2
}

1 Like

Yeah that’s a nice solution - and shorter too :+1:

please someone explain to me how challenge one works. How on earth does rearranging a tuple swap the values of two variables?? I understand tuples (i thought), inout functions etc. I feel like ive never encountered this before however.

var x = 1
var y = 2

how does
(x,y) = (y,x)

have anything to do with the values assigned to x and y?

Imagine tuples like little anonymous arrays (like closures are anonymous functions - you construct them only to use them once - most often to assign them to a variable).

You can access the members of a tuple by their numeric index (since they are not named)

so (x, y) = (y, x) becomes -> (anonymous1.0, anonymous1.1) = (anonymous2.0, anonymous2.1)

so then anonymous2.0 and anonymous2.1 get assigned the values of x and y, and then those are assigned to anonymous1.0 and anonynous1.1

Finally since the two members of anonymous1 are in fact the variables x and y, you get the final values assigned to those two variables…

Three things on Challange #3:

1 . There is a known memory leak issue when using optional binding with switch like in the solution.

https://bugs.swift.org/browse/SR-1339?jql=text%20~%20"switch%20case%20leak"

2 . For people (like me) who like to avoid Any anywhere possible might have found that let result1 = sumAny(Double(), 10, "-10", 2) prints 2 instead.

This is because that’s how NSNumber behaves when the fractional digit is 0. It just drops it.

print((1.0 as? NSNumber) as? Int) // Optional(1)
print(1.0 as? Int) // nil

3 . The map is unnecessary since in reduce, one can just return the input number like so

func sumAny(params: Any...) -> String {
    return String(params.reduce(0) {
        if let string = $1 as? String {
            if string.isEmpty {
                return $0 - 10
            } else if let int = Int(string) where int > 0 {
                return $0 + int
            }
        } else if let int = $1 as? Int {
            return int == 0 ? $0 - 10 : $0 + int
        }
        return $0
    })
}

Not a huge fan of the solution for #4.
I don’t think the solution is “Swifty” at all. It’s just general programming challenge. No reason to be on this thread…
I think introducing stride would make it more Swifty like so:

func countFrom(from: Int, to: Int) {
    assert(from < to)
    print(from.stride(through: to, by: 1).reduce("") { return $0+"\($1)" })
}

Plus, since the original post is old, print no longer prints in-line, but adds a line break at the end. So for the current 2.2 syntax, the following would be the correct answer:

func altCountFrom(from: Int, to: Int) {
    guard from <= to else { return }
    print(from, terminator: from < to ? "" : "\n")
    altCountFrom(from+1, to: to)
}

Challenge 2: Can it also be implemented in this way? using variadic parameter feature in swift

    func concatenateString(_ colors: String...) -> String{
        
     return  colors.count>0 ? String(colors.flatMap { String.CharacterView($0)}) : "None"
    }

    var result = concatenateString()
    result = concatenateString("Red","Blue")

The answer for challenge 2, while definitely “swifty”, could be much simpler in order to complete the challenge as stated:

func params(s1: String = “none”, s2: String = “”) -> String {
return(s1+s2)
}

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!