Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active January 27, 2025 18:59
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)
@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);

}

@dangduong0127
Copy link

dangduong0127 commented Oct 27, 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"];

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

@untamedsensei
Copy link

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!

@KayzMan
Copy link

KayzMan commented Dec 3, 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"
]

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

@micko4435
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"]

// Shuffle the alphabet and pick the first 6 letters
let shuffledAlphabet = alphabet.shuffled()
let password = shuffledAlphabet.prefix(6).joined()

print(password)

}

@pbsi-00
Copy link

pbsi-00 commented Jan 27, 2025

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 '!'.

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