0

The task is to load a file which contains some questions with different number of answers and the 1st listed answer is always the correct one

e.g.:

Who is Jane?
Girl
Boy
Both
Why are we here?
Because
For fun

So I was thinking I'll read lines into an array but in the end I need to be able to shuffle the answers for each question(and somehow keep the track of the correct one) and then shuffle the questions also.

So if i read the lines into the array it would look like this:

a = ['Who is Jane?', 'Girl', 'Boy', 'Both', 'Why are we here?', 'Because', 'For fun']

and now I think for what I have to do it would be best to split the arrays into smaller ones that contains always question and all its answers. So I would have something like

test = [['Who is Jane?', 'Girl', 'Boy', 'Both'], ['Why are we here?', 'Because', 'For fun']]

Anyone knows how could I do this? I know that to access the questions mark you can use a[0][-1] and that will give you the question mark from 'Who is Jane'.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    Is this homework? What have you tried so far? We will help you with specific problems, but we won't do your work for you. The first step is to read the text from the file, which is explained [in this question](http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array). In general, the Python documentation and tutorial are really good and it explains [how to iterate over lists](http://docs.python.org/tutorial/controlflow.html). – Felix Kling Mar 04 '12 at 10:40
  • It's a part of a homework.. but I went through the list documentation.. maybe I don't understand it correctly.. I know how to do shuffle and everything else.. I just can't figure out how to divide the array list into list with lists containing always one question and its answers –  Mar 04 '12 at 10:49
  • btw I have python only for month now.. and the class is twice a week.. and I never programmed before.. So I understand what you are saying Felix, but I think it's more useful to see the code and try to understand it then to come up with it by myself just based on what u wrote, because there is not much I can do in python without advice yet –  Mar 04 '12 at 11:49
  • You should definitely go through the tutorial then: http://docs.python.org/tutorial/index.html It's good :) – Felix Kling Mar 04 '12 at 11:53

2 Answers2

2

This my implementation, maybe yours is better.

>>> a = ['Who is Jane?', 'Girl', 'Boy', 'Both', 'Why are we here?', 'Because', 'For fun']
>>>
>>> def fun(x):
...   y = []
...   for i in x:
...     if i.endswith('?') and y:
...       yield y
...       y = []
...     y.append(i)
...   else:
...     yield y
...
>>> list(fun(a))
[['Who is Jane?', 'Girl', 'Boy', 'Both'], ['Why are we here?', 'Because', 'For fun']]
kev
  • 155,172
  • 47
  • 273
  • 272
  • Kev, the word is "implementation". But you shouldn't be doing people's homework for them. Guide them to solve the problem and learn for themselves. – alexis Mar 04 '12 at 11:20
  • this is gr8 now I have to realize how exactly the function works and then I should be able to implement it :) thx a lot man.. –  Mar 04 '12 at 11:46
0

Here is an outline of one possible algorithm, assuming lines contains the lines form the text file and they have been appropriately preprocessed:

result = []
group = [lines[0]]

for line in <lines from the second element to the last>:
    if <line ends with a question mark>:
        # append group to result
        # reset group to an empty list

    # append line to group

The idea is simply to iterate over the list of lines, create a new list whenever a question is encountered and appends the previous list to the overall result.


Depending on the course you are taking, it's more important to improve your problem solving skills (like finding an algorithm like above) than to learn Python (or any language) perfectly.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • yeah you are right, but like i said im only working with python for a while. Before I posted the question I know the import part is the next Question and I can find it by the question mark at the end.. and that I should probably use some for cycle so it gets done for every question. I just didn't know how.. The assignment I did before this one was to create all possible combinations of length N where every position can be either 1 or 0.. so if the user typed for example 3 the program showed list of 8 combinations like 000,001 etc. –  Mar 04 '12 at 11:54