-
-
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 alphabets = [
"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 0..<6{
var int = Int.random(in: 0..<alphabets.count)
password+=alphabets[int]
}
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"]
//The number of letters in alphabet equals 26
var password = ""
while(password.count < 6){
password += alphabet[Int.random(in: 0...25)]
}
print(password)
}bro these comments are a hoot. You got dudes on here trying to show off by doing it a far more experienced way like this isn't a class for absolute beginners lol
Right lmao
var 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"]
alphabet.shuffle()
let password = alphabet.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 password = { () -> String in
var str = String()
for _ in 0...5 {
str.append(alphabet.randomElement()!)
}
return str
}()
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 randomPassword = ""
//The number of letters in alphabet equals 26
for i in 0...5 {
randomPassword += alphabet[Int.random(in: i...alphabet.count)]
}
let password = randomPassword
print(password)
}
let password = (0...5).map { _ in alphabet[Int.random(in:0...25)]}.joined()
Oi comunidade Brasileira 🇧🇷
A ideia que eu tive aqui foi criar seis variáveis e tirar um numero aleatorio de cada um na criação da variável.
Logo somar todas elas e adicionar na variável "senha"
Então imprimir a senha.
`
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
//Variaveis e asignaçao de letra
var letraUm = alphabet[Int.random(in: 0...25)]
var letraDois = alphabet[Int.random(in: 0...25)]
var letraTres = alphabet[Int.random(in: 0...25)]
var letraQuatro = alphabet[Int.random(in: 0...25)]
var letraCinco = alphabet[Int.random(in: 0...25)]
var letraSeis = alphabet[Int.random(in: 0...25)]
//Soma e asignaçao de senha
var senha = letraUm + letraDois + letraTres + letraQuatro + letraCinco + letraSeis
//Imprimir senha
print(senha)
`
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 randomCharacter1 = alphabet.randomElement()!
var randomCharacter2 = alphabet.randomElement()!
var randomCharacter3 = alphabet.randomElement()!
var randomCharacter4 = alphabet.randomElement()!
var randomCharacter5 = alphabet.randomElement()!
var randomCharacter6 = alphabet.randomElement()!
let password = randomCharacter1 + randomCharacter2 + randomCharacter3 + randomCharacter4 + randomCharacter5 + randomCharacter6
print(password)
//Went about it this way. I know it's a bit complicated but it works! While troubleshooting this I came across something called an "optional" which is a value you have to unwrap for it to be there. After searching on Stack Overflow for a while I found out you do that with a ! after the .randomElement. Have a nice day!
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 0..<6 {
password += alphabet.randomElement()!
}
print(password)
}
//exercise()
let password = alphabet.randomElement()! + alphabet.randomElement()! + alphabet[2] + alphabet[3] + alphabet[4] + alphabet[5]
// you can chose to use the randomElement of you can call single character
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"]
//The number of letters in alphabet equals 26
var password: String = ""
for i in 1...6{
password += alphabet[Int.random(in: 0...25)]
}
print(password)
}
let alphabetArray = ["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 l in 0..<6 {
password += alphabetArray.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"]
var password = alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!
print(password)
I really know I am late, I've all the top solutions on this page and I really don't get what does "!" mean in alphabet.rendomElemnet()! I've been looking all over internet (in my capacity) but still haven't found a reasonable solution. I hope you guys can help me on this . Thanks
Types, say String or Int, have their variant type called Optional (Optional String written in code as String?, Optional Int written in code as Int?).
A String? can either be a String or a null value (called nil in 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 a nickname variable could be either a String, 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
func greet(personNickname: String) -> String {
let greeting = "Hello, " + personNickname + "."
return greeting
}It expects the nickname as a parameter being a String and not a String?. To call your function, you could not do:
print(greet(user.nickname))since nickname is a String? and the function expects a String, you would have a compilation error.
To effectively pass a String to 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'." :
print(greet(user.nickname!))Looking at the randomElement method of the Array class in the documentation, we can see that whenever we call it, it returns a value of the type Element?:
func randomElement() -> Self.Element?Our array alphabet being an array of String elements, calling randomElement on it would return the type String?.
Since our password is of type String and not String?, it should be composed of Strings, so using the randomElement method, we should transform the String? values into String ones by using the ! symbol/operator.
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)
}`
It's great to see all the different solutions for the problem. Here is mine: