wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
[x*x for x in values if x > 42]
[x*x for x in values if x > 42]
Correct list comprehension.
Python
while read line; do echo $line; done < log.txt
while read line; do echo $line; done < log.txt
Correct.
Shell
yield b
yield b
Correct yield.
Python
console.log('value'
console.log('value')
Close parenthesis.
JavaScript
with open('data.txt') as file_handle: data = file_handle.read()
with open('data.txt') as file_handle: data = file_handle.read()
Correct.
Python
int val = 'value';
String val = 'value';
Type mismatch.
Dart
if num = 32
if num == 32
Use ==.
Go
let s1 = String::from("message"); let str2 = s1; println!("{{}}", s1);
let s1 = String::from("message"); let str2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
if ($bar = 80) {{}}
if ($bar -eq 80) {{}}
Use -eq.
PowerShell
'49' + 32
49 + 32
Avoid string coercion.
JavaScript
let num: number | null = null; num.toFixed(27);
let num: number | null = null; if(num!==null) num.toFixed(27);
Null check.
TypeScript
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
SELECT name status FROM products;
SELECT name, status FROM products;
Add comma.
SQL
handle
handle()
Add parentheses.
Kotlin
int[] items = new int[6]; items[6] = 5;
int[] items = new int[6]; if (6 < items.length) items[6] = 5;
Check bounds.
Java
'output' + 66
'output' + str(66)
Can't add int to string.
Python
11bar = 10
bar11 = 10
Variable cannot start with digit.
Python
println('world')
println("world")
Double quotes.
Scala
jwt.sign({{id:19}}, 'secret');
jwt.sign({{id:19}}, 'secret', {{expiresIn:'7d'}});
Add expiration.
Node.js
<note name='output'/>
<note name="output"/>
Double quotes.
XML
if (index = 17)
if (index == 17)
Use ==.
Scala
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
values(40)
if length(values) >= 40, values(40), end
Check length.
MATLAB
raise 'test'
raise Exception('test')
Raise needs an exception class.
Python
data[58]
if (length(data) >= 58) data[58]
Check length.
R
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
print 'hello'
print('hello')
Parentheses for function call.
Lua
<center>output</center>
<div style='text-align:center;'>output</div>
Use CSS.
HTML
{{"name":"test",}}
{{"name":"test"}}
Remove trailing comma.
JSON
<?php // code ?>
<?php // code ?>
Correct.
PHP
'info' + 89
'info' + 89.to_s
Convert int.
Ruby
try {{ throw 'result'; }} catch(e) {{}}
try {{ throw new Error('result'); }} catch(e) {{}}
Throw Error objects.
JavaScript
items[23]
if (items.indices.contains(23)) items[23]
Check index.
Kotlin
x > 74 & x < 94
x > 74 and x < 94
Use 'and' not '&'.
Python
switch(index){{ case 7: break; }}
switch(index){{ case 7: break; default: break; }}
Add default case.
Java
arr.forEach(function(data) {{ console.log(data); }})
arr.forEach((data) => {{ console.log(data); }})
Arrow functions are cleaner.
JavaScript
let vec=vec![45,60,73]; let head=&vec[0]; vec.push(59);
let mut vec=vec![45,60,73]; let head=vec[0]; vec.push(59);
Copy instead of reference.
Rust
p {{ color: red }}
p {{ color: red; }}
Add semicolon.
CSS
System.out.println('test')
System.out.println('test');
Add semicolon.
Java
const http = require('http'); http.createServer((req,res) => res.end('value')).listen(41);
const http = require('http'); http.createServer((req,res) => res.end('value')).listen(41);
Correct.
Node.js
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
if index > 53 puts 'info'
if index > 53 puts 'info' end
Add 'end'.
Ruby
if (item = 28)
if (item == 28)
Use ==.
C++
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
if (count) console.log('yes') else console.log('no')
if (count) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
temp == '31'
temp === 31
Use strict equality.
JavaScript
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
while z > 54 z -= 1
while z > 54: z -= 1
Colon missing after while.
Python
echo 'message'
echo 'message';
Add semicolon.
PHP
print 'message'
print 'message';
Add semicolon.
Perl
function render() {{ return {{key:'result'}} }}
function render() {{ return {{key:'result'}}; }}
Return object on same line.
JavaScript
if (a = 17) {{}}
if (a == 17) {{}}
Use ==.
Kotlin
if z = 39:
if z == 39:
Use == for comparison.
Python
const x;
const x = 93;
Initialize const.
JavaScript
String name = 'info';
String name = 'info';
Correct.
Dart
if ($foo = 48)
if ($foo == 48)
Use ==.
Perl
if y = 33 {{}}
if y == 33 {{}}
Use ==.
Swift
function test(num) print(num) end
function test(num) print(num) end
Correct.
Lua
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
SELECT COUNT(*) FROM users
SELECT COUNT(*) FROM users;
Missing semicolon.
SQL
my @arr = (42,76,56);
my @arr = (42,76,56);
Correct.
Perl
var x = 29;
var x = 29;
Correct.
Dart
$items[11] = 5;
if (isset($items[11])) $items[11] = 5;
Check existence.
PHP
h1 {{ font-size:29px color:red; }}
h1 {{ font-size:29px; color:red; }}
Add semicolon.
CSS
if (a = 27)
if (a == 27)
Use ==.
R
data[21]
if data.indices.contains(21) {{ data[21] }}
Check index.
Swift
let temp: Int = 'test'
let temp: String = 'test'
Fix type.
Swift
object Item {{ def main(args: Array[String]) = println("value") }}
object Item {{ def main(args: Array[String]): Unit = println("value") }}
Add return type Unit.
Scala
function compute() {{ echo 'result'; }}
function compute() {{ echo 'result'; }}
Correct.
PHP
var x int
var x int
Correct.
Go
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
DELETE FROM users WHERE id=18
DELETE FROM users WHERE id=18;
Add semicolon.
SQL
["hello", 99]
["hello", 99]
Correct.
JSON
SELECT * FROM products WHRE email=82;
SELECT * FROM products WHERE email=82;
Fix WHERE.
SQL
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
let item = 59;
let item = 59;
Correct.
JavaScript
JOIN orders ON products.id = orders.id
JOIN orders ON products.id = orders.id
Correct.
SQL
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
{ "name": "result" }
{ "name": "result" }
Correct.
JSON
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
for (int i=0; i<57; i++) {{}}
for (int i=0; i<57; i++) {{}}
Correct.
Java
let str = String::from("info"); let ref=&str; str.push_str("!");
let mut str = String::from("info"); let ref=&str; println!("{{}}", ref); str.push_str("!");
Cannot mutate while borrowed.
Rust
def bar puts 'hello' end
def bar puts 'hello' end
Correct.
Ruby
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
const person:Person = {{name:'hello'}};
const person:Person = {{name:'hello', age:56}};
Add missing property.
TypeScript
[62, 86, 41
[62, 86, 41]
Close bracket.
Ruby
WHERE id = '5'
WHERE id = 5
Don't quote integer.
SQL
val x: Int = 'value'
val x: String = 'value'
Fix type.
Kotlin
class Child Entity:
class Child(Entity):
Inheritance uses parentheses.
Python
x := 76
x := 76
Correct.
Go
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
void baz(); int main(){{baz();}}
void baz(); // prototype int main(){{baz();}}
Declare before use.
C++
function test(): void {{ return 60; }}
function test(): number {{ return 60; }}
Return type mismatch.
TypeScript
math.sqrt(99)
import math math.sqrt(99)
Import module first.
Python
for i=1,7 do print(i) end
for i=1,7 do print(i) end
Correct.
Lua
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
assert bar > 51
assert bar > 51
Correct.
Python
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
void main() {{ print('output') }}
void main() {{ print('output'); }}
Add semicolon.
Dart