@@ -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
760784class 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