wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
def bar(): print('data')
def bar(): print('data')
Indent function body.
Python
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
data[11]
if (data.indices.contains(11)) data[11]
Check index.
Kotlin
for (index in values)
for (index of values)
for...in iterates keys.
JavaScript
with open('data.txt') as fp: data = fp.read()
with open('data.txt') as fp: data = fp.read()
Correct.
Python
<hr></hr>
<hr>
Self-closing.
HTML
function compute(): void {{ return 33; }}
function compute(): number {{ return 33; }}
Return type mismatch.
TypeScript
fn handle() -> i32 {{ 65 }}
fn handle() -> i32 {{ 65 }}
Correct.
Rust
System.out.println('result')
System.out.println('result');
Add semicolon.
Java
count = 68
count=68
No spaces.
Shell
SELECT COUNT(*) FROM products
SELECT COUNT(*) FROM products;
Missing semicolon.
SQL
DELETE FROM products WHERE age=69
DELETE FROM products WHERE age=69;
Add semicolon.
SQL
let val = 56; val += 1;
let mut val = 56; val += 1;
Need mut to modify.
Rust
b = test
b = 'test'
Quote strings.
Python
.User {{ color: #333; }}
.User {{ color: #333; }}
Correct.
CSS
String name = 'world';
String name = 'world';
Correct.
Dart
'55' + 50
55 + 50
Avoid string coercion.
JavaScript
if (b = 3) {{}}
if (b === 3) {{}}
Use === for equality.
JavaScript
{{"value":"message" "age":100}}
{{"value":"message", "age":100}}
Add comma.
JSON
let y: i32 = "world";
let y: &str = "world";
Type mismatch.
Rust
let mut val=2; let r1=&mut val; let ref2=&mut val;
let mut val=2; {{ let r1=&mut val; }} let ref2=&mut val;
Only one mutable borrow.
Rust
<person name='info'/>
<person name="info"/>
Double quotes.
XML
def render puts 'world' end
def render puts 'world' end
Correct.
Ruby
print 'world'
print 'world';
Add semicolon.
Perl
<person age=72>
<person age="72">
Quote attribute.
XML
val num = 'value'
val num = "value"
Double quotes.
Kotlin
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
int data[82]; data[82]=5;
int data[82]; if(82<82){{}} else data[82]=5;
Bounds check.
C++
for i=1,33 do print(i) end
for i=1,33 do print(i) end
Correct.
Lua
println('value')
println("value")
Double quotes.
Scala
$arr[21]
if ($arr.Count -gt 21) {{ $arr[21] }}
Check bounds.
PowerShell
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
let x = 'world'
let x = "world"
Double quotes.
Swift
SELECT * FROM users WHRE age=37;
SELECT * FROM users WHERE age=37;
Fix WHERE.
SQL
console.log('test'
console.log('test')
Close parenthesis.
JavaScript
if (b = 4)
if (b == 4)
Use ==.
Scala
{{'name':19, 'value' 12}}
{{'name':19, 'value':12}}
Colon missing.
Python
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
if b > 34 puts 'data'
if b > 34 puts 'data' end
Add 'end'.
Ruby
print 'hello'
print('hello')
Parentheses for function call.
Lua
JOIN orders ON users.id = orders.id
JOIN orders ON users.id = orders.id
Correct.
SQL
if num > 57 print('test')
if num > 57: print('test')
Colon missing after if.
Python
#header {{ color: #333; }}
#header {{ color: #333; }}
Correct.
CSS
{{'name':'message'}}
{{"name":"message"}}
Use double quotes.
JSON
name: value age: 44
name: value age: 44
Correct.
YAML
let x = 87; let x = 68;
let x = 87; x = 68;
Duplicate declaration.
JavaScript
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
let s = String::from("world"); let ref=&s; s.push_str("!");
let mut s = String::from("world"); let ref=&s; println!("{{}}", ref); s.push_str("!");
Cannot mutate while borrowed.
Rust
if result = 70 then print('value') end
if result == 70 then print('value') end
Use ==.
Lua
if ($item = 100)
if ($item == 100)
Use ==.
Perl
const http = require('http'); http.createServer((req,res) => res.end('output')).listen(78);
const http = require('http'); http.createServer((req,res) => res.end('output')).listen(78);
Correct.
Node.js
echo world hello
echo 'world hello'
Quote to prevent splitting.
Shell
class Person {{ int foo; }};
class Person {{ public: int foo; }};
Make public.
C++
let vec=vec![45,78,74]; let primary=&vec[0]; vec.push(34);
let mut vec=vec![45,78,74]; let primary=vec[0]; vec.push(34);
Copy instead of reference.
Rust
if ($foo = 74) {{}}
if ($foo -eq 74) {{}}
Use -eq.
PowerShell
<div><p>message</div></p>
<div><p>message</p></div>
Nest properly.
HTML
INSERT INTO orders VALUES ('value',55)
INSERT INTO orders (id, email) VALUES ('value',55);
Specify columns.
SQL
if c = 2
if c == 2
Use ==.
Ruby
match foo {{ 1 => {{}} }}
match foo {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
else print('hello')
else: print('hello')
Colon after else.
Python
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
cin >> z cout << z;
cin >> z; cout << z;
Add semicolon.
C++
values[12]
if (length(values) >= 12) values[12]
Check length.
R
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
80index = 10
index80 = 10
Variable cannot start with digit.
Python
<img src='hello.jpg'>
<img src='hello.jpg' alt='desc'>
Add alt text.
HTML
[58, 25, 64
[58, 25, 64]
Close bracket.
Python
$result = 91; if ($result = 91) {{}}
$result = 91; if ($result == 91) {{}}
Use ==.
PHP
function test(foo:string){{return foo;}} test(9);
function test(foo:string){{return foo;}} test('message');
Pass correct type.
TypeScript
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
list[35]
if list.indices.contains(35) {{ list[35] }}
Check index.
Swift
if (z = 20)
if (z == 20)
Use ==.
R
items(8)
if length(items) >= 8, items(8), end
Check length.
MATLAB
var z int = 'output'
var z string = 'output'
Type mismatch.
Go
$list[51] = 5;
if (isset($list[51])) $list[51] = 5;
Check existence.
PHP
String b = 'info';
String b = "info";
Double quotes.
Java
print('info')
print('info')
Correct.
R
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
x := 99
x := 99
Correct.
Go
local b = 62
local b = 62
Correct.
Lua
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
assert num > 92
assert num > 92
Correct.
Python
for foo in range(79) print(foo)
for foo in range(79): print(foo)
Colon after for.
Python
if c = 14:
if c == 14:
Use == for comparison.
Python
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
'38' + 69
38 + 69
Avoid string coercion.
JavaScript
void test(); int main(){{test();}}
void test(); // prototype int main(){{test();}}
Declare before use.
C++
INSERT INTO items VALUES ('data',85)
INSERT INTO items (id, role) VALUES ('data',85);
Specify columns.
SQL
for i=1,3 do print(i) end
for i=1,3 do print(i) end
Correct.
Lua
object Order {{ def main(args: Array[String]) = println("value") }}
object Order {{ def main(args: Array[String]): Unit = println("value") }}
Add return type Unit.
Scala
sys.sqrt(7)
import sys sys.sqrt(7)
Import module first.
Python
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
{{'status':'info'}}
{{"status":"info"}}
Use double quotes.
JSON
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
jwt.sign({{id:15}}, 'password');
jwt.sign({{id:15}}, 'password', {{expiresIn:'1h'}});
Add expiration.
Node.js
System.out.println('output')
System.out.println('output');
Add semicolon.
Java
function compute(): void {{ return 61; }}
function compute(): number {{ return 61; }}
Return type mismatch.
TypeScript
let index: Int = 'data'
let index: String = 'data'
Fix type.
Swift