Skip to content

Commit c6661fc

Browse files
add new question
1 parent 3c4ed34 commit c6661fc

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Shorted Path Dijkstra Algoritgm:
2+
# Along with the shortest distance, the shortest path also needed to be printed which made the problem quite difficult.
3+
4+
import heapq
5+
6+
def dijkstra_with_path(n, edges, source, destination):
7+
graph = [[] for _ in range(n)]
8+
for u, v, w in edges:
9+
graph[u].append((v, w))
10+
graph[v].append((u, w))
11+
12+
dist = [float('inf')] * n
13+
parent = [-1] * n
14+
dist[source] = 0
15+
16+
heap = [(0, source)]
17+
18+
while heap:
19+
d, u = heapq.heappop(heap)
20+
21+
for v, weight in graph[u]:
22+
if dist[u] + weight < dist[v]:
23+
dist[v] = dist[u] + weight
24+
parent[v] = u
25+
heapq.heappush(heap, (dist[v], v))
26+
27+
# Reconstruct path
28+
if dist[destination] == float('inf'):
29+
return -1, []
30+
31+
path = []
32+
current = destination
33+
while current != -1:
34+
path.append(current)
35+
current = parent[current]
36+
path.reverse()
37+
38+
return dist[destination], path
39+
40+
# Example
41+
n = 5
42+
edges = [
43+
(0, 1, 2),
44+
(1, 2, 4),
45+
(0, 3, 1),
46+
(3, 4, 3),
47+
(4, 2, 1)
48+
]
49+
source = 0
50+
destination = 2
51+
52+
distance, path = dijkstra_with_path(n, edges, source, destination)
53+
print("Shortest Distance:", distance)
54+
print("Shortest Path:", path)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#Sum of prime numbers upto N:
2+
#The optimal approach for this problem is the Sieve of Eratosthenes, but the brute-force approach also got accepted.
3+
4+
def is_prime(n):
5+
if n <= 1:
6+
return False
7+
for i in range(2, int(n**0.5)+1):
8+
if n % i == 0:
9+
return False
10+
return True
11+
12+
def sum_of_primes_brute(N):
13+
return sum(i for i in range(2, N+1) if is_prime(i))
14+
15+
# Example usage:
16+
N = 20
17+
print(sum_of_primes_brute(N)) # Output: 77

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
![GitHub contributors](https://img.shields.io/github/contributors/codewithdhruba01/Learn-python-language) ![GitHub License](https://img.shields.io/github/license/codewithdhruba01/Learn-python-language)
33
![GitHub issue/pull request detail](https://img.shields.io/github/issues/detail/author/codewithdhruba01/Learn-python-language/1) ![GitHub watchers](https://img.shields.io/github/watchers/codewithdhruba01/Learn-python-language) ![GitHub forks](https://img.shields.io/github/forks/codewithdhruba01/Learn-python-language)
44

5-
5+
# index
66

77
> [!TIP]
88
> Welcome to [**Learn Python Language**](https://github.com/codewithdhruba01/Learn-python-language) Read, Practice, Code!
@@ -43,7 +43,6 @@ This repository is part of **My Python learning journey,** created to help begin
4343

4444
---
4545

46-
[**Back To Top ⬆️**](#index)
4746

4847
# 📘 DSA Practice Sheets in Python
4948

@@ -162,8 +161,6 @@ This repository is part of **My Python learning journey,** created to help begin
162161

163162
If you are interested in writing code to fix issues, please see [How to Contribute](https://github.com/codewithdhruba01/Learn-python-language/blob/master/CONTRIBUTING.md) in the doc.
164163

165-
[**Back To Top ⬆️**](#index)
166-
167164
## Author
168165

169166
[![GitHub](https://img.shields.io/badge/GitHub_DhrubarajPati-%23121011.svg?logo=github&logoColor=white)](https://github.com/codewithdhruba01)

0 commit comments

Comments
 (0)