-
-
Save TheMuellenator/9fdb0d41d6343218ca1bcb6fb724949e to your computer and use it in GitHub Desktop.
| let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] | |
| //The number of letters in alphabet equals 26 | |
| var password = alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] | |
| print(password) |
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var password = "";
for i in 0...5 {
password += alphabet.randomElement() ?? "";
}
print(password);
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
//The number of letters in alphabet equals 26
var a = alphabet.randomElement()
var b = alphabet.randomElement()
var c = alphabet.randomElement()
var d = alphabet.randomElement()
var e = alphabet.randomElement()
var f = alphabet.randomElement()
var result = ""
result.append(a!)
result.append(b!)
result.append(c!)
result.append(d!)
result.append(e!)
result.append(f!)
let password = result
print(password)
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
var password = ""
for _ in 1 ... 6 {
password += alphabet.randomElement()!
}
print(password)let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
let randomElements = Array(alphabet.shuffled().prefix(6))
let password = randomElements.joined()
print (password)
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
var password = ""
while password.count != 6 {
password = password + alphabet[Int.random(in: 0 ... alphabet.count - 1)]
}
print(password)
func exercise() {
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
let password = alphabet.shuffled().prefix(6).joined()
print(password);
}
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
let a = alphabet.randomElement() ?? "";
let b = alphabet.randomElement() ?? "";
let c = alphabet.randomElement() ?? "";
let d = alphabet.randomElement() ?? "";
let e = alphabet.randomElement() ?? "";
let f = alphabet.randomElement() ?? "";
let password = a + b + c + d + e + f;
print(password);
Hi everyone! A lot of people here using more advanced techniques to solve the challenge. It's interesting to see all the ways it can be done. I'm glad I was also exactly like the official solution to show I'm on track!
let alphabet = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
]
// The number of letters in alphabet equals 26
// a password of six random characters
var password: String = " "
for i in 1...6 {
password += alphabet.randomElement() ?? ""
}
print(password)
func exercise() {
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
// Shuffle the alphabet and pick the first 6 letters
let shuffledAlphabet = alphabet.shuffled()
let password = shuffledAlphabet.prefix(6).joined()
print(password)
}
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
//The number of letters in alphabet equals 26 let password = alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! print(password)The '!' on my code refers to force unwrapping optionals. Basically what optionals are saying is, "There is a value and it is [this value] or there isn't a value at all." If there wasn't a value at all, it would be 'nil'. if tried to run with randomElement alone the code would crash as will keep running and never find a solution.
Thank you for explaining this one. I am actually trying to use the alphabet.randomElement() but I was getting compiler error -- took too long to compile. Did not know you would need the '!'.
func exercise() {
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
let password = (1...6).map { _ in alphabet.randomElement()! }.joined()
print(password)
}
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
//The number of letters in alphabet equals 26
let password = (0..<6).compactMap { _ in
alphabet.randomElement()}.joined()
print(password)
`func exercise() {
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
var password = ""
for _ in 1...6{
password += alphabet.randomElement()!
}
print(password)
}`
Types, say
StringorInt, have their variant type calledOptional(Optional Stringwritten in code asString?,Optional Intwritten in code asInt?).A
String?can either be aStringor a null value (callednilin Swift) that means "nothing".Why do we have that? Let's say you have a program where you can define nicknames for your users, you could give it the
String?type as not everybody necessarily have a nickname, so the value of anicknamevariable could be either aString, either nothing (nil) if no nickname would have been defined for that user.Now let's say you have the following function that greets your users using their nickname
It expects the nickname as a parameter being a
Stringand not aString?. To call your function, you could not do:since nickname is a
String?and the function expects aString, you would have a compilation error.To effectively pass a
Stringto your function, you would use the!symbol that says: "I know there's a value in there, trust me and act as if it was not 'Optional'." :Looking at the
randomElementmethod of theArrayclass in the documentation, we can see that whenever we call it, it returns a value of the typeElement?:Our array
alphabetbeing an array ofStringelements, callingrandomElementon it would return the typeString?.Since our password is of type
Stringand notString?, it should be composed ofStrings, so using therandomElementmethod, we should transform theString?values intoStringones by using the!symbol/operator.