Skip to content

Commit

Permalink
sauvegarde automatique
Browse files Browse the repository at this point in the history
  • Loading branch information
donnerc committed Apr 26, 2024
1 parent e1270b9 commit d6a34b4
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 38 deletions.
77 changes: 68 additions & 9 deletions src/task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'''
This file contains a bug to illustrate the debugger in the gitpod environment.
TODO: to optimize, store for every affiche where the last geq has been seen ...
which improves finding the last greater than
other option : use some sort of dynamic programming to not only store the last
geq, but also the last geq compared to a given value => eats a lot of RAM
'''


Expand All @@ -12,16 +18,49 @@
pass

def debug(*args, **kwargs):
level = kwargs.get('level', 0)
if level >= DBG_LEVEL:
level = kwargs.get('level', 1)
if level <= DBG_LEVEL:
print(*args)

DBG_LEVEL = 0

def find_last_height_ge(heights, i, height):
while i >= 0:
if heights[i] >= height:
return i


def find_last_height_geq(heights, last_geq, i):
'''
>>> last_geq = [0, 0, 0, 0, 0, 0, 0]
>>> heights = [4, 3, 2, 1, 3, 5, 3]
>>> find_last_height_geq(heights, last_geq, 0)
-1
>>> last_geq
[-1, 0, 0, 0, 0, 0, 0]
>>> find_last_height_geq(heights, 1)
0
>>> find_last_height_geq(heights, 3)
2
>>> find_last_height_geq(heights, 4)
1
>>> find_last_height_geq(heights, 5)
-1
>>> find_last_height_geq(heights, 6)
5
>>>
'''
height = heights[i]
i -= 1

j = i
while j >= 0:
if heights[j] < height:
j = last_geq[j]
else:
return j
# j = -1
# for j in range(i, -1, -1):
# if heights[j] >= height:
# last_geq[i + 1] = j
# return j
return -1

def main():
'''
Expand All @@ -33,22 +72,42 @@ def main():
nb_queries = int(input())
nb_visibles = [0 for i in range(nb_queries)]
heights = [0 for i in range(nb_queries)]
last_geq = [0 for i in range(nb_queries)]

index_affiche = 0
for _ in range(nb_queries):
query = [x for x in input().split(' ')]
if len(query) == 1:
print("voir")
debug("query")
print(nb_visibles[index_affiche - 1])
else:
height = int(query[1])
debug("coller", height)
heights[index_affiche] = height
index_last_geq = find_last_height_geq(heights, last_geq, index_affiche)
debug("visibles", nb_visibles)
debug("heights", heights)
debug("last geq index", index_last_geq)
if index_last_geq == -1:
nb_visibles[index_affiche] = 1
elif heights[index_last_geq] == height:
nb_visibles[index_affiche] = nb_visibles[index_last_geq]
elif heights[index_last_geq] > height:
nb_visibles[index_affiche] = nb_visibles[index_last_geq] + 1

index_affiche += 1


debug(index_affiche)
debug("index_affiche", index_affiche)
debug("-----------")


if __name__ == '__main__':
main()
main()


try:
import doctest
doctest.testmod()
except:
print("Impossible de lancer les doctests")
98 changes: 69 additions & 29 deletions tests/test1.run.out

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions tests/test2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
10
Q
C 8
C 7
C 11
Q
C 2
C 4
C 3
Q
C 3
3 changes: 3 additions & 0 deletions tests/test2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0
1
3
69 changes: 69 additions & 0 deletions tests/test2.run.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
0
1
3
**********************************************************************
File "/workspace/soi-problem/src/task.py", line 35, in __main__.find_last_height_geq
Failed example:
last_geq
Expected nothing
Got:
[0, 0, 0, 0, 0, 0, 0]
**********************************************************************
File "/workspace/soi-problem/src/task.py", line 36, in __main__.find_last_height_geq
Failed example:
find_last_height_geq(heights, 1)
Exception raised:
Traceback (most recent call last):
File "/home/gitpod/.pyenv/versions/3.11.9/lib/python3.11/doctest.py", line 1355, in __run
exec(compile(example.source, filename, "single",
File "<doctest __main__.find_last_height_geq[4]>", line 1, in <module>
find_last_height_geq(heights, 1)
TypeError: find_last_height_geq() missing 1 required positional argument: 'i'
**********************************************************************
File "/workspace/soi-problem/src/task.py", line 38, in __main__.find_last_height_geq
Failed example:
find_last_height_geq(heights, 3)
Exception raised:
Traceback (most recent call last):
File "/home/gitpod/.pyenv/versions/3.11.9/lib/python3.11/doctest.py", line 1355, in __run
exec(compile(example.source, filename, "single",
File "<doctest __main__.find_last_height_geq[5]>", line 1, in <module>
find_last_height_geq(heights, 3)
TypeError: find_last_height_geq() missing 1 required positional argument: 'i'
**********************************************************************
File "/workspace/soi-problem/src/task.py", line 40, in __main__.find_last_height_geq
Failed example:
find_last_height_geq(heights, 4)
Exception raised:
Traceback (most recent call last):
File "/home/gitpod/.pyenv/versions/3.11.9/lib/python3.11/doctest.py", line 1355, in __run
exec(compile(example.source, filename, "single",
File "<doctest __main__.find_last_height_geq[6]>", line 1, in <module>
find_last_height_geq(heights, 4)
TypeError: find_last_height_geq() missing 1 required positional argument: 'i'
**********************************************************************
File "/workspace/soi-problem/src/task.py", line 42, in __main__.find_last_height_geq
Failed example:
find_last_height_geq(heights, 5)
Exception raised:
Traceback (most recent call last):
File "/home/gitpod/.pyenv/versions/3.11.9/lib/python3.11/doctest.py", line 1355, in __run
exec(compile(example.source, filename, "single",
File "<doctest __main__.find_last_height_geq[7]>", line 1, in <module>
find_last_height_geq(heights, 5)
TypeError: find_last_height_geq() missing 1 required positional argument: 'i'
**********************************************************************
File "/workspace/soi-problem/src/task.py", line 44, in __main__.find_last_height_geq
Failed example:
find_last_height_geq(heights, 6)
Exception raised:
Traceback (most recent call last):
File "/home/gitpod/.pyenv/versions/3.11.9/lib/python3.11/doctest.py", line 1355, in __run
exec(compile(example.source, filename, "single",
File "<doctest __main__.find_last_height_geq[8]>", line 1, in <module>
find_last_height_geq(heights, 6)
TypeError: find_last_height_geq() missing 1 required positional argument: 'i'
**********************************************************************
1 items had failures:
6 of 9 in __main__.find_last_height_geq
***Test Failed*** 6 failures.

0 comments on commit d6a34b4

Please sign in to comment.