retrait dossier sources recursivité

This commit is contained in:
Florian Mathieu
2024-09-17 21:37:38 +02:00
parent 1ddc046f95
commit 3cb7064576
8 changed files with 0 additions and 2500 deletions

View File

@@ -1,15 +0,0 @@
def combinaisons(n,p):
result = 1
if n>p and p>0:
result = combinaisons(n-1,p-1) + combinaisons(n-1,p)
return result
print(combinaisons(32,5))
def combinaisons2(n,p):
if n>p and p>0:
return combinaisons(n-1,p-1) + combinaisons(n-1,p)
else:
return 1
print(combinaisons2(32,5))

View File

@@ -1,31 +0,0 @@
from functools import lru_cache
from functools import cache
@cache
def steps_to(stair):
if stair == 1:
# You can reach the first stair with only a single step from the floor.
return 1
elif stair == 2:
# You can reach the second stair by jumping from the floor with a single two-stair hop or by jumping a single stair a couple of times.
return 2
elif stair == 3:
# You can reach the third stair using four possible combinations:
# 1. Jumping all the way from the floor
# 2. Jumping two stairs, then one
# 3. Jumping one stair, then two
# 4. Jumping one stair three times
return 4
else:
# You can reach your current stair from three different places:
# 1. From three stairs down
# 2. From two stairs down
# 2. From one stair down
# If you add up the number of ways of getting to those
# those three positions, then you should have your solution.
return steps_to(stair - 3) + steps_to(stair - 2) + steps_to(stair - 1)
print(steps_to(35))

View File

@@ -1,30 +0,0 @@
digraph Fibonnacci {
#bgcolor="transparent"
"fib(6)" -> {fib4 [label="fib(4)"] fib5 [label="fib(5)"]}
subgraph cluster1 {
style="rounded,dashed";
fib4 -> {fib2 [label="fib(2)"] fib3 [label="fib(3)"]}
subgraph cluster1 {
style="rounded,dotted";
fib3 -> {fib1 [label="fib(1)"] fib22 [label="fib(2)"]}
}
}
fib5 -> {fib33 [label="fib(3)"] fib44 [label="fib(4)"]};
subgraph cluster2 {
style="rounded,dotted";
fib33 -> {fib11 [label="fib(1)"] fib222 [label="fib(2)"]};
}
subgraph cluster3 {
style="rounded,dashed";
fib44 -> {fib2222 [label="fib(2)"] fib333 [label="fib(3)"]}
subgraph cluster4 {
style="rounded,dotted";
fib333 -> {fib111 [label="fib(1)"] fib22222 [label="fib(2)"]}
}
}
}

View File

@@ -1,41 +0,0 @@
from random import randint
nb_interrupteurs = 10
etats_interrupteurs = [_%2==1 for _ in range(nb_interrupteurs)]
etats_interrupteurs = [False for _ in range(nb_interrupteurs)]
etats_interrupteurs = [randint(0,1)==0 for _ in range(nb_interrupteurs)]
commutations = []
print(etats_interrupteurs)
def inverse_interrupteur(n: int):
global etats_interrupteurs
global commutations
#print("On inverse l'interrupteur", n)
commutations.append(n)
etats_interrupteurs[n-1] = not etats_interrupteurs[n-1]
def islight() -> bool:
global etats_interrupteurs
result = True
for etat_interrupteur in etats_interrupteurs:
if not etat_interrupteur:
result = False
break
return result
def cherche_commutations(n):
#print("commute", n)
if n == 1 and not islight():
inverse_interrupteur(n)
elif n > 1 and not islight():
cherche_commutations(n-1)
if not islight():
inverse_interrupteur(n)
if not islight():
cherche_commutations(n-1)
cherche_commutations(nb_interrupteurs)
print(etats_interrupteurs)
print(len(commutations))

View File

@@ -1,38 +0,0 @@
def multiplication_egyptienne(n: int, m: int) -> int:
assert n>=1 and m>=1
result = m
if n > 1:
if n % 2 == 0:
result = multiplication_egyptienne(n//2, m*2)
else:
result = m + multiplication_egyptienne(n-1, m)
return result
print(multiplication_egyptienne(7,5))
def multiplication_egyptienne2(n: int, m: int) -> int:
assert n>=1 and m>=1
if n == 1:
return m
elif n % 2 == 0:
return multiplication_egyptienne(n//2, m*2)
else:
return m + multiplication_egyptienne(n-1, m)
print(multiplication_egyptienne2(7,5))
def multiplication_egyptienne_iterative(n: int, m: int) -> int:
assert n > m
assert n>=1 and m>=1
result = 0
while n > 1:
if n % 2 != 0:
result += m
n = n-1
else :
n = n//2
m = m*2
result += m
return result
print(multiplication_egyptienne_iterative(7,5))

File diff suppressed because one or more lines are too long

View File

@@ -1,207 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.44.0 (0)
-->
<!-- Title: Fibonnacci Pages: 1 -->
<svg width="683pt" height="356pt"
viewBox="0.00 0.00 683.00 356.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 352)">
<title>Fibonnacci</title>
<polygon fill="white" stroke="transparent" points="-4,4 -4,-352 679,-352 679,4 -4,4"/>
<g id="clust2" class="cluster">
<title>cluster1</title>
<path fill="none" stroke="black" stroke-dasharray="5,2" d="M20,-80C20,-80 239,-80 239,-80 245,-80 251,-86 251,-92 251,-92 251,-272 251,-272 251,-278 245,-284 239,-284 239,-284 20,-284 20,-284 14,-284 8,-278 8,-272 8,-272 8,-92 8,-92 8,-86 14,-80 20,-80"/>
</g>
<g id="clust4" class="cluster">
<title>cluster1</title>
<path fill="none" stroke="black" stroke-dasharray="1,5" d="M98,-88C98,-88 231,-88 231,-88 237,-88 243,-94 243,-100 243,-100 243,-200 243,-200 243,-206 237,-212 231,-212 231,-212 98,-212 98,-212 92,-212 86,-206 86,-200 86,-200 86,-100 86,-100 86,-94 92,-88 98,-88"/>
</g>
<g id="clust7" class="cluster">
<title>cluster2</title>
<path fill="none" stroke="black" stroke-dasharray="1,5" d="M271,-88C271,-88 404,-88 404,-88 410,-88 416,-94 416,-100 416,-100 416,-200 416,-200 416,-206 410,-212 404,-212 404,-212 271,-212 271,-212 265,-212 259,-206 259,-200 259,-200 259,-100 259,-100 259,-94 265,-88 271,-88"/>
</g>
<g id="clust9" class="cluster">
<title>cluster3</title>
<path fill="none" stroke="black" stroke-dasharray="5,2" d="M436,-8C436,-8 655,-8 655,-8 661,-8 667,-14 667,-20 667,-20 667,-200 667,-200 667,-206 661,-212 655,-212 655,-212 436,-212 436,-212 430,-212 424,-206 424,-200 424,-200 424,-20 424,-20 424,-14 430,-8 436,-8"/>
</g>
<g id="clust11" class="cluster">
<title>cluster4</title>
<path fill="none" stroke="black" stroke-dasharray="1,5" d="M514,-16C514,-16 647,-16 647,-16 653,-16 659,-22 659,-28 659,-28 659,-128 659,-128 659,-134 653,-140 647,-140 647,-140 514,-140 514,-140 508,-140 502,-134 502,-128 502,-128 502,-28 502,-28 502,-22 508,-16 514,-16"/>
</g>
<!-- fib(6) -->
<g id="node1" class="node">
<title>fib(6)</title>
<ellipse fill="none" stroke="black" cx="231" cy="-330" rx="30.59" ry="18"/>
<text text-anchor="middle" x="231" y="-326.3" font-family="Times,serif" font-size="14.00">fib(6)</text>
</g>
<!-- fib4 -->
<g id="node2" class="node">
<title>fib4</title>
<ellipse fill="none" stroke="black" cx="165" cy="-258" rx="30.59" ry="18"/>
<text text-anchor="middle" x="165" y="-254.3" font-family="Times,serif" font-size="14.00">fib(4)</text>
</g>
<!-- fib(6)&#45;&gt;fib4 -->
<g id="edge1" class="edge">
<title>fib(6)&#45;&gt;fib4</title>
<path fill="none" stroke="black" d="M216.68,-313.81C207.77,-304.36 196.15,-292.04 186.19,-281.48"/>
<polygon fill="black" stroke="black" points="188.7,-279.03 179.29,-274.16 183.61,-283.84 188.7,-279.03"/>
</g>
<!-- fib5 -->
<g id="node3" class="node">
<title>fib5</title>
<ellipse fill="none" stroke="black" cx="337" cy="-258" rx="30.59" ry="18"/>
<text text-anchor="middle" x="337" y="-254.3" font-family="Times,serif" font-size="14.00">fib(5)</text>
</g>
<!-- fib(6)&#45;&gt;fib5 -->
<g id="edge2" class="edge">
<title>fib(6)&#45;&gt;fib5</title>
<path fill="none" stroke="black" d="M250.94,-315.83C267.33,-305.01 290.74,-289.55 309.01,-277.49"/>
<polygon fill="black" stroke="black" points="311.09,-280.3 317.51,-271.87 307.24,-274.46 311.09,-280.3"/>
</g>
<!-- fib2 -->
<g id="node4" class="node">
<title>fib2</title>
<ellipse fill="none" stroke="black" cx="47" cy="-186" rx="30.59" ry="18"/>
<text text-anchor="middle" x="47" y="-182.3" font-family="Times,serif" font-size="14.00">fib(2)</text>
</g>
<!-- fib4&#45;&gt;fib2 -->
<g id="edge3" class="edge">
<title>fib4&#45;&gt;fib2</title>
<path fill="none" stroke="black" d="M142.38,-245.7C125.45,-237.01 101.88,-224.42 82,-212 79.21,-210.26 76.35,-208.38 73.52,-206.46"/>
<polygon fill="black" stroke="black" points="75.44,-203.53 65.24,-200.67 71.43,-209.27 75.44,-203.53"/>
</g>
<!-- fib3 -->
<g id="node5" class="node">
<title>fib3</title>
<ellipse fill="none" stroke="black" cx="165" cy="-186" rx="30.59" ry="18"/>
<text text-anchor="middle" x="165" y="-182.3" font-family="Times,serif" font-size="14.00">fib(3)</text>
</g>
<!-- fib4&#45;&gt;fib3 -->
<g id="edge4" class="edge">
<title>fib4&#45;&gt;fib3</title>
<path fill="none" stroke="black" d="M165,-239.7C165,-231.98 165,-222.71 165,-214.11"/>
<polygon fill="black" stroke="black" points="168.5,-214.1 165,-204.1 161.5,-214.1 168.5,-214.1"/>
</g>
<!-- fib33 -->
<g id="node8" class="node">
<title>fib33</title>
<ellipse fill="none" stroke="black" cx="337" cy="-186" rx="30.59" ry="18"/>
<text text-anchor="middle" x="337" y="-182.3" font-family="Times,serif" font-size="14.00">fib(3)</text>
</g>
<!-- fib5&#45;&gt;fib33 -->
<g id="edge7" class="edge">
<title>fib5&#45;&gt;fib33</title>
<path fill="none" stroke="black" d="M337,-239.7C337,-231.98 337,-222.71 337,-214.11"/>
<polygon fill="black" stroke="black" points="340.5,-214.1 337,-204.1 333.5,-214.1 340.5,-214.1"/>
</g>
<!-- fib44 -->
<g id="node9" class="node">
<title>fib44</title>
<ellipse fill="none" stroke="black" cx="463" cy="-186" rx="30.59" ry="18"/>
<text text-anchor="middle" x="463" y="-182.3" font-family="Times,serif" font-size="14.00">fib(4)</text>
</g>
<!-- fib5&#45;&gt;fib44 -->
<g id="edge8" class="edge">
<title>fib5&#45;&gt;fib44</title>
<path fill="none" stroke="black" d="M358.94,-245.33C375.73,-236.35 399.42,-223.56 420,-212 424.29,-209.59 428.8,-207.01 433.23,-204.46"/>
<polygon fill="black" stroke="black" points="435.18,-207.38 442.08,-199.33 431.67,-201.32 435.18,-207.38"/>
</g>
<!-- fib1 -->
<g id="node6" class="node">
<title>fib1</title>
<ellipse fill="none" stroke="black" cx="125" cy="-114" rx="30.59" ry="18"/>
<text text-anchor="middle" x="125" y="-110.3" font-family="Times,serif" font-size="14.00">fib(1)</text>
</g>
<!-- fib3&#45;&gt;fib1 -->
<g id="edge5" class="edge">
<title>fib3&#45;&gt;fib1</title>
<path fill="none" stroke="black" d="M155.72,-168.76C150.95,-160.4 145.01,-150.02 139.63,-140.61"/>
<polygon fill="black" stroke="black" points="142.51,-138.58 134.5,-131.63 136.43,-142.05 142.51,-138.58"/>
</g>
<!-- fib22 -->
<g id="node7" class="node">
<title>fib22</title>
<ellipse fill="none" stroke="black" cx="204" cy="-114" rx="30.59" ry="18"/>
<text text-anchor="middle" x="204" y="-110.3" font-family="Times,serif" font-size="14.00">fib(2)</text>
</g>
<!-- fib3&#45;&gt;fib22 -->
<g id="edge6" class="edge">
<title>fib3&#45;&gt;fib22</title>
<path fill="none" stroke="black" d="M174.05,-168.76C178.7,-160.4 184.49,-150.02 189.73,-140.61"/>
<polygon fill="black" stroke="black" points="192.92,-142.07 194.73,-131.63 186.81,-138.67 192.92,-142.07"/>
</g>
<!-- fib11 -->
<g id="node10" class="node">
<title>fib11</title>
<ellipse fill="none" stroke="black" cx="298" cy="-114" rx="30.59" ry="18"/>
<text text-anchor="middle" x="298" y="-110.3" font-family="Times,serif" font-size="14.00">fib(1)</text>
</g>
<!-- fib33&#45;&gt;fib11 -->
<g id="edge9" class="edge">
<title>fib33&#45;&gt;fib11</title>
<path fill="none" stroke="black" d="M327.95,-168.76C323.3,-160.4 317.51,-150.02 312.27,-140.61"/>
<polygon fill="black" stroke="black" points="315.19,-138.67 307.27,-131.63 309.08,-142.07 315.19,-138.67"/>
</g>
<!-- fib222 -->
<g id="node11" class="node">
<title>fib222</title>
<ellipse fill="none" stroke="black" cx="377" cy="-114" rx="30.59" ry="18"/>
<text text-anchor="middle" x="377" y="-110.3" font-family="Times,serif" font-size="14.00">fib(2)</text>
</g>
<!-- fib33&#45;&gt;fib222 -->
<g id="edge10" class="edge">
<title>fib33&#45;&gt;fib222</title>
<path fill="none" stroke="black" d="M346.28,-168.76C351.05,-160.4 356.99,-150.02 362.37,-140.61"/>
<polygon fill="black" stroke="black" points="365.57,-142.05 367.5,-131.63 359.49,-138.58 365.57,-142.05"/>
</g>
<!-- fib2222 -->
<g id="node12" class="node">
<title>fib2222</title>
<ellipse fill="none" stroke="black" cx="463" cy="-114" rx="30.59" ry="18"/>
<text text-anchor="middle" x="463" y="-110.3" font-family="Times,serif" font-size="14.00">fib(2)</text>
</g>
<!-- fib44&#45;&gt;fib2222 -->
<g id="edge11" class="edge">
<title>fib44&#45;&gt;fib2222</title>
<path fill="none" stroke="black" d="M463,-167.7C463,-159.98 463,-150.71 463,-142.11"/>
<polygon fill="black" stroke="black" points="466.5,-142.1 463,-132.1 459.5,-142.1 466.5,-142.1"/>
</g>
<!-- fib333 -->
<g id="node13" class="node">
<title>fib333</title>
<ellipse fill="none" stroke="black" cx="542" cy="-114" rx="30.59" ry="18"/>
<text text-anchor="middle" x="542" y="-110.3" font-family="Times,serif" font-size="14.00">fib(3)</text>
</g>
<!-- fib44&#45;&gt;fib333 -->
<g id="edge12" class="edge">
<title>fib44&#45;&gt;fib333</title>
<path fill="none" stroke="black" d="M479.37,-170.5C490.49,-160.64 505.39,-147.44 517.87,-136.38"/>
<polygon fill="black" stroke="black" points="520.49,-138.74 525.65,-129.49 515.85,-133.5 520.49,-138.74"/>
</g>
<!-- fib111 -->
<g id="node14" class="node">
<title>fib111</title>
<ellipse fill="none" stroke="black" cx="541" cy="-42" rx="30.59" ry="18"/>
<text text-anchor="middle" x="541" y="-38.3" font-family="Times,serif" font-size="14.00">fib(1)</text>
</g>
<!-- fib333&#45;&gt;fib111 -->
<g id="edge13" class="edge">
<title>fib333&#45;&gt;fib111</title>
<path fill="none" stroke="black" d="M541.75,-95.7C541.64,-87.98 541.51,-78.71 541.39,-70.11"/>
<polygon fill="black" stroke="black" points="544.89,-70.05 541.24,-60.1 537.89,-70.15 544.89,-70.05"/>
</g>
<!-- fib22222 -->
<g id="node15" class="node">
<title>fib22222</title>
<ellipse fill="none" stroke="black" cx="620" cy="-42" rx="30.59" ry="18"/>
<text text-anchor="middle" x="620" y="-38.3" font-family="Times,serif" font-size="14.00">fib(2)</text>
</g>
<!-- fib333&#45;&gt;fib22222 -->
<g id="edge14" class="edge">
<title>fib333&#45;&gt;fib22222</title>
<path fill="none" stroke="black" d="M558.16,-98.5C569.14,-88.64 583.85,-75.44 596.17,-64.38"/>
<polygon fill="black" stroke="black" points="598.75,-66.77 603.86,-57.49 594.08,-61.56 598.75,-66.77"/>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 10 KiB