Randomise Multiple Choice options in swift - swift

I hope you are all well!
I am new in Coding and Swift.
I am making a quiz app and there are 2 things I would need help with.
The first one is : I want to randomise the options of the multiple choice answers for each question every time the game is played.
For example, if i define an array to contain the different answer options (the different colours), i want the question to retrieve 3 random answers together with the correct one like so:
let answers = [
"Red",
"Blue",
"Green",
"Yellow",
"Orange",
"Black",
"Grey",
"White"]
Question(q: "What colour is the ocean ?", a: ["Blue", "randomAnswer1", "randomAnswer2","randomAnswer3"], correctAnswer: "Blue"),
Question(q: "What colour are roses ?", a: ["randomAnswer1", "Red", "randomAnswer2","randomAnswer3"], correctAnswer: "Red"),
The 2nd part I would need help with is:
If my quiz has 100 questions, how could I make the series of the questions to be RANDOM but UNIQUE every time the game is played?
Meaning that until the Quiz ends, each question would need to be displayed only once.
Thank you very much for your help!

1: There is a method to get a random element from an array
let random = answers.randomElement()
2: You can iterate and delete the used element, so next time you get a random element the array does not consist it
answers = answers.filter { $0 != random }

Related

How to get first elements from a pyspark array?

I have an array and I just keep a specific column from it like this in PySpark:
c = [ "green", "blue", "red", "yellow",... ]
And I want to keep the n first elements of this column
How can I do this please ? I know is a simple question but I don't find the solution ...
Thanks

Query by item in array of document, and sort by item's position in the array

I have a collection dinosaurs with documents of this structure:
doc#1:
name: "Tyrannosaurus rex",
dominantColors: [
"beige",
"blue",
"green"
]
doc#2:
name: "Velociraptor",
dominantColors: [
"green",
"orange",
"white"
]
I want to query the collection by color name (for example: green) to get documents sorted by color's position in dominantColors array. First get the documents in which green occurs higher in the array, then those in which it is lower. So, in the provided case I would get doc#2 first, then doc#1.
Each dominantColors array contains 3 elements, with elements sorted from most dominant to least.
I am looking through documentation, but am not able to find a solution. Maybe I need a different data structure altogether?
Cloud Firestore doesn't support querying arrays by ranked index. The only way you can query an array is using an array-contains type query.
What you could do instead is organize your colors using maps where the color is the key and their rank is the value:
name: "Tyrannosaurus rex",
dominantColors: {
"beige": 1,
"blue": 2,
"green": 3
}
Then you can order the query by the value of the map's property. So, in JavaScript, it would be something like this:
firebase
.collection('dinosaurs')
.where('dominantColors.green', '>', 0)
.orderBy('dominantColors.green')

which style is most preferred in Mongodb?

In mongodb, which style is better? 1) or 2)? Can I retrieve only line name from 1) despite of getting whole record on db.record.find("line":"east")?
1.
{
"line": "east",
"station":[
{ "name": "ABC", "_id": "1", },
{ "name": "DEF", "_id": "2" }
]
}
2.
{ "line": "east", "l_id":"1"},
{"station":"ABC", "_id":"1", "l_id":"1"},
{"station":"ABC", "_id":"2", "l_id":"1"}
Note: line and station has one to many relationship.
If you are most commonly getting stations by line and often need all stations of a line alt 1 is the best. If you are commonly retrieving single stations or a subset of stations 2 is the best. Alt 2 can lead to more queries since mongo doesn't really have any relations, alt 1 can lead to larger reads and make it more difficult to keep the working set in RAM because of larger objects. Alt 1 also has a minor drawback if you change values in a station on multiple lines - then you have to update its instance in each of the line documents containg it.
To get a partial object, i.e. not all stations in alt 1, you can do a projection to filter out what you don't want. It still means reading the whole object into memory first so you wouldn't gain a lot in performance from doing that.

Assign a color to individual row numbers

I am trying to assign an individual color to each row in a group. There are approximately 50 rows I need to assign a color and they all must be unique. What would be the easiest way to do this?
I do not want to do the alternating row colors:
"=IIf(RowNumber("DataSet1") Mod 2 = 1, "White","Blue")",
but if there was a way to modify this expression to do 50 colors and then repeat I'd be okay with that.
you can use choose . otherwise use custom code function
=Choose(ROWNUMBER(nothing) mod 50, "Brown", "Blue", "GoldenRod", "Olive", "MediumTurquoise","Red", "Green", "DeepSkyBlue", "Yellow", "Chocolate", "Purple", "DarkOrange" ,.....)

def negatives in JES

I am trying to show my picture as a negative, and I coded it, but it wont show the picture as a negative, did I do something wrong?
def negative(picButterfly2):
for px in getPixels(picButterfly1):
red=getRed(px)
green=getGreen(px)
blue=getBlue(px)
negColor=makeColor(255-red, 255-green, 255-blue)
setColor(px,negColor)
ALSO HOW DO I DRAW HORIZONTAL LINES? Thanks!
Try with correct variables names: you have picButterfly2 NOT EQUAL TO picButterfly1:
This works:
def negative(picButterfly1):
for px in getPixels(picButterfly1):
red=getRed(px)
green=getGreen(px)
blue=getBlue(px)
negColor=makeColor(255-red, 255-green, 255-blue)
setColor(px,negColor)
file = pickAFile()
picture = makePicture(file)
negative(picture)
show(picture)
Also look at:
This (for negating images).
This (for drawing lines) - or any of those.
Your variables "red", "blue", and "green" already have a function in it, change it to a single character or just a capital letter like "Red". I know this was posted in 2014 but I'll leave a comment for the future.