I was having a go at the following problem from the AIO (Australian Informatics Olympiad) Training Problems Site (Question in Italics and specifics in bold, my attempt below):
The Problem
Encyclopaedia
Input File: encyin.txt
Output File: encyout.txt
Time Limit: 1 second
Once again it is quiz night at the My Very First Encyclopedia Appreciation Society. For week after week, you have turned up to these quizzes, deftly answering question after question about zoo animals and days of the week, only for someone else to answer all the obscure bonus-round questions at the end, snatching first place and leaving you with nothing but a 'Nice Try!' sticker.
Dejected and disheartened, you are sitting at home musing upon your past failures, when a thought occurs to you. Could it be...? Riffling through months of angrily scribbled notes, you confirm your sneaking suspicion - the bonus questions follow a super-simple pattern! Your heart skips a beat. Every question in every bonus round of every quiz you've sat through has been phrased in the form: "How many words are there on page x of the My Very First Encyclopedia?" Normally you would consider that a little unlikely, even contrived, but not today - today you have a Mars bar to win.
With infinite care you compile a list of page numbers and their corresponding word counts. Others in your place might try to memorise the list, but no, your plan is far more hi-tech: first, you will write a program that can answer these questions for you; then, you will sneak your trusty laptop into the quiz, and proceed to blitz the competition.
All that's left is for you to actually write the program. The task seems simple: it has to take your list of numbers and tonight's bonus questions, and - quietly - print out the correct answers for you.
Input
The first line of the input will be of the form n q, where n is the number of pages in the encyclopedia, and q is the number of questions to be answered. (1 <= n, q <= 10,000)
Following this will be n lines, each describing a single page. The ith of these lines will contain the single integer pi, the number of words on page i. (0 <= pi <= 2,000,000,000)
Following this will be q lines, each describing a single question. Each of these lines will contain a single integer x, representing the question, "How many words are there on page x?" (1 <= x <= n)
Output
For each question, your program should write a single line of output. This line should contain a single integer, the number of words on the requested page.
My Attempt
I was able to write up the start of the code easily enough:
file = open("encyin.txt", "r")
pages, questions = file.readline().split()
pages = int(pages)
questions = int(questions)
for i in range (0, int(pages)):words = int(file.readline())
But I'm not sure what to do next. How do I assign each number given to a line? For example, the ith line has an integer describing the number of words on page i, so how would I assign this value to page i?
Thanks - I am writing in python.