# Millionaire quiz in mathematica

so here's the deal. I'm trying to write a millionare quiz in Mathematica. Now, this has been already done in other coding languages, specifically I've seen it done in C here:

http://tausiq.wordpress.com/2009/10/02/millionaire/

So, the setup is that I just input answers for the questions, then input the correct answers, and the function/program gives me the amount the player has earned. Now, the problem says to include the 1000 and \$32000 save points, but it isn't all that necessary.

Now, I want to translate this into Mathematica, but I can't seem to find how this would work. I was thinking of maybe using two lists, one with the answers of the user and the second with the correct answers and to somehow compare them. Can this be done with just the if, switch and for commands?

Can anyone help me with this?

EDIT:

Here's what I've done so far

A = {a, b, c, d, a, b, c, d, a, b, c}; B = {a, b, c, d, a, b, c, a, a, b, a};

This is how I read the answers and everything. Now I compute how many are correct:

For[i = 1; j = 0, A[[i]] == B[[i]], i++, 1; j++]

Then, based on the number I get I print out the amount they have won:

t2[k_] := Switch[k, 1, Print[100 KM], 2, Print[200 KM], 3, Print[300 KM], 4, Print[500 KM], 5, Print[1000 KM], 6, Print[2000 KM], 7, Print[4000 KM], 8, Print[8000 KM], 9, Print[16000 KM], 10, Print[32000 KM], 11, Print[64000 KM], 12, Print[125000 KM], 13, Print[250000 KM], 14, Print[500000 KM], 15, Print[1000000 KM]]

t2[j]

Or I use the code given by Pickett:

f1[n_] := Which[n <= 3, Print[100 n, " KM"], n <= 11, Print[500 2^(n - 4), " KM"], 12 <= n, Print[125000 2^(n - 12), " KM"]]

Now I only need to implement somehow the check for the safe points. I can use the second function given by Pickett:

f2[n_] := Which[n >= 10, 32000, n >= 5, 1000, True, 0]

but don't really know how to implement them both to have a check and then to be given the correct amount. I assume that I should somehow use the third function, but honestly I don't know how.

• Mathematica does provide the basic commands to translate most procedural programs with relatively minor changes. Usually there is a "better" way, more "Mathematica" way. If you're trying to learn Mathematica, you will want to figure out how to rewrite the C the program so that as not to use For, perhaps to take advantage of the built-in GUI functions, etc. I realize you want to do this yourself, but a general appeal for help seems to broad for this forum, imo. – Michael E2 Dec 13 '14 at 14:14
• Well, we are using for, if and switch in a class I'm having so it would be great if I could just translate this to Mathematica. – gentrylo Dec 13 '14 at 14:44
• It sounds like you are requesting code to fulfill the objectives of a class. To avoid the question being put on hold, I might suggest that you provide the code you have generated thus far, so community members can help you with understanding conditional and looping expressions in Mathematica. – bobthechemist Dec 13 '14 at 17:36

Mathematica code in general does not look like C code. I will provide a solution that may not help you short term if your goal is to learn about For, If and Switch statements. However if you go through each step and look up all the functions in the documentation then you will learn several things about Mathematica which are useful to know.

prizeFunction1[n_] := Which[
n <= 3, 100 n,
n <= 11, 500 2^(n - 4),
12 <= n, 125000 2^(n - 12)
]
prizeFunction2[n_] := Which[
n >= 10, 32000,
n >= 5, 1000,
True, 0
]

winnings[{correct_, answers_}] := If[Total[correct - answers] == 0,
]


To get the solution corresponding to a list of games:

games = ToCharacterCode /@ ImportString["ABCDABCDABCDABC
ABCDABCA
ABABABCDCDCDBCB
ABABABCDCDA
ABCABCDABCABCDA
ABCABCDABCA", "Lines"];
games = Partition[games, 2];

Composition[winnings, prune] /@ games
(* Out: {1000, 32000, 64000} *)


The logic of this solution is this: prizeFunction1 determines how much a player with n correct answers and no wrong answer wins. prizeFunction2 determines how much a player who got answer n wrong wins. winnings selects either prize function based on if the answerer got any answer wrong. Take a look at games, and you will notice that it is a list of numeric sublists. If the sublists are the same, i.e. the answers are the same as the correct answers, then Total[correct - answer] is zero. Because there are more correct answers than answers only some of the elements have to be taken from the list of correct answers, prune makes sure correct and answer are the same length. Composition is just like the mathematical composition of two functions, Composition[f1,f2][x] means f1[f2[x]], in this case it means prune first and then pass the new lists to winnings.

• I've edited my original post on what I've done so far. This works nicely, but is way too advanced for me to use in this since we haven't worked with ImportString and ToCharacterCode. – gentrylo Dec 13 '14 at 18:28
• @gentrylo it's not necessary to have been working with those functions in class. Reading their respective doc pages should suffice. – Sjoerd C. de Vries Dec 13 '14 at 23:19
• OK, but then can someone explain what /@ means? I get that in the last composition it is equal to the function Map, but I can't figure out what it means for the ImportString command and everything. Would really help. – gentrylo Dec 14 '14 at 11:08
• @gentrylo Sure, I'm happy to answer questions. f /@ expr is always equivalent to Map[f,expr]. In this case it evaluates ToCharacterCode on each sublist in the expression resulting from ImportString (take a look at it by evaluating ImportString[...] separately.) – C. E. Dec 14 '14 at 11:34
• Oh, OK, got it working now. Seems like I was making a mistake before and that's why it didn't work. Two more questions tough. Is it possible to write this so that it works with lists, like I'm trying to do? And if not, could you explain a little bit what the prune and winnings function does? And how it all works out with the composition of them in the last part? Thanks! – gentrylo Dec 14 '14 at 11:49