0 ratings0% found this document useful (0 votes) 52 views20 pagesJupyter Code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
ovive02s 12.52 Untidedt - Jupyter Notebook
Entrée [3]: def maxSubsetSum(arr):
n = len(arr)
dp = [Link]()
maxi_@ = max(@,arr[0])
maxi_1 = max(@,arr(1])
dpe] = maxi_e
dp[1] = maxi_t
for i in range(2,n):
dp[i] = max(®,arr[i}+maxi_e)
maxi_@ = maxi_1
maxi_1 = max(maxi_t,dp[])
print (dp)
return max(dp)
Entrée [4]: arr = [3, 2, 1, 10]
maxSubsetSum(arr)
(3, 2, 4, 12]
out[4]: 12
Entrée [1]: def profit_maximal (prix):
if not prix:
return @
prix_min = prix[@]
profit_max = @
for p in prix:
profit_potentiel = p - prixmin
profit_max = max(profit_max, profit_potentiel)
prix_min = min(prix_min, p)
return profit_max
Entrée [2]: prices = (108, 90, 50, 82, 132, 100]
Entrée [4]: profit_maxinal (prices)
outa
80
localhost:88@8inotebooks/Desktop/Unttled pyr
+120ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [5]: def profit_maximal_2_transac(prix):
if not prix:
return @
# Nombre maximal de transactions.
ka2
n= len(prix)
dp = [[@ for _ in range(K+1)] for
in range(n)]
for j in range(1, K#+1):
max_val = -prix[o]
for i in range(1, n):
dpliJ[3] = max(dp[i-2][J], prix[i] + max_val)
max_val = max(max_val, dp[i-1][j-1] - prix[i])
return dp[n-1][k]
prix_futurs = [100, 180, 260, 310, 40, 535, 695]
print (profit_maximal_2 transac(prix_futurs)) # Cela devrait afficher Le pri
865
Entrée [6]: prices = [10, 22, 5, 75, 65, 80]
profit_maximal_2_transac(prices)
out(s}: 87
Entrée [2]:
NameError Traceback (most recent call las
t)
Cell In[2], line 22
19 return max(max_sum, ©)
21 # Test the function with an example
---> 22 getMaxProfitoptimized([100, 200, -308, 400, -100, 200], 3)
NameError: name ‘getMaxProfitoptimized’ is not defined
localhost 8888/notebooks/Desktop/Untiledipynt 220ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [3]: def getMaxProfitoptimized(pl, k):
n= len(pl)
max_profit = 0
for i in range(1, k + 1):
# Calculate the sum of the first window of size i
window_sum = sum(p1[:4])
max_sum = window_sum
# Slide the window through the array
for j in range(i, n):
# Add the next element and remove the first element from the pr¢
window_sum += pl[j] - pl[j - i]
max_sum = max(max_sum, window_sum)
# Update the max profit if this window size gives a better profit
max_profit = max(max_profit, max_sum)
return max_profit
# Test the function with an example
getMaxProfitoptimized([109, 200, -300, 400, -100, 200], 3)
out[(3]: see
Entrée [7]: def getMaxProfitoP(pl, k):
n= len(pl
ifn == 0:
return @
dp = [9] *n
dp[2} = ple]
max_sum = dp[@]
for i in range(1, n):
ifi @ else pl[i]
else:
# Once we reach k elements, we need to consider subtracting the
dp[i] = max(dp[i - 1] + pl[4] - pl{i - k], pl[4])
# Return the maximum value in dp, which indicates the maximum profit
return max(dp)
# Test the function with an example
print (getMaxProfitoP([4, 3, -2, 9, -4, 2, 7], 6))
——EEEE! »
15,
localhost 8888/notebooks/Desktop/ Untied pyr
4720ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [13]: from collections import deque
def maxProfit(pl, k):
n = len(pl)
if n == 0:
return @
# Step 1: Compute cumulative sum
cumul_list = [2] * (n + 1)
for i in range(i, n + 1):
cumul_list[i] = cumul_list[i - 1] + pl{i - 1]
# Step 2: Compute minimum List using a deque
min_list = [0] *
dq = deque()
for i in range(1, 9 + 1):
# Remove elements outside the current window
while dq and dq[@] < i - k:
[Link]()
# Current minimum (front of the deque)
min_list[i - 1] = cumul_list[dq[o]] if dq else 0
# Maintain deque order by values of cumul_List
while dg and cumul_list[i] <= cumul_list[dq[-1]]:
[Link]()
dq-append(i)
# Step 3: Find maximum profit
max_profit = max(cumul_list[i] - min_list[i - 1] for i in range(1, n +
return max_profit
print (getMaxProfitoP([4, 3, -2, 9, -4, 2, 7], 6))
15
localhost 8888/notebooks/Desktop/Untiledipynt 520ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [12]: from collections import deque
def maxProfit(pl, k):
n = len(pl)
if n == 0:
return @
# Step 1: Compute cumulative sum
cumul_list = [2] * (n + 1)
for i in range(i, n + 1):
cumul_list[i] = cumul_list[i - 1] + pl{i - 2]
# Step 2: Compute minimum List using a deque
min_list = [0] *
dq = deque()
for i in range(1, 9 + 1):
# Remove elements outside the current window
while dq and dq[@] < i - k:
dq. popleft()
# Current minimum (front of the deque)
min_list[i - 1] = cumul_list[dq[o]] if dq else 0
# Maintain deque order by values of cumul_list
while dg and cumul_list[i] <= cumul_list[dq[-1]]:
[Link]()
dq-append(i)
# Step 3: Find maximum profit
max_profit = max(cumul_list[i] - min_list[i - 1] for i in range(1, a +
return max_profit
15
localhost 8888/notebooks/Desktop/Untiledipynt 620ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [10]: def getMaxProfitOP(pl, k):
n= len(pl)
ifn == 0:
return 0
# Initialize an array to store the maximum profit up to each point
max_profit_up_to_i = [@] *n
max_profit_up_to_i[@] = max(pl[0], 0)
max_sum = max_profit_up_to_i[0]
current_window_sum = pl[@]
max_sum_last_k = p1[0]
for i in range(1, n):
# Update the window sum
ifick:
current_window_sum += pl[4]
max_sum_last_k = max(max_sum_last_k, current_window_sum)
else:
current_window_sum += pl[i] - pl[i - k]
# Calculate max profit for this position
max_profit_up_to_i[i] = max(max_profit_up_to_i[i - 1] + pl[i], curr
max_sum = ax(max_sum, max_profit_up_to_i[i])
return max(max_sum, 0)
# Test the function with a few examples
print(getMaxProfitoP([100, 200, -300, 400, -100, 200], 3)) # Expected outpu
print (getMaxProfitoP([300], 3)) # Expected output
print(getMaxProfitoP([4, 3, -2, 9, -4, 2, 7], 6) ) # Expected output: |
see
300
19
localhost 8888/notebooks/Desktop/Untiledipynt 720ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [16]: def clean_building(plan):
len(plan), len(plan[@])
[1ist(row) for row in plan] # Convert to a List of Lists for mi
def dfs(r, c):
ifr <@orc< ore
return
plan{r][c] = # Clean this cell
for dr, de in [(-1, ®), (1, @), (@, -1), (@, 1)]
dfs(r + dr, c + de)
= R or ¢ >= C or plan(r][c]
runs = 0
for r in range(R):
for ¢ in range(c):
if plan[r][c]
dfs(r, c)
runs += 1
return runs
# Test the function
pland = [°.4#..*5, oFWr ws, CHM TH a]
clean_building(plan1)
Entrée [17]: clean_building(plan1)
out[17,
4
Entrée [18]: def clean_building(plan):
R, C= len(plan), len(plan[2])
plan = [list(row) for row in plan} # Convert to a List of Lists for mu
def dfs(r, c):
if <@orc< Gorn >= Ror c >= Cor plan(r][c] in ('#', *
return
plan[r][c] = '.' # Clean this cell
for dr, de in ((-1, 0), (1, @), (@, -1), (@, 1)]:
dfs(r + dr, c + de)
runs = 0
for r in range(R):
for ¢ in range(c):
if plan[r][c] == '*':
dfs(r, c)
runs += 1
return runs
# Test the function
pland = [°.48..*°, twee, Caen EH tt]
pland = ["*#..', #8, a, Tt]
print(clean_building(plan1)) # Output: 3
print(clean_building(plan2)) # Output: 2
localhost 8888/notebooks/Desktop/Untiledipynt 820onnt202s 1282 LUntiledt - Jupyter Notebook
Entrée [19]: def clean_building(plan):
len(plan), len(plan[@])
[1ist(row) for row in plan] # Convert to a List of Lists for mu
def dfs(r, c):
ifr <@orc< ore
return
plan(r][c] = # Marking the cell as cleaned
for dr, de in ((-1, 8), (1, @), (@, -1), (@, 1)):
nr, nc = r+ dr, c+ de
if @ <= nr < Rand @ <= nc < C and plan{nr][nc] == “**:
dfs(nr, nc)
= R or ¢ >= C or plan[r][c]
runs = 0
for r in range(R):
for ¢ in range(c
if plan[r]{c]
df s(n, c)
runs += 1
return runs
# Test the function
pland = ['.8..8', ote, Came, ta HT]
print(clean_building(plan1)) # Expected output: 3
localhost 8888/notebooks/Desktop/Untiledipynt 920ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [20]: def clean_building(plan):
len(plan), len(plan[@])
[1ist(row) for row in plan] # Convert to a List of Lists for mu
def dfs(r, c):
ifr <@orc= R orc >= C or plan(ri{c]
return False
# If the cell is already visited, don't revisit
if plan{r][c] == 'V':
return False
# Check if the cell is dirty before marking it as visited
is_dirty = plan[r][c]
plan(r}[c] = 'V' # Mark the cell as visited
# Explore adjacent cells
if dfs(r +1, ¢):
is_dirty = True
if dfs(r - 1, ¢):
is_dirty = True
if dfs(r, ¢ +41):
is_dirty = True
if dfs(r, ¢ = 1):
is_dirty = True
return is_dirty
runs = 0
for r in range(R):
for c in range(C):
if plan[r][c] in ("*", '."): # Start DFS if it's an unvisited
if dfs(r, c): # If any part of the area is dirty, incremen’
runs += 1
return runs
# Test the function
pland = ['.*..8', Mera, Samm
plan2 = ['*#..', ‘Hey Tat Te]
print(clean_building(plan1)) # Expected Output: 3
print(clean_building(plan2)) # Expected Output: 2
localhost 8888/notebooks/Desktop/Untiledipynt
[1ist(row) for row in plan] # Convert to a List of Lists for mu
1120ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [22]: import numpy as np
import pandas as pd
from sklearn.1inear_model import LinearRegression
class AnalysisbataAndFitLinearRegression:
def _ init__(self}
[Link] = 1
def analyse_and_fit_lrm(self, path):
# path to dataset is "./data/[Link]"
# dataset can be Loaded by uncommenting the Line below
# data = pd.read_csv(path)
def _listwise_deletion(self, data: [Link]):
Feturn [Link]()
Entrée [23]: sum([True,False,True])
out[23}: 2
localhost 8888/notebooks/Desktop/Untiledipynt
1220osrt2028 1252 Unite - Jupyter Notebook
Entrée [24]: from collections import deque
def clean_building(plan):
R, C = len(plan), len(plan[2])
plan = [list(row) for row in plan] # Convert to a List of lists for mu
def bfs(r,
queue = deque([(r, c)])
is_dirty = False
while queue:
r, c = [Link]()
# Continue if out of bounds or is a wall
if p< @orc< @orr >= Rorc >=C or plan[r][c]
continue
# Skip if the cell is already visited
if plan[r][c] :
continue
# Check if the cell is dirty
if plan[r][c] == '*':
is_dirty = True
plan[r][c] = 'V' # Mark the cell as visited
# Add adjacent cells to the queue
[Link]((r + 1, c))
[Link]((r - 1, c))
queue. append((r, ¢ + 1))
[Link]((r, ¢ - 1))
return is_dirty
runs = 0
for r in range(R):
for c in range(c):
if plan[r][c] in ('*', '.'): # Start BFS if it's an unvisited
if bfs(r, c): # If any part of the area is dirty, incremen
runs += 1
return runs
# Test the function
pland = ['.*#..8', eta, Camemee, Tt
pland = ['*.. 5 Hey Tat Te]
print(clean_building(plan1)) # Expected Output: 3
print(clean_building(plan2)) # Expected Output: 2
localhost 8888/notebooks/Desktop/Untiledipynt 13720ovrti2023 12.52 Untied! - Jupyter Notebook
Entrée [25]: from collections import deque
def clean_building(plan):
R, C= len(plan), len(plan[2])
plan = [list(row) for row in plan] # Convert to a List of lists for mu
def bfs(r,
queue = deque([(r, c)])
is_dirty = False
while queue:
r, c = [Link]()
# Continue if out of bounds or is a wall
ifr <@orc=Rorc
continue
# Check if the cell is dirty
if plan[r][c]
is_dirty = True
plan[r][c] = 'V' # Mark the ceLl as visited
# Add adjacent cells to the queue using map
list(map([Link], [(r + 1, c), (r= 1, c), (r, © #41), (ry
return is_dirty
runs = 0
for r in range(R):
for ¢ in range(c):
if plan[r][c] in ("*", '.'): # Start BFS if it's an unvisited |
if bfs(r, c): # If any part of the area is dirty, incremen
runs += 1
return runs
# Test the function
pland = [".4#..*9, Far .', Came Td ai]
plan = ['*#..', #8, TS]
print(clean_building(plan1)) # Expected Output: 3
print(clean_building(plan2)) # Expected output:
localhost 8888/notebooks/Desktop/Untiledipynt 14720ovive02s 12.52 Untidedt - Jupyter Notebook
Entrée [86]: def clean_building(plan):
nb_lignes, nb_colonnes = len(plan), en(plan[@])
visited
set()
def explore(Ligne, colonne):
should_be_cleaned = False
if ligneco or ligne>=nb_lignes or colonne <@ or colonne >= nb_colon
return False
visited. add((ligne,colonne))
if plan[1igne][colonne]
return False
voisins = [(ligne+i,colonne), (1igne-1,colonne), (ligne, colonne+1), (1
if plan[1igne][colonne]
should_be cleaned = True
for voisin in voisins :
if not voisin in visited:
[Link](voisin)
should_be_cleaned = explore(voisin[@],voisin[1]) or should_
return should_be_cleaned
rez = 0
for ligne in range(nb_lignes):
for colonne in range(nb_colonnes) :
if not (Ligne,colonne) in visited and not plan[ligne][colonne]=
k = int(explore(1igne, colonne))
rezt=k
return rez
Entrée [88]:
# Test the function
pland = ['.48..*', ‘otwt on, ‘amen ta ttt)
pland = ["*#..', #8, ToS]
print(clean_building(plan1)) # Expected Output: 3
print(clean_building(plan2)) # Expected Output: 2
localhost:88@8inotebooks/Desktop/Unttled pyr 18120ovive02s 12.52
Entrée [81]:
Entrée [82]:
out(a2):
Entrée [83]:
out [83]:
Untiledt - Jupyter Notebook
pland = [".*#..8°, MPG, CRBS a tT]
nb_lignes, nb_colonnes = len(plani), len(plani[6])
visited = set()
def explore(1igne, colonne):
should_be_cleaned = False
if Ligne<@ or ligne>=nb_lignes or colonne <® or colonne >= nb_colonnes:
return False
[Link]((Ligne, colonne))
if plan{ligne][colonne] =
return False
voisins = [(1igne+1,colonne), (1igne-1,colonne), (1igne, colonne+1), (ligne
if plan[ Ligne] [colonne]
should_be_cleaned = True
for voisin in voisins :
if not voisin in visited:
[Link](voisin)
should_be_cleaned = explore(voisin[@],voisin[1]) or should _be_c
return should_be_cleaned
explore(2,3)
True
visited
{C-1, 3),
C4, 4),
(-1, 5),
(@, 2),
(@, 3),
(@, 4),
(@, 5),
(@, 6),
(1, 2),
(, 3),
(a, 4),
(1, 5),
(2, 3),
(2, 4)}
localhost:88@8inotebooks/Desktop/Unttled pyr
16120ovive02s 12.52
Entrée [89]:
Entrée [90]:
out[99]:
Entrée [91]:
out(91]:
Untiledt - Jupyter Notebook
def clean_building(plan):
nb_lignes, nb_colonnes = len(plan), en(plan[@])
visited = set()
def explore(ligne, colonne):
if Ligne < @ or ligne >= nb_lignes or colonne < @ or colonne >= nb_
return False
if (ligne, colonne) in visited or plan[ligne][colonne]
return False
[Link]((ligne, colonne))
should_be_cleaned = plan{ligne][colonne] == '*'
voisins = [(ligne +1, colonne), (ligne - 1, colonne), (ligne, colo
for voisin in voisins:
if voisin not in visited
should_be_cleaned
explore(voisin[@], voisin{1])
return should_be_cleaned
rez = 0
for ligne in range(nb_lignes):
for colonne in range(nb_colonnes):
if (Ligne, colonne) not in visited and plan[ligne][colonne]
rez += int(explore(ligne, colonne))
return rez
# Test the function
pland = [°.4#..*°, tats, ‘ame a tT]
plan = ['*#..', #8, BTS TY
print(clean_building(plan1)) # Expected output: 3
print(clean_building(plan2)) # Expected Output: 2
1
plan = [
we
]
clean_puilding(plan)
2
localhost:88@8inotebooks/Desktop/Unttled pyr
‘720ovive02s 12.52 Untidedt - Jupyter Notebook
Entrée [92]: plan = [
“aii,
syeeay
“tet,
“wee,
“sie”
]
clean_building(plan)
out [92,
1
Entrée [93]: plan = [
sepeHe,
cere,
segeye
“aa,
]
clean_building(plan)
out[93]:
9
Entrée [94
plan = [
vesunHee,
va,
SENET,
vayeasyee”
aie
ogee
]
clean_building(plan)
out[94]:
Entrée [96
clean_building(plan)
out[96]: 2
localhost:88@8inotebooks/Desktop/Unttled pyr
18120ovrti2023 12.52
Entrée [100]:
Untied - Jupyter Notebook
def clean_building(plan):
nb_lignes, nb_colonnes = len(plan), len(plan[o])
visited = set()
def explore(ligne, colonne):
to_visit = [(Ligne,colonne)]
should_be_cleaned = False
while to_visit:
current_ligne, current_colonne = to_visit.pop()
if (current_ligne, current_colonne) in visited:
continue
elif current_ligne < @ or current_ligne >= nb_lignes or curren
continue
elif plan[current_ligne] [current_colonne]
continue
[Link]((current_Ligne, current_colonne))
should_be_cleaned = should_be_cleaned or (plan[current_ligne][
voisins = [(current_ligne + 1, current_colonne), (current_lign
for element in voisins:
to_visit.append(element)
return should_be_cleaned
rez = 0
for ligne in range(nb_lignes):
for colonne in range(nb_colonnes)
if (ligne, colonne) not in visited and plan[ligne][colonne] !
rez += int(explore(ligne, colonne))
return rez
# Test the function
plant = ['.*#..8', ‘tHe, “amamee a aT]
plan2 = ['*#..', HH, aH tt]
print(clean_building(plan1)) # Expected outpu
print(clean_building(plan2)) # Expected Outpu
localhost 8888/notebooks/Desktop/Untiledipynt
1920ovrti2023 12.52
Entrée [102]:
out [162
Entrée [103]:
out[163]:
Entrée [104]:
Entrée [
Untied - Jupyter Notebook
plan = [
“anita,
tysesye
ane,
ee,
“at
J
clean_building(plan)
1
plan = [
vena,
vegenene
“aE
vegeeeye
tent
et
J
clean_building(plan)
def solution(s):
seen_b = False
for ele in
if ele
if seen_b:
return False
"bi:
True
elif ele
seen_b
return True
localhost 8888/notebooks/Desktop/Untiledipynt
20720