Skip to content

Commit 5207f28

Browse files
committed
Add view code button
1 parent 3c986a5 commit 5207f28

File tree

3 files changed

+135
-139
lines changed

3 files changed

+135
-139
lines changed

leetcode/nonCoprimes/nonCoprimes.html

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<!-- saved from url=(0058)file:///C:/Users/nasci/AppData/Local/Temp/tmpfod_hznj.html -->
2+
<!-- saved from url=(0058)file:///C:/Users/nasci/AppData/Local/Temp/tmpyyf_t802.html -->
33
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
44

55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -46,39 +46,48 @@
4646
background-clip: text;
4747
}
4848

49-
.description {
50-
font-size: 1.2em;
51-
opacity: 0.9;
52-
max-width: 600px;
53-
margin: 0 auto;
54-
line-height: 1.6;
55-
}
56-
57-
.problem-link-container {
58-
margin-top: 16px;
49+
.header-links {
50+
display: flex;
51+
justify-content: center;
52+
gap: 16px;
53+
margin: 20px 0;
54+
flex-wrap: wrap;
5955
}
6056

61-
.problem-link {
62-
display: inline-flex;
63-
align-items: center;
64-
gap: 8px;
65-
background: rgba(59, 130, 246, 0.2);
66-
color: #93c5fd;
57+
.header-link {
58+
background: rgba(255, 255, 255, 0.12);
59+
backdrop-filter: blur(10px);
60+
color: white;
6761
text-decoration: none;
68-
padding: 12px 24px;
69-
border-radius: 12px;
62+
border: 1px solid rgba(255, 255, 255, 0.2);
63+
padding: 10px 20px;
64+
border-radius: 14px;
65+
font-size: 14px;
7066
font-weight: 600;
71-
font-size: 1.1em;
7267
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
73-
border: 1px solid rgba(59, 130, 246, 0.3);
74-
backdrop-filter: blur(10px);
68+
display: inline-flex;
69+
align-items: center;
70+
gap: 8px;
7571
}
7672

77-
.problem-link:hover {
78-
background: rgba(59, 130, 246, 0.3);
79-
color: #dbeafe;
73+
.header-link:hover {
74+
background: rgba(255, 255, 255, 0.2);
8075
transform: translateY(-2px);
81-
box-shadow: 0 8px 25px rgba(59, 130, 246, 0.4);
76+
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
77+
text-decoration: none;
78+
color: white;
79+
}
80+
81+
.header-link:active {
82+
transform: translateY(0);
83+
}
84+
85+
.description {
86+
font-size: 1.2em;
87+
opacity: 0.9;
88+
max-width: 600px;
89+
margin: 20px auto 0;
90+
line-height: 1.6;
8291
}
8392

8493
.controls {
@@ -306,6 +315,16 @@
306315
button {
307316
width: 200px;
308317
}
318+
319+
.header-links {
320+
flex-direction: column;
321+
align-items: center;
322+
}
323+
324+
.header-link {
325+
width: 200px;
326+
justify-content: center;
327+
}
309328
}
310329

311330
@keyframes fadeIn {
@@ -323,12 +342,7 @@
323342
<div class="container">
324343
<header>
325344
<h1>🎯 Replace Non-Coprime Numbers in Array</h1>
326-
327-
<div class="problem-link-container">
328-
<a href="https://leetcode.com/problems/replace-non-coprime-numbers-in-array/description/" target="_blank" class="problem-link">
329-
🔗 View Original Problem
330-
</a>
331-
</div>
345+
<div class="header-links"><a href="https://leetcode.com/problems/replace-non-coprime-numbers-in-array/description/" target="_blank" class="header-link">📝 View Problem</a><a href="https://github.com/Vic-Nas/PythonSolutions/blob/main/leetcode/nonCoprimes/nonCoprimes.vn.py" target="_blank" class="header-link">💻 View Code</a></div>
332346

333347
</header>
334348

leetcode/nonCoprimes/nonCoprimes_vis.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ def ppcm(a, b): return a * b // pgcd(a, b)
4848
class App(AlgorithmVisualizer):
4949
def __init__(self, title = "Replace Non-Coprime Numbers in Array",
5050
desc = "",
51-
problem_link = "https://leetcode.com/problems/replace-non-coprime-numbers-in-array/description/",
51+
problem_url = "https://leetcode.com/problems/replace-non-coprime-numbers-in-array/description/",
52+
code_url = "https://github.com/Vic-Nas/PythonSolutions/blob/main/leetcode/nonCoprimes/nonCoprimes.vn.py",
5253
nums = []):
5354
self.res = DoubleLinkedList(nums)
54-
super().__init__(title, problem_link = problem_link, description = desc)
55+
super().__init__(title, problem_url = problem_url, description = desc, code_url = code_url)
5556
def run_algorithm(self):
5657
res = self.res
5758
now = res.head

utils/visualize.py

Lines changed: 86 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ class AlgorithmVisualizer(ABC):
2222
class BinarySearch(AlgorithmVisualizer):
2323
def __init__(self, arr, target):
2424
super().__init__(
25-
"Binary Search",
26-
problem_link="https://leetcode.com/problems/binary-search/"
25+
title="Binary Search",
26+
problem_url="https://leetcode.com/problems/binary-search/",
27+
code_url="https://github.com/user/repo/blob/main/binary_search.py"
2728
)
2829
self.arr = arr
2930
self.target = target
@@ -65,18 +66,20 @@ def get_complexity(self):
6566
BinarySearch([1,3,5,7,9], 5).show()
6667
"""
6768

68-
def __init__(self, title: str, description: str = "", problem_link: str = None):
69+
def __init__(self, title: str, description: str = "", problem_url: str = "", code_url: str = ""):
6970
"""
7071
Initialize the visualizer.
7172
7273
Args:
7374
title: Name of your algorithm (e.g., "Binary Search")
74-
description: Brief explanation (e.g., "Finding target in sorted array")
75-
problem_link: URL to the problem (e.g., LeetCode link)
75+
description: Brief explanation (optional, will be shown as subtitle)
76+
problem_url: Link to the problem (e.g., LeetCode URL)
77+
code_url: Link to your code implementation (e.g., GitHub URL)
7678
"""
7779
self.title = title
7880
self.description = description
79-
self.problem_link = problem_link
81+
self.problem_url = problem_url
82+
self.code_url = code_url
8083
self.steps = []
8184

8285
@abstractmethod
@@ -282,21 +285,6 @@ def _render_primitive(self, name: str, value: Any) -> str:
282285
"""Render primitive values (int, float, string, etc.)."""
283286
return f'<div class="variable primitive"><strong>{name}:</strong> <span class="value">{value}</span></div>'
284287

285-
def _generate_problem_link_html(self) -> str:
286-
"""Generate HTML for the problem link section."""
287-
if self.problem_link:
288-
return f'''
289-
<div class="problem-link-container">
290-
<a href="{self.problem_link}" target="_blank" class="problem-link">
291-
🔗 View Original Problem
292-
</a>
293-
</div>
294-
'''
295-
elif self.description:
296-
return f'<p class="description">{self.description}</p>'
297-
else:
298-
return ''
299-
300288
def _generate_html(self) -> str:
301289
"""Generate the complete HTML visualization."""
302290
complexity_info = self.get_complexity()
@@ -316,6 +304,22 @@ def _generate_html(self) -> str:
316304
'step_number': step['step_number']
317305
})
318306

307+
# Generate header with links
308+
header_html = f'<h1>🎯 {self.title}</h1>'
309+
310+
links_html = ""
311+
if self.problem_url or self.code_url:
312+
links = []
313+
if self.problem_url:
314+
links.append(f'<a href="{self.problem_url}" target="_blank" class="header-link">📝 View Problem</a>')
315+
if self.code_url:
316+
links.append(f'<a href="{self.code_url}" target="_blank" class="header-link">💻 View Code</a>')
317+
links_html = f'<div class="header-links">{"".join(links)}</div>'
318+
319+
description_html = ""
320+
if self.description:
321+
description_html = f'<p class="description">{self.description}</p>'
322+
319323
return f'''<!DOCTYPE html>
320324
<html lang="en">
321325
<head>
@@ -329,8 +333,9 @@ def _generate_html(self) -> str:
329333
<body>
330334
<div class="container">
331335
<header>
332-
<h1>🎯 {self.title}</h1>
333-
{self._generate_problem_link_html()}
336+
{header_html}
337+
{links_html}
338+
{description_html}
334339
</header>
335340
336341
<div class="controls">
@@ -483,39 +488,48 @@ def _get_css(self) -> str:
483488
background-clip: text;
484489
}
485490
486-
.description {
487-
font-size: 1.2em;
488-
opacity: 0.9;
489-
max-width: 600px;
490-
margin: 0 auto;
491-
line-height: 1.6;
492-
}
493-
494-
.problem-link-container {
495-
margin-top: 16px;
491+
.header-links {
492+
display: flex;
493+
justify-content: center;
494+
gap: 16px;
495+
margin: 20px 0;
496+
flex-wrap: wrap;
496497
}
497498
498-
.problem-link {
499-
display: inline-flex;
500-
align-items: center;
501-
gap: 8px;
502-
background: rgba(59, 130, 246, 0.2);
503-
color: #93c5fd;
499+
.header-link {
500+
background: rgba(255, 255, 255, 0.12);
501+
backdrop-filter: blur(10px);
502+
color: white;
504503
text-decoration: none;
505-
padding: 12px 24px;
506-
border-radius: 12px;
504+
border: 1px solid rgba(255, 255, 255, 0.2);
505+
padding: 10px 20px;
506+
border-radius: 14px;
507+
font-size: 14px;
507508
font-weight: 600;
508-
font-size: 1.1em;
509509
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
510-
border: 1px solid rgba(59, 130, 246, 0.3);
511-
backdrop-filter: blur(10px);
510+
display: inline-flex;
511+
align-items: center;
512+
gap: 8px;
512513
}
513514
514-
.problem-link:hover {
515-
background: rgba(59, 130, 246, 0.3);
516-
color: #dbeafe;
515+
.header-link:hover {
516+
background: rgba(255, 255, 255, 0.2);
517517
transform: translateY(-2px);
518-
box-shadow: 0 8px 25px rgba(59, 130, 246, 0.4);
518+
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
519+
text-decoration: none;
520+
color: white;
521+
}
522+
523+
.header-link:active {
524+
transform: translateY(0);
525+
}
526+
527+
.description {
528+
font-size: 1.2em;
529+
opacity: 0.9;
530+
max-width: 600px;
531+
margin: 20px auto 0;
532+
line-height: 1.6;
519533
}
520534
521535
.controls {
@@ -743,6 +757,16 @@ def _get_css(self) -> str:
743757
button {
744758
width: 200px;
745759
}
760+
761+
.header-links {
762+
flex-direction: column;
763+
align-items: center;
764+
}
765+
766+
.header-link {
767+
width: 200px;
768+
justify-content: center;
769+
}
746770
}
747771
748772
@keyframes fadeIn {
@@ -756,14 +780,16 @@ def _get_css(self) -> str:
756780
'''
757781

758782

759-
# Example implementations with clickable links
783+
# Updated example implementations
760784
class BinarySearchExample(AlgorithmVisualizer):
761-
"""Example: Binary Search visualization with LeetCode link"""
785+
"""Example: Binary Search visualization"""
762786

763787
def __init__(self, arr: List[int], target: int):
764788
super().__init__(
765-
"Binary Search",
766-
problem_link="https://leetcode.com/problems/binary-search/"
789+
title="Binary Search",
790+
description="Efficiently searching in a sorted array",
791+
problem_url="https://leetcode.com/problems/binary-search/",
792+
code_url="https://github.com/example/algorithms/blob/main/binary_search.py"
767793
)
768794
self.arr = arr
769795
self.target = target
@@ -819,57 +845,12 @@ def get_complexity(self) -> Dict[str, str]:
819845
}
820846

821847

822-
class TwoSumExample(AlgorithmVisualizer):
823-
"""Example: Two Sum with hash map approach and LeetCode link"""
824-
825-
def __init__(self, nums: List[int], target: int):
826-
super().__init__(
827-
"Two Sum",
828-
problem_link="https://leetcode.com/problems/two-sum/"
829-
)
830-
self.nums = nums
831-
self.target = target
848+
if __name__ == "__main__":
849+
print("🎨 Enhanced Algorithm Visualizer with Links")
850+
print("=" * 50)
851+
print("\n✨ Quick Examples:")
852+
print("\n📚 Check the docstrings for full usage guide!")
832853

833-
def run_algorithm(self) -> List[int]:
834-
seen = {}
835-
836-
self.add_step(
837-
{'nums': self.nums, 'target': self.target, 'seen': seen},
838-
"🚀 Starting two sum search with hash map"
839-
)
840-
841-
for i, num in enumerate(self.nums):
842-
complement = self.target - num
843-
844-
self.add_step(
845-
{'nums': self.nums, 'i': i, 'target': self.target, 'seen': seen, 'complement': complement},
846-
f"At index {i}: need {complement} to sum to {self.target}",
847-
current=[i]
848-
)
849-
850-
if complement in seen:
851-
self.add_step(
852-
{'nums': self.nums, 'i': i, 'target': self.target, 'seen': seen},
853-
f"🎯 Found pair! {num} + {complement} = {self.target}",
854-
highlight=[seen[complement], i]
855-
)
856-
return [seen[complement], i]
857-
858-
seen[num] = i
859-
self.add_step(
860-
{'nums': self.nums, 'i': i, 'target': self.target, 'seen': seen},
861-
f"Added {num} -> index {i} to hash map"
862-
)
863-
864-
self.add_step(
865-
{'nums': self.nums, 'target': self.target, 'seen': seen},
866-
"❌ No valid pair found"
867-
)
868-
return []
869-
870-
def get_complexity(self) -> Dict[str, str]:
871-
return {
872-
"time": "O(n)",
873-
"space": "O(n)",
874-
"explanation": "Single pass with hash map lookup"
875-
}
854+
# Example usage
855+
# visualizer = BinarySearchExample([1, 3, 5, 7, 9, 11, 13], 7)
856+
# visualizer.show()

0 commit comments

Comments
 (0)