Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168716
Image: ubuntu2004
%answer plot 5*x + 5
%answer What is the plot of the expression 5 * x ^ 2 - x?
%answer What is the plot of the expression 5*x?
%answer How many primes are there under 1 million?
There are 78498 primes under 1 million.
%answer How many primes are there under 5 million?
There are 348513 primes under 5 million.
%answer How many primes are there under 2000?
There are 303 primes under 2000.
def find_all(string, sub): listindex = [] i = string.find(sub, 0) while i >= 0: listindex.append(i) i = string.find(sub, i + 1) return listindex def variables_in_string(s): """ Returns list of variable names in a given string. EXAMPLES: sage: variables_in_string('There are $n$ boxes.') ['n'] sage: variables_in_string('What is the $n$th prime number?') ['n'] """ #variables = dict([(remove_non_letters(word), n) for n, word in enumerate(s.split()) if '$' in word]) variables = [word for word in s.split() if '$' in word] def remove_non_identifiers(s): """ Removes '$'s and everything outside them. """ start, finish = find_all(s, '$') return s[start + 1: finish] variables = [remove_non_identifiers(variable) for variable in variables] return variables def does_question_match_template(question, template): # return False return True class Question(object): pass class ComputationalQuestion(Question): pass class NumberOfPrimesQuestion(ComputationalQuestion): """ EXAMPLES: sage: NumberOfPrimesQuestion('How many primes are there under 1 million?').answer() 'There are 78498 primes under 1 million' """ def __init__(self, question, question_template): self.question = question self.question_template = question_template #self.variables = dict([(name, None) for name in variables_in_string(self.question_template)]) self.start, self.finish = find_all(self.question_template, '$') self.variables = {} self.variables['upper_bound'] = self.question[self.start: -(len(self.question_template) - self.finish - 1)] def answer(self): units = dict(million=10 ^ 6, billion=10 ^ 9) if self.variables['upper_bound'].isdigit(): upper_bound = ZZ(self.variables['upper_bound']) else: n, unit = self.variables['upper_bound'].split() n = ZZ(n) unit = units[unit] upper_bound = n * unit return 'There are %s primes under %s.' % (len(prime_range(upper_bound)), self.variables['upper_bound']) class PlotQuestion(ComputationalQuestion): """ EXAMPLES: sage: PlotQuestion('What is the plot of the expression x^2 + 5?').answer() """ def __init__(self, question, question_template): self.question = question self.question_template = question_template #self.variables = dict([(name, None) for name in variables_in_string(self.question_template)]) self.start, self.finish = find_all(self.question_template, '$') self.variables = {} self.variables['expression'] = self.question[self.start:] if self.question_template[-1] == '$' else self.question[self.start: -(len(self.question_template) - self.finish - 1)] def answer(self): expression = sage_eval(('var("x")', self.variables['expression'])) plot(expression).save('plot.png') return html('') question_templates = {} question_templates['What is the plot of the expression $expression$?'] = PlotQuestion question_templates['plot $expression$'] = PlotQuestion question_templates['How many primes are there under $upper_bound$?'] = NumberOfPrimesQuestion class Answer(object): def eval(self, s, globals=None, locals=None): if globals is None: globals = {} if locals is None: locals = {} s = str(s) question = s words_in_question = set(question.split()) templates = dict([(template, len(words_in_question.intersection(set(template.split())))) for template in question_templates]) if templates.values().count(max(templates.values())) == 1: t = dict(zip(templates.values(), templates.keys()))[max(templates.values())] #print question_templates[t](question, t).variables #print len(question_templates[t](question, t).question) #print question_templates[t](question, t).finish return question_templates[t](question, t).answer() else: templates = [q for q, l in templates.items() if l == max(templates.values())] size = len(question.split()) size_differences = dict([(template, abs(size - len(template.split()))) for template in templates]) t = dict(zip(size_differences.values(), size_differences.keys()))[min(size_differences.values())] #print question_templates[t](question, t).variables #print len(question_templates[t](question, t).question) #print question_templates[t](question, t).finish return question_templates[t](question, t).answer() #return NumberOfPrimesQuestion(question).answer() answer = Answer()