Md-IUT-Cours/algo_avancee/piles.html

104 lines
23 KiB
HTML
Raw Normal View History

2015-01-28 09:34:38 +01:00
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>piles.html</title>
<meta name="generator" content="Haroopad 0.13.0" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>div.oembedall-githubrepos{border:1px solid #DDD;border-radius:4px;list-style-type:none;margin:0 0 10px;padding:8px 10px 0;font:13.34px/1.4 helvetica,arial,freesans,clean,sans-serif;width:452px;background-color:#fff}div.oembedall-githubrepos .oembedall-body{background:-moz-linear-gradient(center top,#FAFAFA,#EFEFEF);background:-webkit-gradient(linear,left top,left bottom,from(#FAFAFA),to(#EFEFEF));border-bottom-left-radius:4px;border-bottom-right-radius:4px;border-top:1px solid #EEE;margin-left:-10px;margin-top:8px;padding:5px 10px;width:100%}div.oembedall-githubrepos h3{font-size:14px;margin:0;padding-left:18px;white-space:nowrap}div.oembedall-githubrepos p.oembedall-description{color:#444;font-size:12px;margin:0 0 3px}div.oembedall-githubrepos p.oembedall-updated-at{color:#888;font-size:11px;margin:0}div.oembedall-githubrepos ul.oembedall-repo-stats{border:none;float:right;font-size:11px;font-weight:700;padding-left:15px;position:relative;z-index:5;margin:0}div.oembedall-githubrepos ul.oembedall-repo-stats li{border:none;color:#666;display:inline-block;list-style-type:none;margin:0!important}div.oembedall-githubrepos ul.oembedall-repo-stats li a{background-color:transparent;border:none;color:#666!important;background-position:5px -2px;background-repeat:no-repeat;border-left:1px solid #DDD;display:inline-block;height:21px;line-height:21px;padding:0 5px 0 23px}div.oembedall-githubrepos ul.oembedall-repo-stats li:first-child a{border-left:medium none;margin-right:-3px}div.oembedall-githubrepos ul.oembedall-repo-stats li a:hover{background:5px -27px no-repeat #4183C4;color:#FFF!important;text-decoration:none}div.oembedall-githubrepos ul.oembedall-repo-stats li:first-child a:hover{border-bottom-left-radius:3px;border-top-left-radius:3px}ul.oembedall-repo-stats li:last-child a:hover{border-bottom-right-radius:3px;border-top-right-radius:3px}span.oembedall-closehide{background-color:#aaa;border-radius:2px;cursor:pointer;margin-right:3px}div.oembedall-container{margin-top:5px;text-align:left}.oembedall-ljuser{font-weight:700}.oembedall-ljuser img{vertical-align:bottom;border:0;padding-right:1px}.oembedall-stoqembed{border-bottom:1px dotted #999;float:left;overflow:hidden;width:730px;line-height:1;background:#FFF;color:#000;font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif;font-size:80%;text-align:left;margin:0;padding:0}.oembedall-stoqembed a{color:#07C;text-decoration:none;margin:0;padding:0}.oembedall-stoqembed a:hover{text-decoration:underline}.oembedall-stoqembed a:visited{color:#4A6B82}.oembedall-stoqembed h3{font-family:Trebuchet MS,Liberation Sans,DejaVu Sans,sans-serif;font-size:130%;font-weight:700;margin:0;padding:0}.oembedall-stoqembed .oembedall-reputation-score{color:#444;font-size:120%;font-weight:700;margin-right:2px}.oembedall-stoqembed .oembedall-user-info{height:35px;width:185px}.oembedall-stoqembed .oembedall-user-info .oembedall-user-gravatar32{float:left;height:32px;width:32px}.oembedall-stoqembed .oembedall-user-info .oembedall-user-details{float:left;margin-left:5px;overflow:hidden;white-space:nowrap;width:145px}.oembedall-stoqembed .oembedall-question-hyperlink{font-weight:700}.oembedall-stoqembed .oembedall-stats{background:#EEE;margin:0 0 0 7px;padding:4px 7px 6px;width:58px}.oembedall-stoqembed .oembedall-statscontainer{float:left;margin-right:8px;width:86px}.oembedall-stoqembed .oembedall-votes{color:#555;padding:0 0 7px;text-align:center}.oembedall-stoqembed .oembedall-vote-count-post{font-size:240%;color:#808185;display:block;font-weight:700}.oembedall-stoqembed .oembedall-views{color:#999;padding-top:4px;text-align:center}.oembedall-stoqembed .oembedall-status{margin-top:-3px;padding:4px 0;text-align:center;background:#75845C;color:#FFF}.oembedall-stoqembed .oembedall-status strong{color:#FFF;display:block;font-size:140%}.oembedall-stoqembed .oembedall-summary{float:left;width:635px}.oembedall-stoqembed .oembedall-excerpt{line-height:1.2;margin:0;padding:0 0 5px}.oembedall-stoqembed .oembedall-tags{float:left;line-height:18px}.oembedall-stoqembed .oembedall-tags a:hover{text-decor
</head>
<body class="markdown metro-vibes">
<h1 id="algo-avancée---listes"><a name="algo-avancée---listes" href="#algo-avancée---listes"></a>Algo avancée - Listes</h1><p class="toc" style="undefined"></p><ul>
<li><ul>
<li><span class="title">
<a href="#problématique" title="Problématique">Problématique</a>
</span>
<!--span class="number">
0
</span-->
</li>
<li><span class="title">
<a href="#les-piles" title="Les piles">Les piles</a>
</span>
<!--span class="number">
1
</span-->
<ul>
<li><span class="title">
<a href="#modélisation-liste" title="Modélisation liste">Modélisation liste</a>
</span>
<!--span class="number">
2
</span-->
</li>
<li><span class="title">
<a href="#modélisation-tableau" title="Modélisation tableau">Modélisation tableau</a>
</span>
<!--span class="number">
3
</span-->
</li>
</ul>
</li>
<li><span class="title">
<a href="#problèmes" title="Problèmes">Problèmes</a>
</span>
<!--span class="number">
4
</span-->
<ul>
<li><span class="title">
<a href="#bien-parenthésé-?" title="Bien parenthésé ?">Bien parenthésé ?</a>
</span>
<!--span class="number">
5
</span-->
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p></p><h2 id="problématique"><a name="problématique" href="#problématique"></a>Problématique</h2><p>Les tableaux et les listes sont des structures basiques et ne permettent pas la résolution de certains problèmes.</p><h2 id="les-piles"><a name="les-piles" href="#les-piles"></a>Les piles</h2><p>Les piles sont comme une pile dassiettes.<br>On dispose de fonctions permettant les opérations sur les piles :</p><pre class="python hljs">p = creer_pile()
empiler(p, e)
depiler(p)
elt = sommet(p)
vide = pile_vide(p)
pleine = pile_pleine(p)
</pre><p>On peut modéliser les piles de différentes manières</p><h3 id="modélisation-liste"><a name="modélisation-liste" href="#modélisation-liste"></a>Modélisation liste</h3><p>On peut voir les piles comme des listes :<br>La tête sappelle maintenant sommet, on empile en ajoutant un élément avant la tête, on dépile en enlevant le sommet et en le déplaçant.<br>La pile ici ne sera jamais pleine.</p><pre class="python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">creer_pile</span><span class="hljs-params">()</span>:</span>
p = Pile(sommet=<span class="hljs-keyword">None</span>)
<span class="hljs-keyword">return</span> p
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pile_vide</span><span class="hljs-params">(p)</span>:</span>
<span class="hljs-keyword">return</span> p.sommet == <span class="hljs-keyword">None</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">empile</span><span class="hljs-params">(p, e)</span>:</span>
ptr = new_maillon()
ptr.suivant = p.sommet
ptr.valeur = e
p.sommet = ptr
</pre><h3 id="modélisation-tableau"><a name="modélisation-tableau" href="#modélisation-tableau"></a>Modélisation tableau</h3><p>Renvoi au cours dalgo.</p><h2 id="problèmes"><a name="problèmes" href="#problèmes"></a>Problèmes</h2><h3 id="bien-parenthésé-?"><a name="bien-parenthésé-?" href="#bien-parenthésé-?"></a>Bien parenthésé ?</h3><p>On cherche à déterminer si une expression est bien parenthésée ou non. On va utiliser les piles pour résoudre ce problème :</p><pre class="python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">check_parentheses</span><span class="hljs-params">(expression)</span>:</span>
pile = creer_pile()
<span class="hljs-keyword">for</span> car <span class="hljs-keyword">in</span> expression:
<span class="hljs-keyword">if</span> car == <span class="hljs-string">'('</span> <span class="hljs-keyword">or</span> car == <span class="hljs-string">'['</span>:
empile(pile, car)
<span class="hljs-keyword">elif</span> car == <span class="hljs-string">')'</span>:
<span class="hljs-keyword">if</span> pile_vide(pile) <span class="hljs-keyword">or</span> sommet(pile) != <span class="hljs-string">'('</span>:
<span class="hljs-keyword">return</span> <span class="hljs-keyword">False</span>
depile(pile)
<span class="hljs-keyword">elif</span> car == <span class="hljs-string">']'</span>:
<span class="hljs-keyword">if</span> pile_vide(pile) <span class="hljs-keyword">or</span> sommet(pile) != <span class="hljs-string">'[]'</span>:
<span class="hljs-keyword">return</span> <span class="hljs-keyword">False</span>
depile(pile)
i += <span class="hljs-number">1</span>
<span class="hljs-keyword">return</span> pile_vide(pile)
</pre>
<footer style="position:fixed; font-size:.8em; text-align:right; bottom:0px; margin-left:-25px; height:20px; width:100%;">generated by <a href="http://pad.haroopress.com" target="_blank">haroopad</a></footer>
</body>
</html>