Skip to content

Commit c2f4e24

Browse files
committed
leetcode-kazakhstan-alga
1 parent 556a30a commit c2f4e24

File tree

14 files changed

+311
-12
lines changed

14 files changed

+311
-12
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEMINI_API_KEY=your_api_key

api/index.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

app/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
from app import routes

app/complexity.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import google.generativeai as genai
2+
import json
3+
import os
4+
from dotenv import load_dotenv
5+
6+
load_dotenv()
7+
8+
with open('prompts.json') as f:
9+
prompts = json.load(f)
10+
11+
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
12+
13+
model_gemini_pro = genai.GenerativeModel('gemini-pro')
14+
15+
def check_is_valid_code(code: str) -> bool:
16+
try:
17+
prompt = prompts['check_is_valid_code'].format(code)
18+
response = model_gemini_pro.generate_content(
19+
prompt,
20+
generation_config = genai.types.GenerationConfig(
21+
max_output_tokens = 100, # limit on the total number of words generated
22+
temperature = 0.5, # creativity level of the model ~ controls the randomness of the generated text
23+
top_p = 0.8, # influences the model to choose the next word based on its probability
24+
top_k = 30, # considers only the top K most probable words at each step
25+
)
26+
)
27+
return 'true' in response.text.lower()
28+
except Exception as e:
29+
return False
30+
31+
def get_complexity_analysis(code: str) -> dict:
32+
try:
33+
if not check_is_valid_code(code):
34+
raise ValueError('Invalid code snippet')
35+
time_complexity_prompt = prompts['get_time_complexity'].format(prompts['complexity_options'], code)
36+
memory_complexity_prompt = prompts['get_memory_complexity'].format(prompts['complexity_options'], code)
37+
time_complexity = model_gemini_pro.generate_content(
38+
time_complexity_prompt,
39+
generation_config = genai.types.GenerationConfig(
40+
max_output_tokens = 100,
41+
temperature = 0.5,
42+
top_p = 0.8,
43+
top_k = 30,
44+
)
45+
).text
46+
memory_complexity = model_gemini_pro.generate_content(
47+
memory_complexity_prompt,
48+
generation_config = genai.types.GenerationConfig(
49+
max_output_tokens = 100,
50+
temperature = 0.5,
51+
top_p = 0.8,
52+
top_k = 30,
53+
)
54+
).text
55+
complexity_description_prompt = prompts['get_description'].format(time_complexity, memory_complexity, code)
56+
complexity_description = model_gemini_pro.generate_content(
57+
complexity_description_prompt,
58+
generation_config = genai.types.GenerationConfig(
59+
temperature = 0.7,
60+
top_p = 0.8,
61+
top_k = 30,
62+
)
63+
).text
64+
return {
65+
'status': 'success',
66+
'time': time_complexity,
67+
'memory': memory_complexity,
68+
'description': complexity_description
69+
}
70+
except Exception as e:
71+
return {
72+
'status': 'error',
73+
'time': None,
74+
'memory': None,
75+
'description': str(e)
76+
}
77+
78+
def main():
79+
code = '''
80+
class Solution:
81+
def maxTotalReward(self, nums: List[int]) -> int:
82+
nums.sort()
83+
ma = nums[-1]
84+
dp = [0] * (ma * 2 + 1)
85+
for v in nums:
86+
for i in range(v):
87+
if dp[i+v] < dp[i] + v:
88+
dp[i+v] = dp[i] + v
89+
return max(dp)
90+
'''
91+
# print(check_is_valid_code(code))
92+
print(get_complexity_analysis(code))
93+
94+
if __name__ == '__main__':
95+
main()

app/routes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from flask import render_template, request
2+
from app.complexity import get_complexity_analysis
3+
from app import app
4+
5+
@app.route('/', methods=['GET', 'POST'])
6+
def index():
7+
complexity = None
8+
9+
if request.method == 'POST':
10+
code = request.form['code']
11+
complexity = get_complexity_analysis(code)
12+
13+
print(complexity)
14+
15+
return render_template('index.html', complexity=complexity)
16+
17+
@app.route('/stars')
18+
def hall_of_fame():
19+
return render_template('stars.html')
154 KB
Loading

app/static/leetcode-logo.png

14.7 KB
Loading

app/static/style.css

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@import url('https://fonts.googleapis.com/css?family=Poppins:400,500,700,800&display=swap');
2+
3+
body {
4+
font-family: 'Poppins', sans-serif;
5+
margin: 0;
6+
padding: 0;
7+
background-color: #f1f1f1;
8+
}
9+
10+
.logo {
11+
width: 150px;
12+
display: block;
13+
margin: 0 auto;
14+
margin-bottom: 25px;
15+
}
16+
17+
.chart {
18+
width: 100%;
19+
max-width: 600px;
20+
margin: 0 auto;
21+
background-color: #fff;
22+
padding: 20px;
23+
border-radius: 5px;
24+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
25+
margin-top: 25px;
26+
margin-bottom: 25px;
27+
}
28+
29+
.loader {
30+
border: 4px solid rgba(0, 0, 0, 0.1);
31+
border-top: 4px solid #3498db;
32+
border-radius: 50%;
33+
width: 30px;
34+
height: 30px;
35+
animation: spin 1s linear infinite;
36+
margin: 0 auto;
37+
margin-top: 20px;
38+
}
39+
40+
@keyframes spin {
41+
0% { transform: rotate(0deg); }
42+
100% { transform: rotate(360deg); }
43+
}

app/templates/index.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta name="description" content="Analyze time and memory complexity of LeetCode solutions.">
7+
<title>LeetCode Solutions Complexity Analyzer</title>
8+
<link rel="icon" type="image/png" href="{{ url_for('static', filename='leetcode-logo.png') }}">
9+
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
10+
<link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet">
11+
<link rel="stylesheet" type='text/css' href="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/devicon.min.css"/>
12+
</head>
13+
<body class="bg-gray-100">
14+
<div class="container mx-auto px-4 py-8">
15+
<div class="bg-white shadow-md rounded-lg p-6 max-w-2xl mx-auto">
16+
<!-- Header Section -->
17+
<a href="#"><img src="{{ url_for('static', filename='leetcode-logo.png') }}" alt="LeetCode Logo" class="logo mb-6"></a>
18+
<h1 class="text-3xl font-bold mb-6 text-center text-gray-800">LeetCode Solutions Complexity Analyzer</h1>
19+
20+
<!-- Redirection Buttons Section -->
21+
<div class="flex justify-center space-x-4 mb-6">
22+
<a href="https://github.com/silvermete0r/QazKosuKosu" target="_blank" class="bg-purple-500 hover:bg-gray-600 text-white font-semibold py-2 px-4 rounded-md transition duration-300"><i class="devicon-python-plain"></i> Qaz++ Playground</a>
23+
<a href="/stars" class="bg-yellow-500 hover:bg-gray-600 text-white font-semibold py-2 px-4 rounded-md transition duration-300"><i class="devicon-cplusplus-plain"></i> Hall of Fame LC (KZ)</a>
24+
<a href="https://t.me/leetcodekz" target="_blank" class="bg-green-500 hover:bg-gray-600 text-white font-semibold py-2 px-4 rounded-md transition duration-300"><i class="devicon-php-plain"></i> LeetCode TG (KZ)</a>
25+
</div>
26+
27+
<!-- Form for Code Input -->
28+
<form method="POST" action="/" class="space-y-6">
29+
<div class="form-group">
30+
<label for="code" class="block text-gray-700 font-semibold mb-2">Code:</label>
31+
<textarea id="code" name="code" rows="10" class="w-full p-3 border border-gray-300 rounded-md" required></textarea>
32+
</div>
33+
<button type="submit" class="w-full bg-blue-500 text-white font-semibold py-3 rounded-md hover:bg-blue-600 transition duration-300">Analyze</button>
34+
</form>
35+
36+
<!-- Complexity Analysis Results (if available) -->
37+
{% if complexity %}
38+
<div class="mt-8 p-4 bg-blue-100 border border-blue-300 rounded-md">
39+
<h2 class="text-2xl font-semibold mb-4 text-gray-800">Complexity Analysis</h2>
40+
<img src="{{ url_for('static', filename='big-o-notation-chart.png') }}" alt="Big O Notation Chart" class="chart mb-6">
41+
<p class="text-lg text-gray-700">Time Complexity: <span class="font-bold text-red-500">{{ complexity.time }}</span></p>
42+
<p class="text-lg text-gray-700">Memory Complexity: <span class="font-bold text-red-500">{{ complexity.memory }}</span></p>
43+
<p class="text-lg text-gray-700">Description: <span class="italic text-yellow-600">{{ complexity.description }}</span></p>
44+
<p class="text-lg text-gray-700">Reference: <a href="https://www.bigocheatsheet.com/" class="text-purple-600" target="_blank">Big O CheatSheet</a></p>
45+
</div>
46+
{% endif %}
47+
48+
<!-- Github Stats -->
49+
<div class="flex justify-center space-x-4 mt-8 mb-6">
50+
<a href="https://github.com/silvermete0r/https://github.com/silvermete0r/leetcode-solution-complexity-analyzer">
51+
<img src="https://img.shields.io/github/stars/silvermete0r/leetcode-solution-complexity-analyzer?style=for-the-badge">
52+
</a>
53+
<a href="https://github.com/silvermete0r/leetcode-solution-complexity-analyzer">
54+
<img src="https://img.shields.io/github/contributors/silvermete0r/leetcode-solution-complexity-analyzer?style=for-the-badge">
55+
</a>
56+
<a href="https://github.com/silvermete0r/leetcode-solution-complexity-analyzer">
57+
<img src="https://img.shields.io/github/license/silvermete0r/leetcode-solution-complexity-analyzer?style=for-the-badge">
58+
</a>
59+
</div>
60+
61+
<!-- Footer Section -->
62+
<div class="text-center mt-8 text-gray-600">
63+
Made with ❤ by <a href="https://github.com/silvermete0r" class="text-green-600" target="_blank">@silvermete0r</a>
64+
</div>
65+
</div>
66+
</div>
67+
</body>
68+
</html>

app/templates/stars.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta name="description" content="Analyze time and memory complexity of LeetCode solutions.">
7+
<title>Kazakhstan LeetCode Hall of Fame</title>
8+
<link rel="icon" type="image/png" href="{{ url_for('static', filename='leetcode-logo.png') }}">
9+
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
10+
<link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet">
11+
<link rel="stylesheet" type='text/css' href="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/devicon.min.css"/>
12+
</head>
13+
<body class="bg-gray-100">
14+
<div class="container mx-auto px-4 py-8">
15+
<div class="bg-white shadow-md rounded-lg p-6 max-w-2xl mx-auto">
16+
<!-- Header Section -->
17+
<a href="/"><img src="{{ url_for('static', filename='leetcode-logo.png') }}" alt="LeetCode Logo" class="logo mb-6"></a>
18+
<h1 class="text-3xl font-bold mb-6 text-center text-gray-800">Kazakhstan LeetCode Hall of Fame</h1>
19+
20+
<!-- Redirection Buttons Section -->
21+
<div class="flex justify-center space-x-4 mb-6">
22+
<a href="https://github.com/silvermete0r/QazKosuKosu" target="_blank" class="bg-purple-500 hover:bg-gray-600 text-white font-semibold py-2 px-4 rounded-md transition duration-300"><i class="devicon-python-plain"></i> Qaz++ Playground</a>
23+
<a href="#" class="bg-yellow-500 hover:bg-gray-600 text-white font-semibold py-2 px-4 rounded-md transition duration-300"><i class="devicon-cplusplus-plain"></i> Hall of Fame LC (KZ)</a>
24+
<a href="https://t.me/leetcodekz" target="_blank" class="bg-green-500 hover:bg-gray-600 text-white font-semibold py-2 px-4 rounded-md transition duration-300"><i class="devicon-php-plain"></i> LeetCode TG (KZ)</a>
25+
</div>
26+
27+
<!-- TOP 10 LeetCode users in Kazakhstan -->
28+
<div class="mt-8 p-4 bg-blue-100 border border-blue-300 rounded-md">
29+
<h2 class="text-2xl font-semibold mb-4 text-gray-800">Top 10 LeetCode Users in Kazakhstan</h2>
30+
<ol class="list-decimal list-inside text-lg text-gray-700">
31+
<a href="https://leetcode.com/kaikaikaikaikai" target="_blank" class="text-red-600"><li>kaikaikaikaikai</li></a>
32+
<a href="https://leetcode.com/rtmkz" target="_blank" class="text-yellow-600"><li>rtmkz</li></a>
33+
<a href="https://leetcode.com/Tima_" target="_blank" class="text-purple-600"><li>Tima_</li></a>
34+
<a href="https://leetcode.com/DimmyT" target="_blank"><li>DimmyT</li></a>
35+
<a href="https://leetcode.com/erel3" target="_blank"><li>erel3</li></a>
36+
<a href="https://leetcode.com/shinbay" target="_blank"><li>shinbay</li></a>
37+
<a href="https://leetcode.com/daurenabd" target="_blank"><li>daurenabd</li></a>
38+
<a href="https://leetcode.com/temirulan" target="_blank"><li>temirulan</li></a>
39+
<a href="https://leetcode.com/proggerkz" target="_blank"><li>proggerkz</li></a>
40+
<a href="https://leetcode.com/404akhan" target="_blank"><li>404akhan</li></a>
41+
</ol>
42+
</div>
43+
44+
<p class="text-lg text-gray-700 mt-4">Note: This list is based on the user's performances in LeetCode contests. Last update: 18 Jun., 2024.</p>
45+
<p class="text-lg text-gray-700 mt-4">Data retrieved from <a href="https://clist.by/resource/leetcode.com/?country=KZ" target="_blank" class="text-purple-600">Clist.by Kazakhstan LeetCode Rankings</a></p>
46+
47+
<!-- Github Stats -->
48+
<div class="flex justify-center space-x-4 mt-8 mb-6">
49+
<a href="https://github.com/silvermete0r/https://github.com/silvermete0r/leetcode-solution-complexity-analyzer">
50+
<img src="https://img.shields.io/github/stars/silvermete0r/leetcode-solution-complexity-analyzer?style=for-the-badge">
51+
</a>
52+
<a href="https://github.com/silvermete0r/leetcode-solution-complexity-analyzer">
53+
<img src="https://img.shields.io/github/contributors/silvermete0r/leetcode-solution-complexity-analyzer?style=for-the-badge">
54+
</a>
55+
<a href="https://github.com/silvermete0r/leetcode-solution-complexity-analyzer">
56+
<img src="https://img.shields.io/github/license/silvermete0r/leetcode-solution-complexity-analyzer?style=for-the-badge">
57+
</a>
58+
</div>
59+
60+
<!-- Footer Section -->
61+
<div class="text-center mt-8 text-gray-600">
62+
Made with ❤ by <a href="https://github.com/silvermete0r" class="text-green-600" target="_blank">@silvermete0r</a>
63+
</div>
64+
</div>
65+
</div>
66+
</body>
67+
</html>

0 commit comments

Comments
 (0)