-
-
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) |
@TheMuellenator, thank you for the help 🙌 🤩 🙌
i was 1...26 OFF
initially using
• let password = alphabet[Int.random(in: 0...25)]
let INSTEAD of a var
• alphabet[Int.random(in: 1...26)] INSTEAD of 0...25
tried to go a little further (mistakenly thinking i was correctly referring back to section: 4 | lesson: 44 | timeline: 13:03) and use
• var password = alphabet.randomElement() + alphabet.randomElement() + alphabet.randomElement() + alphabet.randomElement() + alphabet.randomElement() + alphabet.randomElement()
but the playground wouldn't run it, seemed to just 'time out' 🤔🤔🤔
making the changes you have posted i feel happy i got it 'mostly' 😳😐 correct but want to see if the randomElement() part might work to (try and) 'refactor' to make the code even simpler/ even more 'efficient'.
that said read a comment above about not 'overthinking' 🤔 etc.
so am happy with this and will continue (slowly) onwards LOL
thanks again
b.en 🥸 in e.dinburgh 🏰
comprehension level: (absolute) beginner
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
let 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"]
let 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("the generated password is '", password,"'")
i waas confused about array.shuffle()
dsfs
sfs
let num = [1, 2, 3, 4, 5, 6]
var pass = [String]()
for _ in num {
let g = alphabet[Int.random(in: 0...25)]
pass.append(g)
}
let password = pass.joined(separator: "")
print(password)
//I saw people just solve it in 6 line and I manage to do it by hard way 😅
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 = ""
for _ in 1...6 {
password += alphabet[Int.random(in: 1...26)]
}
print(password)
}
exercise()
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
It's great to see all the different solutions for the problem. Here is mine:
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 randomAlphabet = alphabet;
randomAlphabet.shuffle();
let password = randomAlphabet[0..<6].joined();
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 String
s, 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);
}
Hi, this is my solution for this challange.
func exercise() {
}