Prerequisites — Solutions


Exercise 1 Solution

mkdir -p /tmp/my-portfolio
cd /tmp/my-portfolio
touch README.md about.md projects.md
git init

README.md content:

# Alice Johnson

I'm learning to build AI-powered applications.

## Skills
- Python
- JavaScript
- Git
- Flask

## Connect
[GitHub](https://github.com/alicejohnson)
git add README.md
git commit -m "Initial commit"
echo "About me page" > about.md
git add about.md
git commit -m "Add about page"

Exercise 2 Solution

# counter.py
import sys

def count_file(filename):
    with open(filename, 'r') as f:
        content = f.read()
    lines = content.split('\n')
    words = content.split()
    chars = len(content)
    return len(lines), len(words), chars

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python counter.py <filename>")
        sys.exit(1)

    filename = sys.argv[1]
    lines, words, chars = count_file(filename)

    print(f"File: {filename}")
    print(f"{'Lines:':<10}{lines:>6}")
    print(f"{'Words:':<10}{words:>6}")
    print(f"{'Chars:':<10}{chars:>6}")

Test:

echo -e "Hello world\nThis is a test\nPython is fun" > sample.txt
python counter.py sample.txt

Exercise 3 Solution

// weather.ts
interface Todo {
    userId: number;
    id: number;
    title: string;
    completed: boolean;
}

interface Weather {
    city: string;
    temperature: number;
    condition: string;
}

async function fetchWeather(): Promise<void> {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos/1");
    const todo: Todo = await response.json();

    const weather: Weather = {
        city: "Sample City",
        temperature: 22,
        condition: todo.title
    };

    console.log(`City: ${weather.city}, Temperature: ${weather.temperature}°C, Condition: ${weather.condition}`);
}

fetchWeather().catch(console.error);

Exercise 4 Solution

mkdir -p /tmp/git-flow && cd /tmp/git-flow
git init

# main branch
echo 'print("Hello")' > main.py
git add main.py && git commit -m "Add hello script"

# Feature branch
git checkout -b feature/greet
cat > main.py << 'EOF'
name = input("Enter your name: ")
print(f"Hello, {name}!")
EOF
git add main.py && git commit -m "Add greeting feature"

# Merge back
git checkout main
git merge feature/greet
git branch -d feature/greet
git log --oneline

Exercise 5 Solution

data.py:

import json
import random

names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
cities = ["NYC", "London", "Tokyo", "Paris", "Sydney"]

people = []
for i in range(100):
    person = {
        "name": random.choice(names),
        "age": random.randint(10, 80),
        "city": random.choice(cities)
    }
    people.append(person)

with open("people.json", "w") as f:
    json.dump(people, f, indent=2)

print("Generated 100 people")

report.ts:

import * as fs from 'fs';

interface Person {
    name: string;
    age: number;
    city: string;
}

const data = fs.readFileSync('people.json', 'utf-8');
const people: Person[] = JSON.parse(data);

const adults = people.filter(p => p.age > 18);

let markdown = "# Adults Report\n\n";
markdown += "| Name | Age | City |\n";
markdown += "|------|-----|------|\n";

adults.forEach(p => {
    markdown += `| ${p.name} | ${p.age} | ${p.city} |\n`;
});

fs.writeFileSync('adults.md', markdown);
console.log(`Wrote ${adults.length} adults to adults.md`);
# Run it
python data.py
tsx report.ts
git init
git add .
git commit -m "Generate people data and adult report"
git log --oneline
git diff HEAD~1

Exercise 6 Solution

find . -name "*.py" -print | xargs wc -l | tail -1 > loc.txt

Exercise 7 Solution

# Spaghetti Carbonara

## Ingredients
- 200g spaghetti
- 2 eggs
- 100g pancetta
- 50g Parmesan cheese
- Salt and pepper

## Instructions
1. Boil spaghetti in salted water.
2. Fry pancetta until crispy.
3. Beat eggs with grated Parmesan.
4. Drain pasta and mix with pancetta.
5. Remove from heat and stir in egg mixture.
6. Season with pepper and serve.

> **Tip:** Don't let the eggs scramble — the heat of the pasta is enough to cook them.

Cooking time: 15 minutes