Certainly! Here are several independent micro code blocks, each containing at least 10 lines of code with random but sensible content:
# Micro block 1: Simple function to calculate factorial
def factorial(n):
if n < 0:
return None
if n == 0 or n == 1:
return 1
result = 1
for i in range(2, n + 1):
result *= i
return result
print(factorial(5))
// Micro block 2: Check if a string is a palindrome
function isPalindrome(str) {
const cleaned = str.toLowerCase().replace(/[\W_]/g, '');
const reversed = cleaned.split('').reverse().join('');
return cleaned === reversed;
}
console.log(isPalindrome("A man, a plan, a canal, Panama"));
// Micro block 3: Class representing a simple counter
public class Counter {
private int count;
public Counter() {
count = 0;
}
public void increment() {
count++;
}
public void reset() {
count = 0;
}
public int getCount() {
return count;
}
}
// Micro block 4: Swap two integers using pointers
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
# Micro block 5: Generate Fibonacci series up to n terms
def fibonacci(n)
seq = [0, 1]
while seq.length < n
seq << seq[-1] + seq[-2]
end
seq[0...n]
end
puts fibonacci(10).join(', ')
<?php
// Micro block 6: Simple login simulation
function authenticate($user, $pass) {
$validUser = "admin";
$validPass = "1234";
if ($user === $validUser && $pass === $validPass) {
return "Login successful";
}
return "Login failed";
}
echo authenticate("admin", "1234");
?>
// Micro block 7: Find max number in slice
package main
import "fmt"
func max(nums []int) int {
maxNum := nums[0]
for _, v := range nums {
if v > maxNum {
maxNum = v
}
}
return maxNum
}
func main() {
numbers := []int{5, 8, 2, 9, 3}
fmt.Println("Max:", max(numbers))
}
// Micro block 8: Struct for Point and distance calculation
import Foundation
struct Point {
var x: Double
var y: Double
func distance(to point: Point) -> Double {
let dx = x - point.x
let dy = y - point.y
return (dx * dx + dy * dy).squareRoot()
}
}
let p1 = Point(x: 3, y: 4)
let p2 = Point(x: 0, y: 0)
print(p1.distance(to: p2))
# Micro block 9: Script to count files in directory
#!/bin/bash
dir=${1:-.}
if [ -d "$dir" ]; then
count=$(ls -1 "$dir" | wc -l)
echo "There are $count files in $dir"
else
echo "$dir is not a valid directory"
fi
// Micro block 10: Interface and class implementing it
interface Vehicle {
speed: number;
move(): void;
}
class Car implements Vehicle {
speed: number;
constructor(speed: number) {
this.speed = speed;
}
move() {
console.log(`Car is moving at ${this.speed} km/h`);
}
}
const myCar = new Car(80);
myCar.move();
If you need more (hundreds or thousands) just tell me how you'd prefer to receive them or if you want them generated programmatically!