1 | #!/usr/bin/python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Pyromaths |
---|
5 | # Un programme en Python qui permet de créer des fiches d'exercices types de |
---|
6 | # mathématiques niveau collège ainsi que leur corrigé en LaTeX. |
---|
7 | # Copyright (C) 2006 -- Jérôme Ortais (jerome.ortais@pyromaths.org) |
---|
8 | # |
---|
9 | # This program is free software; you can redistribute it and/or modify |
---|
10 | # it under the terms of the GNU General Public License as published by |
---|
11 | # the Free Software Foundation; either version 2 of the License, or |
---|
12 | # (at your option) any later version. |
---|
13 | # |
---|
14 | # This program is distributed in the hope that it will be useful, |
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | # GNU General Public License for more details. |
---|
18 | # |
---|
19 | # You should have received a copy of the GNU General Public License |
---|
20 | # along with this program; if not, write to the Free Software |
---|
21 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | # |
---|
23 | ''' |
---|
24 | Created on 21 janv. 2015 |
---|
25 | |
---|
26 | @author: jerome |
---|
27 | ''' |
---|
28 | from pyromaths import ex |
---|
29 | from random import randrange |
---|
30 | def listeToclasses(liste): |
---|
31 | result = [r'$[%s' % (liste[0])] |
---|
32 | for l in liste[1:]: |
---|
33 | result.append(r'$[%s' % (l + eval(result[-1][2:]))) |
---|
34 | result[-2] = result[-2] + r' ~;~ %s[$' % (l + eval(result[-2][2:])) |
---|
35 | result.pop(-1) |
---|
36 | return result |
---|
37 | class Histogramme(ex.TexExercise): |
---|
38 | ''' |
---|
39 | classdocs |
---|
40 | ''' |
---|
41 | # description = u'Construire un histogramme' |
---|
42 | # level = u"2.Seconde" |
---|
43 | |
---|
44 | |
---|
45 | def __init__(self): |
---|
46 | ''' |
---|
47 | Constructor |
---|
48 | ''' |
---|
49 | echelle1 = 5 * 2 ** randrange(1, 6) |
---|
50 | debut = randrange(0, 4) * 10 ** randrange(0, 4) |
---|
51 | classes = [debut] |
---|
52 | classes.extend([echelle1 * randrange(1, 5) for dummy in range(randrange(4, 6))]) |
---|
53 | echelle2 = 5 * 2 ** randrange(1, 6) |
---|
54 | effectifs = [echelle2 * randrange(1, 5) for dummy in range(len(classes) - 1)] |
---|
55 | self.classes = classes |
---|
56 | self.effectifs = effectifs |
---|
57 | self.echelle1 = echelle1 |
---|
58 | self.echelle2 = echelle2 |
---|
59 | |
---|
60 | def tex_statement(self): |
---|
61 | exo = [r'\exercice'] |
---|
62 | exo.append(_(u'Tracer l\'histogramme de la série ci-dessous :\\par')) |
---|
63 | exo.append(r'\begin{tabularx}{.8\linewidth}[t]{|l|*{%s}{>{\centering\arraybackslash}X|}}' % len(self.classes)) |
---|
64 | exo.append(_(r'\hline Classe & %s \\' % " & ").join(listeToclasses(self.classes))) |
---|
65 | exo.append(_(r'\hline Effectif & %s \\ \hline') % " & ".join([str(i) for i in self.effectifs])) |
---|
66 | exo.append(r'\end{tabularx}') |
---|
67 | return exo |
---|
68 | |
---|
69 | def tex_answer(self): |
---|
70 | exo = [r'\exercice*'] |
---|
71 | exo.append(r'\begin{center}') |
---|
72 | exo.append(r'\begin{asy}') |
---|
73 | exo.append(r'import stats;') |
---|
74 | exo.append(r'import graph;') |
---|
75 | exo.append(r'size(15cm,7cm,false);') |
---|
76 | # exo.append(r'unitsize(1cm);') |
---|
77 | exo.append(r'real[] tabxi={%s};' % ",".join([str(sum(self.classes[:i + 1])) for i in range(len(self.classes))])) |
---|
78 | exo.append(r'real[] tabni={%s};' % ",".join([str(i) for i in self.effectifs])) |
---|
79 | exo.append(r'real uniteaire=%s;' % self.echelle2) |
---|
80 | exo.append(r'string libelleunite="Effectif de %s.";' % (self.echelle2)) |
---|
81 | exo.append(r'pen p1=lightred,p2=1bp+black;') |
---|
82 | exo.append(u'string libellecaractere="Valeurs du caractère";') |
---|
83 | exo.append(r'real minaxe=%s;' % self.classes[0]) |
---|
84 | exo.append(r'real maxaxe=%s;' % sum(self.classes)) |
---|
85 | exo.append(r'real uniteaxe=%s;' % self.echelle1) |
---|
86 | exo.append(r'real largeurunite=abs(tabxi[1]-tabxi[0]);') |
---|
87 | exo.append(r'int iclasse=0;') |
---|
88 | exo.append(r'real[] tabhi;') |
---|
89 | exo.append(r'for(int i=0; i < tabni.length; ++i){') |
---|
90 | exo.append(r'tabhi[i]=tabni[i]/(tabxi[i+1]-tabxi[i]);') |
---|
91 | exo.append(r'if (largeurunite>abs(tabxi[i+1]-tabxi[i])) {') |
---|
92 | exo.append(r'largeurunite=abs(tabxi[i+1]-tabxi[i]);') |
---|
93 | exo.append(r'iclasse=i;') |
---|
94 | exo.append(r'}') |
---|
95 | exo.append(r'}') |
---|
96 | exo.append(r'real hauteurmaxi=max(tabhi);') |
---|
97 | exo.append(r'real hauteurunite=(uniteaire/tabni[iclasse])*tabhi[iclasse];') |
---|
98 | exo.append(r'histogram(tabxi,tabhi,low=0,bars=true,p1,p2);') |
---|
99 | exo.append(r'filldraw(box((%s,hauteurmaxi+.5hauteurunite),(%s+largeurunite,hauteurmaxi+1.5hauteurunite)),p1,p2);' % (self.classes[0], self.classes[0])) |
---|
100 | exo.append(r'xaxis(libellecaractere, Bottom, minaxe,maxaxe,') |
---|
101 | exo.append(r'RightTicks(Label(currentpen+fontsize(6)), new real[]{%s}),above=false);' % (",".join([str(self.classes[0] + k * self.echelle1) for k in range(sum(self.classes) // self.echelle1 + 1)]))) |
---|
102 | exo.append(r'for(int k=0; k<(2*hauteurmaxi/hauteurunite+4); ++k){') |
---|
103 | exo.append(r'draw((minaxe,.5k*hauteurunite)--(maxaxe,.5k*hauteurunite),.5bp+gray);') |
---|
104 | exo.append(r'}') |
---|
105 | exo.append(r'for(int k=0; k<=((maxaxe-minaxe)/uniteaxe); ++k){') |
---|
106 | exo.append(r'draw((minaxe+k*uniteaxe,0)--(minaxe+k*uniteaxe,hauteurmaxi+1.5hauteurunite),.5bp+gray);') |
---|
107 | exo.append(r'}') |
---|
108 | exo.append(r'label(libelleunite,(%s+largeurunite,hauteurmaxi+hauteurunite),align=E,Fill(white));' % (self.classes[0])) |
---|
109 | exo.append(r'histogram(tabxi,tabhi,low=0,bars=true,p1+opacity(0),p2);') |
---|
110 | exo.append(r'\end{asy}') |
---|
111 | exo.append(r'\end{center}') |
---|
112 | return exo |
---|