|
| 1 | +# Examples |
| 2 | + |
| 3 | +## Hello World |
| 4 | + |
| 5 | +Here's a simple "Hello World" example in different languages: |
| 6 | + |
| 7 | +<!-- langtabs-start --> |
| 8 | +```rust |
| 9 | +fn main() { |
| 10 | + println!("Hello, World!"); |
| 11 | +} |
| 12 | +``` |
| 13 | + |
| 14 | +```python |
| 15 | +def main(): |
| 16 | + print("Hello, World!") |
| 17 | + |
| 18 | +if __name__ == "__main__": |
| 19 | + main() |
| 20 | +``` |
| 21 | + |
| 22 | +```javascript |
| 23 | +function main() { |
| 24 | + console.log("Hello, World!"); |
| 25 | +} |
| 26 | + |
| 27 | +main(); |
| 28 | +``` |
| 29 | + |
| 30 | +```go |
| 31 | +package main |
| 32 | + |
| 33 | +import "fmt" |
| 34 | + |
| 35 | +func main() { |
| 36 | + fmt.Println("Hello, World!") |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +```java |
| 41 | +public class HelloWorld { |
| 42 | + public static void main(String[] args) { |
| 43 | + System.out.println("Hello, World!"); |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | +<!-- langtabs-end --> |
| 48 | + |
| 49 | +## Simple Function Example |
| 50 | + |
| 51 | +Here's how you might define a function to calculate factorial in different languages: |
| 52 | + |
| 53 | +<!-- langtabs-start --> |
| 54 | +```rust |
| 55 | +fn factorial(n: u64) -> u64 { |
| 56 | + match n { |
| 57 | + 0 | 1 => 1, |
| 58 | + _ => n * factorial(n - 1) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn main() { |
| 63 | + println!("5! = {}", factorial(5)); // Outputs: 5! = 120 |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +```python |
| 68 | +def factorial(n): |
| 69 | + if n <= 1: |
| 70 | + return 1 |
| 71 | + return n * factorial(n - 1) |
| 72 | + |
| 73 | +print(f"5! = {factorial(5)}") # Outputs: 5! = 120 |
| 74 | +``` |
| 75 | + |
| 76 | +```javascript |
| 77 | +function factorial(n) { |
| 78 | + if (n <= 1) return 1; |
| 79 | + return n * factorial(n - 1); |
| 80 | +} |
| 81 | + |
| 82 | +console.log(`5! = ${factorial(5)}`); // Outputs: 5! = 120 |
| 83 | +``` |
| 84 | + |
| 85 | +```go |
| 86 | +package main |
| 87 | + |
| 88 | +import "fmt" |
| 89 | + |
| 90 | +func factorial(n uint64) uint64 { |
| 91 | + if n <= 1 { |
| 92 | + return 1 |
| 93 | + } |
| 94 | + return n * factorial(n-1) |
| 95 | +} |
| 96 | + |
| 97 | +func main() { |
| 98 | + fmt.Printf("5! = %d\n", factorial(5)) // Outputs: 5! = 120 |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +```java |
| 103 | +public class FactorialExample { |
| 104 | + static long factorial(int n) { |
| 105 | + if (n <= 1) return 1; |
| 106 | + return n * factorial(n - 1); |
| 107 | + } |
| 108 | + |
| 109 | + public static void main(String[] args) { |
| 110 | + System.out.println("5! = " + factorial(5)); // Outputs: 5! = 120 |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | +<!-- langtabs-end --> |
| 115 | + |
| 116 | +## Data Structures Example |
| 117 | + |
| 118 | +Here's how you might implement a simple stack in different languages: |
| 119 | + |
| 120 | +<!-- langtabs-start --> |
| 121 | +```rust |
| 122 | +struct Stack<T> { |
| 123 | + items: Vec<T>, |
| 124 | +} |
| 125 | + |
| 126 | +impl<T> Stack<T> { |
| 127 | + fn new() -> Self { |
| 128 | + Stack { items: Vec::new() } |
| 129 | + } |
| 130 | + |
| 131 | + fn push(&mut self, item: T) { |
| 132 | + self.items.push(item); |
| 133 | + } |
| 134 | + |
| 135 | + fn pop(&mut self) -> Option<T> { |
| 136 | + self.items.pop() |
| 137 | + } |
| 138 | + |
| 139 | + fn is_empty(&self) -> bool { |
| 140 | + self.items.is_empty() |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +fn main() { |
| 145 | + let mut stack = Stack::new(); |
| 146 | + stack.push(1); |
| 147 | + stack.push(2); |
| 148 | + stack.push(3); |
| 149 | + |
| 150 | + while let Some(item) = stack.pop() { |
| 151 | + println!("Popped: {}", item); |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +```python |
| 157 | +class Stack: |
| 158 | + def __init__(self): |
| 159 | + self.items = [] |
| 160 | + |
| 161 | + def push(self, item): |
| 162 | + self.items.append(item) |
| 163 | + |
| 164 | + def pop(self): |
| 165 | + if not self.is_empty(): |
| 166 | + return self.items.pop() |
| 167 | + return None |
| 168 | + |
| 169 | + def is_empty(self): |
| 170 | + return len(self.items) == 0 |
| 171 | + |
| 172 | +# Usage |
| 173 | +stack = Stack() |
| 174 | +stack.push(1) |
| 175 | +stack.push(2) |
| 176 | +stack.push(3) |
| 177 | + |
| 178 | +while not stack.is_empty(): |
| 179 | + print(f"Popped: {stack.pop()}") |
| 180 | +``` |
| 181 | + |
| 182 | +```javascript |
| 183 | +class Stack { |
| 184 | + constructor() { |
| 185 | + this.items = []; |
| 186 | + } |
| 187 | + |
| 188 | + push(item) { |
| 189 | + this.items.push(item); |
| 190 | + } |
| 191 | + |
| 192 | + pop() { |
| 193 | + if (!this.isEmpty()) { |
| 194 | + return this.items.pop(); |
| 195 | + } |
| 196 | + return null; |
| 197 | + } |
| 198 | + |
| 199 | + isEmpty() { |
| 200 | + return this.items.length === 0; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +// Usage |
| 205 | +const stack = new Stack(); |
| 206 | +stack.push(1); |
| 207 | +stack.push(2); |
| 208 | +stack.push(3); |
| 209 | + |
| 210 | +while (!stack.isEmpty()) { |
| 211 | + console.log(`Popped: ${stack.pop()}`); |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +```go |
| 216 | +package main |
| 217 | + |
| 218 | +import "fmt" |
| 219 | + |
| 220 | +type Stack struct { |
| 221 | + items []int |
| 222 | +} |
| 223 | + |
| 224 | +func (s *Stack) Push(item int) { |
| 225 | + s.items = append(s.items, item) |
| 226 | +} |
| 227 | + |
| 228 | +func (s *Stack) Pop() (int, bool) { |
| 229 | + if s.IsEmpty() { |
| 230 | + return 0, false |
| 231 | + } |
| 232 | + |
| 233 | + index := len(s.items) - 1 |
| 234 | + item := s.items[index] |
| 235 | + s.items = s.items[:index] |
| 236 | + return item, true |
| 237 | +} |
| 238 | + |
| 239 | +func (s *Stack) IsEmpty() bool { |
| 240 | + return len(s.items) == 0 |
| 241 | +} |
| 242 | + |
| 243 | +func main() { |
| 244 | + stack := Stack{} |
| 245 | + stack.Push(1) |
| 246 | + stack.Push(2) |
| 247 | + stack.Push(3) |
| 248 | + |
| 249 | + for !stack.IsEmpty() { |
| 250 | + item, _ := stack.Pop() |
| 251 | + fmt.Printf("Popped: %d\n", item) |
| 252 | + } |
| 253 | +} |
| 254 | +``` |
| 255 | + |
| 256 | +```java |
| 257 | +import java.util.ArrayList; |
| 258 | + |
| 259 | +public class StackExample { |
| 260 | + static class Stack<T> { |
| 261 | + private ArrayList<T> items = new ArrayList<>(); |
| 262 | + |
| 263 | + public void push(T item) { |
| 264 | + items.add(item); |
| 265 | + } |
| 266 | + |
| 267 | + public T pop() { |
| 268 | + if (isEmpty()) { |
| 269 | + return null; |
| 270 | + } |
| 271 | + return items.remove(items.size() - 1); |
| 272 | + } |
| 273 | + |
| 274 | + public boolean isEmpty() { |
| 275 | + return items.isEmpty(); |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + public static void main(String[] args) { |
| 280 | + Stack<Integer> stack = new Stack<>(); |
| 281 | + stack.push(1); |
| 282 | + stack.push(2); |
| 283 | + stack.push(3); |
| 284 | + |
| 285 | + while (!stack.isEmpty()) { |
| 286 | + System.out.println("Popped: " + stack.pop()); |
| 287 | + } |
| 288 | + } |
| 289 | +} |
| 290 | +``` |
| 291 | +<!-- langtabs-end --> |
0 commit comments