-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
1343 lines (1089 loc) · 44.4 KB
/
app.py
File metadata and controls
1343 lines (1089 loc) · 44.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import logging
import time
import hashlib
import ipaddress
import requests
from collections import defaultdict, deque
from flask import Flask, render_template, request, jsonify, redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)
# Rate limiting for geo-blocking to prevent spam
class RateLimiter:
def __init__(self):
self.requests = defaultdict(deque)
self.redirected_ips = defaultdict(float)
def is_rate_limited(self, ip, max_requests=5, window_minutes=60):
"""Check if IP is rate limited for geo-blocking"""
now = time.time()
window_seconds = window_minutes * 60
# Clean old entries
while self.requests[ip] and self.requests[ip][0] < now - window_seconds:
self.requests[ip].popleft()
# Check if already redirected recently (24 hour cooldown)
if ip in self.redirected_ips and now - self.redirected_ips[ip] < 86400: # 24 hours
return True
# Check rate limit
if len(self.requests[ip]) >= max_requests:
return True
# Add current request
self.requests[ip].append(now)
return False
def mark_redirected(self, ip):
"""Mark an IP as redirected to prevent repeated redirects"""
self.redirected_ips[ip] = time.time()
# Global rate limiter instance
rate_limiter = RateLimiter()
# Initialize Flask app
app = Flask(__name__)
# Configuration
app.secret_key = os.environ.get("SESSION_SECRET", "dev-secret-key")
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URL")
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
"pool_recycle": 300,
"pool_pre_ping": True,
"pool_timeout": 20,
"pool_size": 10,
"max_overflow": 20,
"connect_args": {
"connect_timeout": 10,
"application_name": "sliding_window_app"
}
}
# Initialize database
db.init_app(app)
# Create all tables
with app.app_context():
# Import models here to ensure they are registered
from models import Review
db.create_all()
def add_security_headers(response):
"""Add security headers to all responses"""
response.headers['X-Content-Type-Options'] = 'nosniff'
response.headers['X-Frame-Options'] = 'DENY'
response.headers['X-XSS-Protection'] = '1; mode=block'
response.headers['Referrer-Policy'] = 'strict-origin-when-cross-origin'
return response
def get_client_ip():
"""Get the client's real IP address"""
# Check for IP from reverse proxy headers
forwarded_for = request.headers.get('X-Forwarded-For')
if forwarded_for and isinstance(forwarded_for, str):
return forwarded_for.split(',')[0].strip()
real_ip = request.headers.get('X-Real-IP')
if real_ip and isinstance(real_ip, str):
return real_ip
return request.remote_addr or '127.0.0.1'
def is_russian_ip(ip_address):
"""Check if IP address is from Russian IP ranges using known allocations"""
try:
ip = ipaddress.ip_address(ip_address)
# Major Russian IP ranges (CIDR blocks allocated to Russia)
russian_ranges = [
'5.8.0.0/13', # Rostelecom
'5.16.0.0/13', # Rostelecom
'31.173.0.0/16', # MTS
'37.139.0.0/16', # Beeline
'46.17.0.0/16', # Corbina Telecom
'46.32.0.0/19', # Petersburg Internet Network
'77.88.0.0/18', # Yandex
'78.25.0.0/16', # Rostelecom
'78.108.0.0/16', # TTK
'81.176.0.0/13', # Rostelecom
'82.138.0.0/16', # Corbina Telecom
'85.21.0.0/16', # Rostelecom
'87.226.0.0/16', # TTK
'89.175.0.0/16', # Petersburg Internet Network
'91.103.0.0/16', # Petersburg Internet Network
'91.185.0.0/16', # MTS
'91.186.0.0/16', # MTS
'94.19.0.0/16', # Petersburg Internet Network
'95.24.0.0/16', # Petersburg Internet Network
'95.162.0.0/16', # Petersburg Internet Network
'176.59.0.0/16', # Rostelecom
'178.20.0.0/14', # Rostelecom
'178.176.0.0/12', # Rostelecom
'185.4.0.0/16', # Selectel
'188.113.0.0/16', # Rostelecom
'188.170.0.0/16', # Rostelecom
'212.1.0.0/16', # Rostelecom
'213.59.0.0/16' # Rostelecom
]
for cidr in russian_ranges:
if ip in ipaddress.ip_network(cidr):
return True
except (ValueError, ipaddress.AddressValueError):
pass
return False
def check_geo_blocking():
"""Check if the request is from Russia and redirect if necessary"""
try:
client_ip = get_client_ip()
# Validate IP address exists and is a string
if not client_ip or not isinstance(client_ip, str):
return None
# Skip geo-blocking for localhost/development
if client_ip in ['127.0.0.1', 'localhost', '::1'] or client_ip.startswith('192.168.'):
return None
# Skip for API endpoints to prevent context cancellation
if request.path.startswith('/api/'):
return None
# Check rate limiting to prevent spam
if rate_limiter.is_rate_limited(client_ip):
# If rate limited, serve normal content instead of redirecting
return None
# Check if IP is from Russian ranges
if is_russian_ip(client_ip):
# Mark IP as redirected to prevent repeated redirects
rate_limiter.mark_redirected(client_ip)
# Log the redirect with rate limiting info
logging.info(f"Redirecting Russian IP {client_ip} to Ukraine support (rate limited for 24h)")
# Add cache headers to prevent excessive requests
response = redirect('https://linktr.ee/UkraineTheLatest', code=301) # Permanent redirect
response.headers['Cache-Control'] = 'public, max-age=86400' # 24 hour cache
response.headers['Connection'] = 'close' # Close connection cleanly
return response
except Exception as e:
logging.warning(f"Geo-blocking error: {e}")
return None
# Set up logging - use INFO level for production
log_level = logging.DEBUG if os.environ.get('FLASK_ENV') == 'development' else logging.INFO
logging.basicConfig(level=log_level)
# Apply security headers to all responses
app.after_request(add_security_headers)
# Apply geo-blocking check to all requests
@app.before_request
def before_request():
"""Check geo-blocking before processing any request"""
redirect_response = check_geo_blocking()
if redirect_response:
return redirect_response
@app.route('/')
def index():
"""Main page with sliding window visualization"""
return render_template('index.html')
@app.route('/api/validate_input', methods=['POST'])
def validate_input():
"""Validate user input for array/string"""
try:
data = request.get_json()
input_text = data.get('input', '').strip()
input_type = data.get('type', 'array')
if not input_text:
return jsonify({'valid': False, 'error': 'Input cannot be empty'})
if input_type == 'array':
# Try to parse as comma-separated numbers
try:
elements = [int(x.strip()) for x in input_text.split(',') if x.strip()]
if len(elements) == 0:
return jsonify({'valid': False, 'error': 'Please enter at least one number'})
return jsonify({'valid': True, 'parsed': elements})
except ValueError:
return jsonify({'valid': False, 'error': 'Please enter valid numbers separated by commas'})
elif input_type == 'string':
# String input - just validate it's not empty
if len(input_text) == 0:
return jsonify({'valid': False, 'error': 'String cannot be empty'})
return jsonify({'valid': True, 'parsed': list(input_text)})
return jsonify({'valid': False, 'error': 'Invalid input type'})
except Exception as e:
app.logger.error(f"Error validating input: {str(e)}")
return jsonify({'valid': False, 'error': 'An error occurred while validating input'})
@app.route('/api/calculate_step', methods=['POST'])
def calculate_step():
"""Calculate the result for a specific window position"""
try:
data = request.get_json()
elements = data.get('elements', [])
window_start = int(data.get('window_start', 0))
window_size = int(data.get('window_size', 1))
algorithm = data.get('algorithm', 'sum')
# Validate inputs
if not elements or window_start < 0 or window_size <= 0:
return jsonify({'error': 'Invalid parameters'})
if window_start + window_size > len(elements):
return jsonify({'error': 'Window extends beyond array bounds'})
# Get current window
window = elements[window_start:window_start + window_size]
# Calculate result based on algorithm
if algorithm == 'sum':
result = sum(window)
description = f"Sum of window: {' + '.join(map(str, window))} = {result}"
elif algorithm == 'max':
result = max(window)
description = f"Maximum in window: max({', '.join(map(str, window))}) = {result}"
elif algorithm == 'min':
result = min(window)
description = f"Minimum in window: min({', '.join(map(str, window))}) = {result}"
elif algorithm == 'avg':
result = sum(window) / len(window)
description = f"Average of window: ({' + '.join(map(str, window))}) / {len(window)} = {result:.2f}"
elif algorithm == 'longest_substring':
# For longest substring, we need to track the current window and check for duplicates
window_str = ''.join(str(x) for x in window)
unique_chars = set(window)
if len(unique_chars) == len(window):
result = len(window)
description = f"Window '{window_str}' has no repeating characters. Length: {result}"
else:
result = 0
duplicates = []
seen = set()
for char in window:
if char in seen:
duplicates.append(char)
seen.add(char)
description = f"Window '{window_str}' has repeating characters: {', '.join(set(duplicates))}"
elif algorithm == 'permutation_in_string':
# For permutation in string, check if current window is a permutation of the pattern
window_str = ''.join(str(x) for x in window)
pattern = data.get('pattern', '')
if len(window_str) == len(pattern):
# Check if window is a permutation of pattern
from collections import Counter
window_count = Counter(window_str)
pattern_count = Counter(pattern)
if window_count == pattern_count:
result = 1 # Found permutation
description = f"Window '{window_str}' is a permutation of pattern '{pattern}' ✓"
else:
result = 0 # Not a permutation
description = f"Window '{window_str}' is not a permutation of pattern '{pattern}'"
else:
result = 0
description = f"Window '{window_str}' (length {len(window_str)}) doesn't match pattern length {len(pattern)}"
else:
return jsonify({'error': 'Unknown algorithm'})
return jsonify({
'result': result,
'window': window,
'description': description
})
except Exception as e:
app.logger.error(f"Error calculating step: {str(e)}")
return jsonify({'error': 'An error occurred while calculating'})
@app.route('/api/generate_code', methods=['POST'])
def generate_code():
"""Generate algorithm code based on current configuration"""
try:
data = request.get_json()
algorithm = data.get('algorithm', 'sum')
window_type = data.get('window_type', 'fixed')
window_size = data.get('window_size', 3)
language = data.get('language', 'python')
# Helper function to format window size in code
def get_java_code(template_name, k=window_size):
java_templates = {
'fixed_sum': f"""import java.util.*;
public class SlidingWindowSum {{
public static int maxSumSubarray(int[] arr, int k) {{
if (arr.length < k) {{
return -1;
}}
// Calculate sum of first window
int windowSum = 0;
for (int i = 0; i < k; i++) {{
windowSum += arr[i];
}}
int maxSum = windowSum;
// Slide the window
for (int i = k; i < arr.length; i++) {{
// Remove first element of previous window and add current element
windowSum = windowSum - arr[i - k] + arr[i];
maxSum = Math.max(maxSum, windowSum);
}}
return maxSum;
}}
public static void main(String[] args) {{
int[] arr = {{1, 2, 3, 4, 5, 6, 7, 8}};
int result = maxSumSubarray(arr, {k});
System.out.println("Maximum sum of subarray of size {k}: " + result);
}}
}}""",
'fixed_max': f"""import java.util.*;
public class SlidingWindowMaximum {{
public static List<Integer> maxInWindows(int[] arr, int k) {{
if (arr.length < k) {{
return new ArrayList<>();
}}
Deque<Integer> deque = new ArrayDeque<>();
List<Integer> result = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {{
// Remove elements outside current window
while (!deque.isEmpty() && deque.peekFirst() <= i - k) {{
deque.pollFirst();
}}
// Remove smaller elements from rear
while (!deque.isEmpty() && arr[deque.peekLast()] <= arr[i]) {{
deque.pollLast();
}}
deque.offerLast(i);
// Add maximum of current window to result
if (i >= k - 1) {{
result.add(arr[deque.peekFirst()]);
}}
}}
return result;
}}
public static void main(String[] args) {{
int[] arr = {{1, 2, 3, 4, 5, 6, 7, 8}};
List<Integer> result = maxInWindows(arr, {k});
System.out.println("Maximum in each window of size {k}: " + result);
}}
}}""",
'fixed_min': f"""import java.util.*;
public class SlidingWindowMinimum {{
public static List<Integer> minInWindows(int[] arr, int k) {{
if (arr.length < k) {{
return new ArrayList<>();
}}
Deque<Integer> deque = new ArrayDeque<>();
List<Integer> result = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {{
// Remove elements outside current window
while (!deque.isEmpty() && deque.peekFirst() <= i - k) {{
deque.pollFirst();
}}
// Remove larger elements from rear
while (!deque.isEmpty() && arr[deque.peekLast()] >= arr[i]) {{
deque.pollLast();
}}
deque.offerLast(i);
// Add minimum of current window to result
if (i >= k - 1) {{
result.add(arr[deque.peekFirst()]);
}}
}}
return result;
}}
public static void main(String[] args) {{
int[] arr = {{1, 2, 3, 4, 5, 6, 7, 8}};
List<Integer> result = minInWindows(arr, {k});
System.out.println("Minimum in each window of size {k}: " + result);
}}
}}""",
'fixed_avg': f"""import java.util.*;
public class SlidingWindowAverage {{
public static List<Double> averageInWindows(int[] arr, int k) {{
if (arr.length < k) {{
return new ArrayList<>();
}}
List<Double> result = new ArrayList<>();
double windowSum = 0;
// Calculate sum of first window
for (int i = 0; i < k; i++) {{
windowSum += arr[i];
}}
result.add(windowSum / k);
// Slide the window
for (int i = k; i < arr.length; i++) {{
windowSum = windowSum - arr[i - k] + arr[i];
result.add(windowSum / k);
}}
return result;
}}
public static void main(String[] args) {{
int[] arr = {{1, 2, 3, 4, 5, 6, 7, 8}};
List<Double> result = averageInWindows(arr, {k});
System.out.println("Average of each window of size {k}: " + result);
}}
}}""",
'variable_longest_substring': """import java.util.*;
public class LongestSubstring {
public static int lengthOfLongestSubstring(String s) {
Map<Character, Integer> charMap = new HashMap<>();
int left = 0;
int maxLength = 0;
for (int right = 0; right < s.length(); right++) {
char currentChar = s.charAt(right);
if (charMap.containsKey(currentChar) && charMap.get(currentChar) >= left) {
left = charMap.get(currentChar) + 1;
}
charMap.put(currentChar, right);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
public static void main(String[] args) {
String s = "abcabcbb";
int result = lengthOfLongestSubstring(s);
System.out.println("Length of longest substring without repeating characters: " + result);
}
}""",
'variable_permutation_in_string': """import java.util.*;
public class PermutationInString {
public static boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) {
return false;
}
Map<Character, Integer> s1Count = new HashMap<>();
for (char c : s1.toCharArray()) {
s1Count.put(c, s1Count.getOrDefault(c, 0) + 1);
}
int windowSize = s1.length();
for (int i = 0; i <= s2.length() - windowSize; i++) {
String window = s2.substring(i, i + windowSize);
Map<Character, Integer> windowCount = new HashMap<>();
for (char c : window.toCharArray()) {
windowCount.put(c, windowCount.getOrDefault(c, 0) + 1);
}
if (s1Count.equals(windowCount)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String s1 = "ab";
String s2 = "eidbaooo";
boolean result = checkInclusion(s1, s2);
System.out.println("Permutation of '" + s1 + "' exists in '" + s2 + "': " + result);
}
}"""
}
return java_templates.get(template_name, "")
# Generate code based on language and algorithm
if language == 'java':
if window_type == 'fixed':
if algorithm == 'sum':
code = get_java_code('fixed_sum')
elif algorithm == 'max':
code = get_java_code('fixed_max')
elif algorithm == 'min':
code = get_java_code('fixed_min')
elif algorithm == 'avg':
code = get_java_code('fixed_avg')
else:
code = get_java_code('fixed_sum') # Default
else: # variable window
if algorithm == 'longest_substring':
code = get_java_code('variable_longest_substring')
elif algorithm == 'permutation_in_string':
code = get_java_code('variable_permutation_in_string')
else:
code = get_java_code('variable_longest_substring') # Default
elif language == 'python':
code_templates = {
'fixed': {
'sum': f'''def sliding_window_sum(arr, k={window_size}):
"""Find maximum sum of subarray of size k"""
if len(arr) < k:
return None
# Calculate sum of first window
window_sum = sum(arr[:k])
max_sum = window_sum
# Slide the window
for i in range(k, len(arr)):
# Remove first element of previous window and add current element
window_sum = window_sum - arr[i - k] + arr[i]
max_sum = max(max_sum, window_sum)
return max_sum
# Example usage:
arr = [1, 2, 3, 4, 5, 6, 7, 8]
result = sliding_window_sum(arr, {window_size})
print(f"Maximum sum of subarray of size {window_size}: {{result}}")''',
'max': f'''def sliding_window_maximum(arr, k={window_size}):
"""Find maximum in each window of size k"""
if len(arr) < k:
return []
from collections import deque
dq = deque()
result = []
for i in range(len(arr)):
# Remove elements outside current window
while dq and dq[0] <= i - k:
dq.popleft()
# Remove smaller elements from rear
while dq and arr[dq[-1]] <= arr[i]:
dq.pop()
dq.append(i)
# Add maximum of current window to result
if i >= k - 1:
result.append(arr[dq[0]])
return result
# Example usage:
arr = [1, 2, 3, 4, 5, 6, 7, 8]
result = sliding_window_maximum(arr, {window_size})
print(f"Maximum in each window of size {window_size}: {{result}}")''',
'min': f'''def sliding_window_minimum(arr, k={window_size}):
"""Find minimum in each window of size k"""
if len(arr) < k:
return []
from collections import deque
dq = deque()
result = []
for i in range(len(arr)):
# Remove elements outside current window
while dq and dq[0] <= i - k:
dq.popleft()
# Remove larger elements from rear
while dq and arr[dq[-1]] >= arr[i]:
dq.pop()
dq.append(i)
# Add minimum of current window to result
if i >= k - 1:
result.append(arr[dq[0]])
return result
# Example usage:
arr = [1, 2, 3, 4, 5, 6, 7, 8]
result = sliding_window_minimum(arr, {window_size})
print(f"Minimum in each window of size {window_size}: {{result}}")''',
'avg': f'''def sliding_window_average(arr, k={window_size}):
"""Find average of each window of size k"""
if len(arr) < k:
return []
result = []
window_sum = sum(arr[:k])
result.append(window_sum / k)
for i in range(k, len(arr)):
window_sum = window_sum - arr[i - k] + arr[i]
result.append(window_sum / k)
return result
# Example usage:
arr = [1, 2, 3, 4, 5, 6, 7, 8]
result = sliding_window_average(arr, {window_size})
print(f"Average of each window of size {window_size}: {{result}}")'''
},
'variable': {
'longest_substring': '''def longest_substring_without_repeating(s):
"""Find length of longest substring without repeating characters"""
char_map = {}
left = 0
max_length = 0
for right in range(len(s)):
if s[right] in char_map and char_map[s[right]] >= left:
left = char_map[s[right]] + 1
char_map[s[right]] = right
max_length = max(max_length, right - left + 1)
return max_length
# Example usage:
s = "abcabcbb"
result = longest_substring_without_repeating(s)
print(f"Length of longest substring without repeating characters: {result}")''',
'permutation_in_string': '''def check_inclusion(s1, s2):
"""Check if any permutation of s1 exists in s2"""
if len(s1) > len(s2):
return False
from collections import Counter
s1_count = Counter(s1)
window_size = len(s1)
for i in range(len(s2) - window_size + 1):
window = s2[i:i + window_size]
window_count = Counter(window)
if s1_count == window_count:
return True
return False
# Example usage:
s1 = "ab"
s2 = "eidbaooo"
result = check_inclusion(s1, s2)
print(f"Permutation of '{s1}' exists in '{s2}': {result}")'''
}
}
# Handle variable window algorithms requested as fixed window
if algorithm in ['longest_substring', 'permutation_in_string'] and window_type == 'fixed':
code = code_templates.get('variable', {}).get(algorithm, "# Python implementation not available for this combination")
else:
code = code_templates.get(window_type, {}).get(algorithm, f'''# Algorithm '{algorithm}' not implemented in this demo
# Available algorithms: sum, max, min, avg, longest_substring, permutation_in_string
# This visualizer supports the most common sliding window patterns
# For custom algorithms, adapt the pattern from the implemented examples
def sliding_window_template(arr, k=3):
"""Template for implementing sliding window algorithms"""
if len(arr) < k:
return None
# Initialize window
# Process each window position
# Return result
return "Implement your algorithm logic here"
# Example usage:
arr = [1, 2, 3, 4, 5, 6, 7, 8]
result = sliding_window_template(arr, 3)
print(f"Result: {{result}}")''')
elif language == 'java':
if window_type == 'fixed':
# Handle variable window algorithms requested as fixed window first
if algorithm == 'longest_substring':
code = '''import java.util.*;
public class LongestSubstring {
public static int lengthOfLongestSubstring(String s) {
Map<Character, Integer> charMap = new HashMap<>();
int left = 0;
int maxLength = 0;
for (int right = 0; right < s.length(); right++) {
if (charMap.containsKey(s.charAt(right)) && charMap.get(s.charAt(right)) >= left) {
left = charMap.get(s.charAt(right)) + 1;
}
charMap.put(s.charAt(right), right);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
public static void main(String[] args) {
String s = "abcabcbb";
int result = lengthOfLongestSubstring(s);
System.out.println("Length of longest substring without repeating characters: " + result);
}
}'''
elif algorithm == 'permutation_in_string':
code = '''import java.util.*;
public class PermutationInString {
public static boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) {
return false;
}
Map<Character, Integer> s1Count = new HashMap<>();
for (char c : s1.toCharArray()) {
s1Count.put(c, s1Count.getOrDefault(c, 0) + 1);
}
int windowSize = s1.length();
for (int i = 0; i <= s2.length() - windowSize; i++) {
String window = s2.substring(i, i + windowSize);
Map<Character, Integer> windowCount = new HashMap<>();
for (char c : window.toCharArray()) {
windowCount.put(c, windowCount.getOrDefault(c, 0) + 1);
}
if (s1Count.equals(windowCount)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String s1 = "ab";
String s2 = "eidbaooo";
boolean result = checkInclusion(s1, s2);
System.out.println("Permutation of '" + s1 + "' exists in '" + s2 + "': " + result);
}
}'''
elif algorithm == 'sum':
code = f'''import java.util.*;
public class SlidingWindowSum {{
public static int slidingWindowSum(int[] arr, int k) {{
if (arr.length < k) {{
return -1;
}}
// Calculate sum of first window
int windowSum = 0;
for (int i = 0; i < k; i++) {{
windowSum += arr[i];
}}
int maxSum = windowSum;
// Slide the window
for (int i = k; i < arr.length; i++) {{
windowSum = windowSum - arr[i - k] + arr[i];
maxSum = Math.max(maxSum, windowSum);
}}
return maxSum;
}}
public static void main(String[] args) {{
int[] arr = {{1, 2, 3, 4, 5, 6, 7, 8}};
int result = slidingWindowSum(arr, {window_size});
System.out.println("Maximum sum of subarray of size {window_size}: " + result);
}}
}}'''
else:
code = "// Java implementation for other algorithms not shown for brevity"
else: # variable window
if algorithm == 'longest_substring':
code = '''import java.util.*;
public class LongestSubstring {
public static int lengthOfLongestSubstring(String s) {
Map<Character, Integer> charMap = new HashMap<>();
int left = 0;
int maxLength = 0;
for (int right = 0; right < s.length(); right++) {
if (charMap.containsKey(s.charAt(right)) && charMap.get(s.charAt(right)) >= left) {
left = charMap.get(s.charAt(right)) + 1;
}
charMap.put(s.charAt(right), right);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
public static void main(String[] args) {
String s = "abcabcbb";
int result = lengthOfLongestSubstring(s);
System.out.println("Length of longest substring without repeating characters: " + result);
}
}'''
elif algorithm == 'permutation_in_string':
code = '''import java.util.*;
public class PermutationInString {
public static boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) {
return false;
}
Map<Character, Integer> s1Count = new HashMap<>();
for (char c : s1.toCharArray()) {
s1Count.put(c, s1Count.getOrDefault(c, 0) + 1);
}
int windowSize = s1.length();
for (int i = 0; i <= s2.length() - windowSize; i++) {
String window = s2.substring(i, i + windowSize);
Map<Character, Integer> windowCount = new HashMap<>();
for (char c : window.toCharArray()) {
windowCount.put(c, windowCount.getOrDefault(c, 0) + 1);
}
if (s1Count.equals(windowCount)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String s1 = "ab";
String s2 = "eidbaooo";
boolean result = checkInclusion(s1, s2);
System.out.println("Permutation of '" + s1 + "' exists in '" + s2 + "': " + result);
}
}'''
else:
code = "// Java implementation for other variable window algorithms not shown for brevity"
elif language == 'javascript':
if window_type == 'fixed':
if algorithm == 'sum':
code = f'''function slidingWindowSum(arr, k = {window_size}) {{
if (arr.length < k) {{
return null;
}}
// Calculate sum of first window
let windowSum = 0;
for (let i = 0; i < k; i++) {{
windowSum += arr[i];
}}
let maxSum = windowSum;
// Slide the window
for (let i = k; i < arr.length; i++) {{
windowSum = windowSum - arr[i - k] + arr[i];
maxSum = Math.max(maxSum, windowSum);
}}
return maxSum;
}}
// Example usage:
const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const result = slidingWindowSum(arr, {window_size});
console.log(`Maximum sum of subarray of size {window_size}: ${{result}}`);'''
elif algorithm == 'max':
code = f'''function slidingWindowMaximum(arr, k = {window_size}) {{
if (arr.length < k) {{
return [];
}}
const deque = [];
const result = [];
for (let i = 0; i < arr.length; i++) {{
// Remove elements outside current window
while (deque.length > 0 && deque[0] <= i - k) {{
deque.shift();
}}
// Remove smaller elements from rear
while (deque.length > 0 && arr[deque[deque.length - 1]] <= arr[i]) {{
deque.pop();
}}
deque.push(i);
// Add maximum of current window to result
if (i >= k - 1) {{
result.push(arr[deque[0]]);
}}
}}
return result;
}}
// Example usage:
const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const result = slidingWindowMaximum(arr, {window_size});
console.log(`Maximum in each window of size {window_size}: ${{result}}`);'''
elif algorithm == 'min':
code = f'''function slidingWindowMinimum(arr, k = {window_size}) {{
if (arr.length < k) {{
return [];
}}
const deque = [];
const result = [];
for (let i = 0; i < arr.length; i++) {{
// Remove elements outside current window
while (deque.length > 0 && deque[0] <= i - k) {{
deque.shift();
}}
// Remove larger elements from rear
while (deque.length > 0 && arr[deque[deque.length - 1]] >= arr[i]) {{