Spaces:
Sleeping
Sleeping
Commit ·
df1ba4a
1
Parent(s): 9fb8186
feat: replace chinese comments with english, update title to chinese
Browse files- app.py +19 -19
- templates/index.html +3 -3
app.py
CHANGED
|
@@ -9,27 +9,27 @@ SNIPPETS = [
|
|
| 9 |
# --- Python ---
|
| 10 |
{
|
| 11 |
"language": "Python",
|
| 12 |
-
"code": """#
|
| 13 |
def binary_search(arr, target):
|
| 14 |
left, right = 0, len(arr) - 1
|
| 15 |
|
| 16 |
while left <= right:
|
| 17 |
mid = (left + right) // 2
|
| 18 |
if arr[mid] == target:
|
| 19 |
-
return mid #
|
| 20 |
elif arr[mid] < target:
|
| 21 |
left = mid + 1
|
| 22 |
else:
|
| 23 |
right = mid - 1
|
| 24 |
|
| 25 |
-
return -1 #
|
| 26 |
},
|
| 27 |
{
|
| 28 |
"language": "Python",
|
| 29 |
-
"code": """# Flask
|
| 30 |
@app.route('/api/users', methods=['GET'])
|
| 31 |
def get_users():
|
| 32 |
-
#
|
| 33 |
users = db.session.query(User).all()
|
| 34 |
return jsonify({
|
| 35 |
"count": len(users),
|
|
@@ -38,7 +38,7 @@ def get_users():
|
|
| 38 |
},
|
| 39 |
{
|
| 40 |
"language": "Python",
|
| 41 |
-
"code": """#
|
| 42 |
def quick_sort(arr):
|
| 43 |
if len(arr) <= 1:
|
| 44 |
return arr
|
|
@@ -53,8 +53,8 @@ def quick_sort(arr):
|
|
| 53 |
{
|
| 54 |
"language": "JavaScript",
|
| 55 |
"code": """/**
|
| 56 |
-
*
|
| 57 |
-
*
|
| 58 |
*/
|
| 59 |
function debounce(func, wait) {
|
| 60 |
let timeout;
|
|
@@ -62,7 +62,7 @@ function debounce(func, wait) {
|
|
| 62 |
const context = this;
|
| 63 |
clearTimeout(timeout);
|
| 64 |
|
| 65 |
-
//
|
| 66 |
timeout = setTimeout(() => {
|
| 67 |
func.apply(context, args);
|
| 68 |
}, wait);
|
|
@@ -71,7 +71,7 @@ function debounce(func, wait) {
|
|
| 71 |
},
|
| 72 |
{
|
| 73 |
"language": "JavaScript",
|
| 74 |
-
"code": """// Promise
|
| 75 |
fetch('https://api.example.com/data')
|
| 76 |
.then(response => {
|
| 77 |
if (!response.ok) {
|
|
@@ -90,7 +90,7 @@ fetch('https://api.example.com/data')
|
|
| 90 |
# --- Go ---
|
| 91 |
{
|
| 92 |
"language": "Go",
|
| 93 |
-
"code": """//
|
| 94 |
package main
|
| 95 |
|
| 96 |
import (
|
|
@@ -101,7 +101,7 @@ import (
|
|
| 101 |
func main() {
|
| 102 |
var wg sync.WaitGroup
|
| 103 |
|
| 104 |
-
//
|
| 105 |
for i := 0; i < 5; i++ {
|
| 106 |
wg.Add(1)
|
| 107 |
go func(id int) {
|
|
@@ -110,7 +110,7 @@ func main() {
|
|
| 110 |
}(i)
|
| 111 |
}
|
| 112 |
|
| 113 |
-
wg.Wait() //
|
| 114 |
fmt.Println("All workers done")
|
| 115 |
}"""
|
| 116 |
},
|
|
@@ -118,7 +118,7 @@ func main() {
|
|
| 118 |
# --- SQL ---
|
| 119 |
{
|
| 120 |
"language": "SQL",
|
| 121 |
-
"code": """--
|
| 122 |
SELECT
|
| 123 |
u.username,
|
| 124 |
COUNT(o.id) as order_count,
|
|
@@ -134,7 +134,7 @@ ORDER BY total_spent DESC;"""
|
|
| 134 |
# --- React ---
|
| 135 |
{
|
| 136 |
"language": "React",
|
| 137 |
-
"code": """//
|
| 138 |
import React, { useState } from 'react';
|
| 139 |
|
| 140 |
export default function Counter() {
|
|
@@ -142,12 +142,12 @@ export default function Counter() {
|
|
| 142 |
|
| 143 |
return (
|
| 144 |
<div className="p-4 border rounded">
|
| 145 |
-
<h2 className="text-xl"
|
| 146 |
<button
|
| 147 |
onClick={() => setCount(count + 1)}
|
| 148 |
className="bg-blue-500 text-white px-4 py-2 mt-2"
|
| 149 |
>
|
| 150 |
-
|
| 151 |
</button>
|
| 152 |
</div>
|
| 153 |
);
|
|
@@ -157,7 +157,7 @@ export default function Counter() {
|
|
| 157 |
# --- Java ---
|
| 158 |
{
|
| 159 |
"language": "Java",
|
| 160 |
-
"code": """//
|
| 161 |
public class Singleton {
|
| 162 |
private static volatile Singleton instance;
|
| 163 |
|
|
@@ -179,7 +179,7 @@ public class Singleton {
|
|
| 179 |
# --- HTML/CSS ---
|
| 180 |
{
|
| 181 |
"language": "HTML",
|
| 182 |
-
"code": """<!--
|
| 183 |
<div class="card">
|
| 184 |
<img src="avatar.jpg" alt="User Avatar" class="card-img">
|
| 185 |
<div class="card-body">
|
|
|
|
| 9 |
# --- Python ---
|
| 10 |
{
|
| 11 |
"language": "Python",
|
| 12 |
+
"code": """# Binary Search Implementation
|
| 13 |
def binary_search(arr, target):
|
| 14 |
left, right = 0, len(arr) - 1
|
| 15 |
|
| 16 |
while left <= right:
|
| 17 |
mid = (left + right) // 2
|
| 18 |
if arr[mid] == target:
|
| 19 |
+
return mid # Target found
|
| 20 |
elif arr[mid] < target:
|
| 21 |
left = mid + 1
|
| 22 |
else:
|
| 23 |
right = mid - 1
|
| 24 |
|
| 25 |
+
return -1 # Not found"""
|
| 26 |
},
|
| 27 |
{
|
| 28 |
"language": "Python",
|
| 29 |
+
"code": """# Flask Route Example
|
| 30 |
@app.route('/api/users', methods=['GET'])
|
| 31 |
def get_users():
|
| 32 |
+
# Get user list from database
|
| 33 |
users = db.session.query(User).all()
|
| 34 |
return jsonify({
|
| 35 |
"count": len(users),
|
|
|
|
| 38 |
},
|
| 39 |
{
|
| 40 |
"language": "Python",
|
| 41 |
+
"code": """# Quick Sort Algorithm
|
| 42 |
def quick_sort(arr):
|
| 43 |
if len(arr) <= 1:
|
| 44 |
return arr
|
|
|
|
| 53 |
{
|
| 54 |
"language": "JavaScript",
|
| 55 |
"code": """/**
|
| 56 |
+
* Debounce Function
|
| 57 |
+
* Limits the rate at which a function can fire.
|
| 58 |
*/
|
| 59 |
function debounce(func, wait) {
|
| 60 |
let timeout;
|
|
|
|
| 62 |
const context = this;
|
| 63 |
clearTimeout(timeout);
|
| 64 |
|
| 65 |
+
// Execute later
|
| 66 |
timeout = setTimeout(() => {
|
| 67 |
func.apply(context, args);
|
| 68 |
}, wait);
|
|
|
|
| 71 |
},
|
| 72 |
{
|
| 73 |
"language": "JavaScript",
|
| 74 |
+
"code": """// Promise Chain Example
|
| 75 |
fetch('https://api.example.com/data')
|
| 76 |
.then(response => {
|
| 77 |
if (!response.ok) {
|
|
|
|
| 90 |
# --- Go ---
|
| 91 |
{
|
| 92 |
"language": "Go",
|
| 93 |
+
"code": """// Concurrency Example
|
| 94 |
package main
|
| 95 |
|
| 96 |
import (
|
|
|
|
| 101 |
func main() {
|
| 102 |
var wg sync.WaitGroup
|
| 103 |
|
| 104 |
+
// Start 5 goroutines
|
| 105 |
for i := 0; i < 5; i++ {
|
| 106 |
wg.Add(1)
|
| 107 |
go func(id int) {
|
|
|
|
| 110 |
}(i)
|
| 111 |
}
|
| 112 |
|
| 113 |
+
wg.Wait() // Wait for all goroutines to finish
|
| 114 |
fmt.Println("All workers done")
|
| 115 |
}"""
|
| 116 |
},
|
|
|
|
| 118 |
# --- SQL ---
|
| 119 |
{
|
| 120 |
"language": "SQL",
|
| 121 |
+
"code": """-- Query High Value Users
|
| 122 |
SELECT
|
| 123 |
u.username,
|
| 124 |
COUNT(o.id) as order_count,
|
|
|
|
| 134 |
# --- React ---
|
| 135 |
{
|
| 136 |
"language": "React",
|
| 137 |
+
"code": """// Simple Counter Component
|
| 138 |
import React, { useState } from 'react';
|
| 139 |
|
| 140 |
export default function Counter() {
|
|
|
|
| 142 |
|
| 143 |
return (
|
| 144 |
<div className="p-4 border rounded">
|
| 145 |
+
<h2 className="text-xl">Current Count: {count}</h2>
|
| 146 |
<button
|
| 147 |
onClick={() => setCount(count + 1)}
|
| 148 |
className="bg-blue-500 text-white px-4 py-2 mt-2"
|
| 149 |
>
|
| 150 |
+
Increment (+1)
|
| 151 |
</button>
|
| 152 |
</div>
|
| 153 |
);
|
|
|
|
| 157 |
# --- Java ---
|
| 158 |
{
|
| 159 |
"language": "Java",
|
| 160 |
+
"code": """// Singleton Pattern (Double Checked Locking)
|
| 161 |
public class Singleton {
|
| 162 |
private static volatile Singleton instance;
|
| 163 |
|
|
|
|
| 179 |
# --- HTML/CSS ---
|
| 180 |
{
|
| 181 |
"language": "HTML",
|
| 182 |
+
"code": """<!-- Simple Card Layout -->
|
| 183 |
<div class="card">
|
| 184 |
<img src="avatar.jpg" alt="User Avatar" class="card-img">
|
| 185 |
<div class="card-body">
|
templates/index.html
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title
|
| 7 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
<link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;700&display=swap" rel="stylesheet">
|
| 9 |
<style>
|
|
@@ -42,8 +42,8 @@
|
|
| 42 |
<!-- Header -->
|
| 43 |
<header class="w-full max-w-5xl flex flex-col md:flex-row justify-between items-center mb-6 gap-4">
|
| 44 |
<div class="flex items-baseline gap-2">
|
| 45 |
-
<h1 class="text-2xl font-bold text-blue-400 tracking-tight"
|
| 46 |
-
<span class="text-slate-500 text-xs font-mono">v1.
|
| 47 |
</div>
|
| 48 |
|
| 49 |
<div class="flex flex-wrap items-center gap-4 text-sm">
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>程序员打字练习 | Code Typing Practice</title>
|
| 7 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
<link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;700&display=swap" rel="stylesheet">
|
| 9 |
<style>
|
|
|
|
| 42 |
<!-- Header -->
|
| 43 |
<header class="w-full max-w-5xl flex flex-col md:flex-row justify-between items-center mb-6 gap-4">
|
| 44 |
<div class="flex items-baseline gap-2">
|
| 45 |
+
<h1 class="text-2xl font-bold text-blue-400 tracking-tight">程序员打字练习</h1>
|
| 46 |
+
<span class="text-slate-500 text-xs font-mono">v1.2</span>
|
| 47 |
</div>
|
| 48 |
|
| 49 |
<div class="flex flex-wrap items-center gap-4 text-sm">
|