Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active August 16, 2024 09:13
Show Gist options
  • Save TheMuellenator/9fdb0d41d6343218ca1bcb6fb724949e to your computer and use it in GitHub Desktop.
Save TheMuellenator/9fdb0d41d6343218ca1bcb6fb724949e to your computer and use it in GitHub Desktop.
iOS repl.it - Randomisation Challenge Solution
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)
@Kangajan18
Copy link

Hi, this is my solution for this challange.

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 0...5{
    password = password + (alphabet.randomElement()!)
}    
print(password)

}

Copy link

ghost commented Mar 13, 2023

@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

@LM-1005-Q
Copy link

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)

}

@dixon-a
Copy link

dixon-a commented Apr 25, 2023

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()

@DrChhun
Copy link

DrChhun commented May 4, 2023

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 😅

@MushroomMSTR
Copy link

MushroomMSTR commented May 5, 2023

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()

@Jai-create22
Copy link

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

@raphaelalmeidamartins
Copy link

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);
}

@Invader1999
Copy link

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)

@eltonbrayner
Copy link

eltonbrayner commented Jun 9, 2023

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)
}

Copy link

ghost commented Jun 24, 2023

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

@Ven1884
Copy link

Ven1884 commented Jun 30, 2023

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)

@0ys0219
Copy link

0ys0219 commented Aug 23, 2023

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)

@Danfelogar
Copy link

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)

}

@GangChenGoCode
Copy link

let password = (0...5).map { _ in alphabet[Int.random(in:0...25)]}.joined()

@leandroqros
Copy link

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)

`

@marekrakus03
Copy link

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!

@AstraXR
Copy link

AstraXR commented Jan 14, 2024

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()

@kesselly4099
Copy link

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

@FadhilMustari
Copy link

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)

}

@Nurreles
Copy link

Nurreles commented Feb 7, 2024

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)

@uncommon-design
Copy link

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)

@Cyrille-Dakhlia
Copy link

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.

@YashDjsonDookun
Copy link

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);

@Sirio2022
Copy link

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)

@h40hm4ru
Copy link

h40hm4ru commented May 7, 2024

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)

@ParsaMotiallah
Copy link

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)

@toporusan
Copy link

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)

@vovahvfr
Copy link

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);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment