refactor: use python instead of javascript

This commit is contained in:
Bjarke Sporring
2026-01-05 12:21:48 +01:00
parent e1b8d8418a
commit f11f5a4646
14 changed files with 550 additions and 735 deletions

View File

@@ -27,327 +27,261 @@ git config user.email "user@workshop.local" | Out-Null
# Commit 1: Initial calculator
$calc1 = @"
class Calculator {
add(a, b) {
return a + b;
}
}
module.exports = Calculator;
class Calculator:
def add(self, a, b):
return a + b
"@
Set-Content -Path "calculator.js" -Value $calc1
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc1
git add calculator.py
git commit -m "Initial calculator with add function" | Out-Null
# Commit 2: Add subtract
$calc2 = @"
class Calculator {
add(a, b) {
return a + b;
}
class Calculator:
def add(self, a, b):
return a + b
subtract(a, b) {
return a - b;
}
}
module.exports = Calculator;
def subtract(self, a, b):
return a - b
"@
Set-Content -Path "calculator.js" -Value $calc2
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc2
git add calculator.py
git commit -m "Add subtract function" | Out-Null
# Commit 3: Add multiply
$calc3 = @"
class Calculator {
add(a, b) {
return a + b;
}
class Calculator:
def add(self, a, b):
return a + b
subtract(a, b) {
return a - b;
}
def subtract(self, a, b):
return a - b
multiply(a, b) {
return a * b;
}
}
module.exports = Calculator;
def multiply(self, a, b):
return a * b
"@
Set-Content -Path "calculator.js" -Value $calc3
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc3
git add calculator.py
git commit -m "Add multiply function" | Out-Null
# Commit 4: Add divide
$calc4 = @"
class Calculator {
add(a, b) {
return a + b;
}
class Calculator:
def add(self, a, b):
return a + b
subtract(a, b) {
return a - b;
}
def subtract(self, a, b):
return a - b
multiply(a, b) {
return a * b;
}
def multiply(self, a, b):
return a * b
divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
}
module.exports = Calculator;
def divide(self, a, b):
if b == 0:
raise ValueError('Division by zero')
return a / b
"@
Set-Content -Path "calculator.js" -Value $calc4
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc4
git add calculator.py
git commit -m "Add divide function" | Out-Null
# Commit 5: Add modulo
$calc5 = @"
class Calculator {
add(a, b) {
return a + b;
}
class Calculator:
def add(self, a, b):
return a + b
subtract(a, b) {
return a - b;
}
def subtract(self, a, b):
return a - b
multiply(a, b) {
return a * b;
}
def multiply(self, a, b):
return a * b
divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
def divide(self, a, b):
if b == 0:
raise ValueError('Division by zero')
return a / b
modulo(a, b) {
return a % b;
}
}
module.exports = Calculator;
def modulo(self, a, b):
return a % b
"@
Set-Content -Path "calculator.js" -Value $calc5
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc5
git add calculator.py
git commit -m "Add modulo function" | Out-Null
# Commit 6: BUG - Introduce error in add function
$calc6 = @"
class Calculator {
add(a, b) {
return a - b; // BUG: Should be a + b
}
class Calculator:
def add(self, a, b):
return a - b # BUG: Should be a + b
subtract(a, b) {
return a - b;
}
def subtract(self, a, b):
return a - b
multiply(a, b) {
return a * b;
}
def multiply(self, a, b):
return a * b
divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
def divide(self, a, b):
if b == 0:
raise ValueError('Division by zero')
return a / b
modulo(a, b) {
return a % b;
}
}
module.exports = Calculator;
def modulo(self, a, b):
return a % b
"@
Set-Content -Path "calculator.js" -Value $calc6
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc6
git add calculator.py
git commit -m "Refactor add function for clarity" | Out-Null
# Commit 7: Add power function (bug still exists)
$calc7 = @"
class Calculator {
add(a, b) {
return a - b; // BUG: Should be a + b
}
class Calculator:
def add(self, a, b):
return a - b # BUG: Should be a + b
subtract(a, b) {
return a - b;
}
def subtract(self, a, b):
return a - b
multiply(a, b) {
return a * b;
}
def multiply(self, a, b):
return a * b
divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
def divide(self, a, b):
if b == 0:
raise ValueError('Division by zero')
return a / b
modulo(a, b) {
return a % b;
}
def modulo(self, a, b):
return a % b
power(a, b) {
return Math.pow(a, b);
}
}
module.exports = Calculator;
def power(self, a, b):
return a ** b
"@
Set-Content -Path "calculator.js" -Value $calc7
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc7
git add calculator.py
git commit -m "Add power function" | Out-Null
# Commit 8: Add square root
$calc8 = @"
class Calculator {
add(a, b) {
return a - b; // BUG: Should be a + b
}
import math
subtract(a, b) {
return a - b;
}
class Calculator:
def add(self, a, b):
return a - b # BUG: Should be a + b
multiply(a, b) {
return a * b;
}
def subtract(self, a, b):
return a - b
divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
def multiply(self, a, b):
return a * b
modulo(a, b) {
return a % b;
}
def divide(self, a, b):
if b == 0:
raise ValueError('Division by zero')
return a / b
power(a, b) {
return Math.pow(a, b);
}
def modulo(self, a, b):
return a % b
sqrt(a) {
return Math.sqrt(a);
}
}
def power(self, a, b):
return a ** b
module.exports = Calculator;
def sqrt(self, a):
return math.sqrt(a)
"@
Set-Content -Path "calculator.js" -Value $calc8
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc8
git add calculator.py
git commit -m "Add square root function" | Out-Null
# Commit 9: Add absolute value
$calc9 = @"
class Calculator {
add(a, b) {
return a - b; // BUG: Should be a + b
}
import math
subtract(a, b) {
return a - b;
}
class Calculator:
def add(self, a, b):
return a - b # BUG: Should be a + b
multiply(a, b) {
return a * b;
}
def subtract(self, a, b):
return a - b
divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
def multiply(self, a, b):
return a * b
modulo(a, b) {
return a % b;
}
def divide(self, a, b):
if b == 0:
raise ValueError('Division by zero')
return a / b
power(a, b) {
return Math.pow(a, b);
}
def modulo(self, a, b):
return a % b
sqrt(a) {
return Math.sqrt(a);
}
def power(self, a, b):
return a ** b
abs(a) {
return Math.abs(a);
}
}
def sqrt(self, a):
return math.sqrt(a)
module.exports = Calculator;
def abs(self, a):
return abs(a)
"@
Set-Content -Path "calculator.js" -Value $calc9
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc9
git add calculator.py
git commit -m "Add absolute value function" | Out-Null
# Commit 10: Add max function
$calc10 = @"
class Calculator {
add(a, b) {
return a - b; // BUG: Should be a + b
}
import math
subtract(a, b) {
return a - b;
}
class Calculator:
def add(self, a, b):
return a - b # BUG: Should be a + b
multiply(a, b) {
return a * b;
}
def subtract(self, a, b):
return a - b
divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
def multiply(self, a, b):
return a * b
modulo(a, b) {
return a % b;
}
def divide(self, a, b):
if b == 0:
raise ValueError('Division by zero')
return a / b
power(a, b) {
return Math.pow(a, b);
}
def modulo(self, a, b):
return a % b
sqrt(a) {
return Math.sqrt(a);
}
def power(self, a, b):
return a ** b
abs(a) {
return Math.abs(a);
}
def sqrt(self, a):
return math.sqrt(a)
max(a, b) {
return a > b ? a : b;
}
}
def abs(self, a):
return abs(a)
module.exports = Calculator;
def max(self, a, b):
return a if a > b else b
"@
Set-Content -Path "calculator.js" -Value $calc10
git add calculator.js
Set-Content -Path "calculator.py" -Value $calc10
git add calculator.py
git commit -m "Add max function" | Out-Null
# Create a test file
$test = @"
const Calculator = require('./calculator.js');
const calc = new Calculator();
import sys
from calculator import Calculator
// Test addition (this will fail due to bug)
const result = calc.add(5, 3);
if (result !== 8) {
console.log('FAIL: add(5, 3) returned ' + result + ', expected 8');
process.exit(1);
}
calc = Calculator()
console.log('PASS: All tests passed');
process.exit(0);
# Test addition (this will fail due to bug)
result = calc.add(5, 3)
if result != 8:
print(f'FAIL: add(5, 3) returned {result}, expected 8')
sys.exit(1)
print('PASS: All tests passed')
sys.exit(0)
"@
Set-Content -Path "test.js" -Value $test
Set-Content -Path "test.py" -Value $test
# Return to module directory
Set-Location ..
@@ -363,11 +297,11 @@ Write-Host "Manually checking each commit would be tedious." -ForegroundColor Ye
Write-Host "Use git bisect to find it efficiently!" -ForegroundColor Green
Write-Host "`nYour task:" -ForegroundColor Yellow
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
Write-Host "2. Test the bug: node test.js (it will fail)" -ForegroundColor White
Write-Host "2. Test the bug: python test.py (it will fail)" -ForegroundColor White
Write-Host "3. Start bisect: git bisect start" -ForegroundColor White
Write-Host "4. Mark current as bad: git bisect bad" -ForegroundColor White
Write-Host "5. Mark old commit as good: git bisect good HEAD~10" -ForegroundColor White
Write-Host "6. Git will checkout a commit - test it: node test.js" -ForegroundColor White
Write-Host "6. Git will checkout a commit - test it: python test.py" -ForegroundColor White
Write-Host "7. Mark result: git bisect good (if test passes) or git bisect bad (if it fails)" -ForegroundColor White
Write-Host "8. Repeat until Git finds the first bad commit" -ForegroundColor White
Write-Host "9. Note the commit hash" -ForegroundColor White

View File

@@ -89,7 +89,7 @@ if ($userCommit -ne $actualBadCommit) {
Write-Host " git bisect start" -ForegroundColor White
Write-Host " git bisect bad" -ForegroundColor White
Write-Host " git bisect good HEAD~10" -ForegroundColor White
Write-Host " # Then test with: node test.js" -ForegroundColor White
Write-Host " # Then test with: node test.py" -ForegroundColor White
Write-Host " git bisect good # or bad" -ForegroundColor White
Set-Location ..
exit 1
@@ -100,7 +100,7 @@ if ($userCommit -ne $actualBadCommit) {
# Verify the commit actually has the bug
git checkout $actualBadCommit 2>$null | Out-Null
$calcContent = Get-Content "calculator.js" -Raw
$calcContent = Get-Content "calculator.py" -Raw
if ($calcContent -notmatch "add\(a, b\)[\s\S]*?return a - b") {
Write-Host "[WARNING] The identified commit doesn't seem to have the expected bug." -ForegroundColor Yellow