Code Playground

Explore practical code examples for AI, web development, and algorithms

Simple Tokenizer Implementation

A basic tokenizer that splits text into tokens for AI processing

javascript
javascript
// Simple tokenizer for demonstration
function tokenizeText(text) {
  // Split on word boundaries and punctuation
  const tokens = text.match(/\w+|[^\w\s]/g) || [];
  
  // Add token metadata
  return tokens.map((token, index) => ({
    text: token,
...
View Full Code →

Temperature Sampling in AI

How temperature affects randomness in AI text generation

python
python
import numpy as np

def temperature_sampling(logits, temperature=1.0):
    """
    Apply temperature to model outputs for controlled randomness.
    
    Args:
        logits: Raw model outputs (before softmax)
...
View Full Code →

Text Embeddings Generator

Convert text to vector embeddings for similarity search

typescript
typescript
interface Embedding {
  text: string;
  vector: number[];
}

class EmbeddingGenerator {
  private vocabulary: Map<string, number>;
  
...
View Full Code →

Interactive React Component

A React component with state management and effects

jsx
jsx
import React, { useState, useEffect } from 'react';

function AnimatedCounter({ target, duration = 1000 }) {
  const [count, setCount] = useState(0);
  const [isAnimating, setIsAnimating] = useState(false);
  
  useEffect(() => {
    if (target === count) return;
...
View Full Code →

API Request Handler

Robust API handler with retry logic and error handling

javascript
javascript
class APIHandler {
  constructor(baseURL, options = {}) {
    this.baseURL = baseURL;
    this.maxRetries = options.maxRetries || 3;
    this.retryDelay = options.retryDelay || 1000;
    this.timeout = options.timeout || 5000;
  }
  
...
View Full Code →

Data Processing Pipeline

Stream-based data processing with transformations

python
python
from typing import Iterator, Callable, Any
import json
from datetime import datetime

class DataPipeline:
    """Efficient data processing pipeline with chaining"""
    
    def __init__(self, source: Iterator[Any]):
...
View Full Code →

Binary Search Tree Implementation

A complete BST with insert, search, and traversal operations

java
java
public class BinarySearchTree<T extends Comparable<T>> {
    private class Node {
        T data;
        Node left, right;
        
        Node(T data) {
            this.data = data;
            this.left = null;
...
View Full Code →

QuickSort with Optimizations

Efficient QuickSort implementation with median-of-three pivot selection

cpp
cpp
#include <vector>
#include <random>
#include <algorithm>

template<typename T>
class QuickSort {
private:
    // Threshold for switching to insertion sort
...
View Full Code →

Learn by Doing 🚀

These examples are designed to be practical and educational. Copy, modify, and experiment with them in your own projects!