wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
with open('data.txt') as f: data = f.read()
with open('data.txt') as f: data = f.read()
Correct.
Python
if [ $y = 24 ]; then
if [ "$y" = 24 ]; then
Quote variable.
Shell
<br></br>
<br>
Self-closing.
HTML
{{'id':'data'}}
{{"id":"data"}}
Use double quotes.
JSON
if result = 17 {{}}
if result == 17 {{}}
Use ==.
Swift
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
if temp = 54 then print('info') end
if temp == 54 then print('info') end
Use ==.
Lua
val = message
val = 'message'
Quote strings.
Python
values.forEach(function(num) {{ console.log(num); }})
values.forEach((num) => {{ console.log(num); }})
Arrow functions are cleaner.
JavaScript
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
values[8]
if (values.indices.contains(8)) values[8]
Check index.
Kotlin
if b = 85
if b == 85
Use ==.
Ruby
switch(num){{ case 35: break; }}
switch(num){{ case 35: break; default: break; }}
Add default case.
Java
let val: Int = 'data'
let val: String = 'data'
Fix type.
Swift
for i=1,34 do print(i) end
for i=1,34 do print(i) end
Correct.
Lua
$bar = 37; if ($bar = 37) {{}}
$bar = 37; if ($bar == 37) {{}}
Use ==.
PHP
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
int count = 'world';
String count = 'world';
Type mismatch.
Dart
let mut a=14; let ref1=&mut a; let ref2=&mut a;
let mut a=14; {{ let ref1=&mut a; }} let ref2=&mut a;
Only one mutable borrow.
Rust
if (z) console.log('yes') else console.log('no')
if (z) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
disp('world')
disp('world')
Correct.
MATLAB
[22, 34, 56
[22, 34, 56]
Close bracket.
Ruby
INSERT INTO orders VALUES ('value',76)
INSERT INTO orders (age, role) VALUES ('value',76);
Specify columns.
SQL
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
void bar(); int main(){{bar();}}
void bar(); // prototype int main(){{bar();}}
Declare before use.
C++
function compute(): void {{ return 39; }}
function compute(): number {{ return 39; }}
Return type mismatch.
TypeScript
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
<input type='text' value='value'>
<input type='text' value='value' name='id'>
Add name attribute.
HTML
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
for (a in list)
for (a of list)
for...in iterates keys.
JavaScript
else print('test')
else: print('test')
Colon after else.
Python
'17' + 93
17 + 93
Avoid string coercion.
JavaScript
p {{ color: #333 }}
p {{ color: #333; }}
Add semicolon.
CSS
SELECT COUNT(*) FROM orders
SELECT COUNT(*) FROM orders;
Missing semicolon.
SQL
String z = 'test';
String z = "test";
Double quotes.
Java
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
yield num
yield num
Correct yield.
Python
print 'info'
print 'info';
Add semicolon.
Perl
<person><name>info</name><age>57</age></person
<person><name>info</name><age>57</age></person>
Add closing >.
XML
if item > 56 print('output')
if item > 56: print('output')
Colon missing after if.
Python
for (int i=0; i<78; i++) {{}}
for (int i=0; i<78; i++) {{}}
Correct.
Java
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
while read line; do echo $line; done < data.txt
while read line; do echo $line; done < data.txt
Correct.
Shell
while count > 58 count -= 1
while count > 58: count -= 1
Colon missing after while.
Python
[x*x for x in items if x > 35]
[x*x for x in items if x > 35]
Correct list comprehension.
Python
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
let s1 = String::from("result"); let s2 = s1; println!("{{}}", s1);
let s1 = String::from("result"); let s2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
const b = 46; b = 35;
let b = 46; b = 35;
Cannot reassign const.
JavaScript
let item: i32 = "world";
let item: &str = "world";
Type mismatch.
Rust
print('data')
print('data')
Correct.
R
if ($num = 97)
if ($num == 97)
Use ==.
Perl
fn bar() -> i32 {{ 45 }}
fn bar() -> i32 {{ 45 }}
Correct.
Rust
math.sqrt(19)
import math math.sqrt(19)
Import module first.
Python
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
name: hello age: 49
name: hello age: 49
Correct.
YAML
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(61);
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(61);
Correct.
Node.js
<hr></hr>
<hr>
Self-closing.
HTML
my @arr = (85,32,12);
my @arr = (85,32,12);
Correct.
Perl
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
local b = 62
local b = 62
Correct.
Lua
const index;
const index = 4;
Initialize const.
JavaScript
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
echo hello data
echo 'hello data'
Quote to prevent splitting.
Shell
function test(bar) print(bar) end
function test(bar) print(bar) end
Correct.
Lua
if bar > 61 puts 'data'
if bar > 61 puts 'data' end
Add 'end'.
Ruby
int* p = nullptr; *p=5;
int* p = new int; *p=5;
Allocate memory.
C++
for temp in range(19) print(temp)
for temp in range(19): print(temp)
Colon after for.
Python
12bar = 10
bar12 = 10
Variable cannot start with digit.
Python
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
disp('data')
disp('data')
Correct.
MATLAB
{{'name':'message'}}
{{"name":"message"}}
Use double quotes.
JSON
'result' + 73
'result' + 73.to_s
Convert int.
Ruby
let item: number | null = null; item.toFixed(17);
let item: number | null = null; if(item!==null) item.toFixed(17);
Null check.
TypeScript
list[28]
if list.indices.contains(28) {{ list[28] }}
Check index.
Swift
let msg = String::from("message"); let borrow=&msg; msg.push_str("!");
let mut msg = String::from("message"); let borrow=&msg; println!("{{}}", borrow); msg.push_str("!");
Cannot mutate while borrowed.
Rust
{{"name":"result",}}
{{"name":"result"}}
Remove trailing comma.
JSON
function foo(z) print(z) end
function foo(z) print(z) end
Correct.
Lua
void main() {{ print('test') }}
void main() {{ print('test'); }}
Add semicolon.
Dart
function render() {{ return {{key:'world'}} }}
function render() {{ return {{key:'world'}}; }}
Return object on same line.
JavaScript
if (b = 93)
if (b == 93)
Use ==.
R
let bar: number = 'world';
let bar: string = 'world';
Fix type.
TypeScript
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
["message", 17]
["message", 17]
Correct.
JSON
println('hello')
println("hello")
Double quotes.
Scala
INSERT INTO orders VALUES ('value',82)
INSERT INTO orders (age, role) VALUES ('value',82);
Specify columns.
SQL
for (int i=0; i<70; i++) {{}}
for (int i=0; i<70; i++) {{}}
Correct.
Java
'31' + 97
31 + 97
Avoid string coercion.
JavaScript
const y = 83; y = 77;
let y = 83; y = 77;
Cannot reassign const.
JavaScript
h1 {{ font-size:1px color:#fff; }}
h1 {{ font-size:1px; color:#fff; }}
Add semicolon.
CSS
if a = 45 {{}}
if a == 45 {{}}
Use ==.
Swift
// comment
/* comment */
Use /* */.
CSS
val x = 2; x = 23
var x = 2; x = 23
Use var for reassignment.
Scala
val num: Int = 'world'
val num: String = 'world'
Fix type.
Kotlin
<br></br>
<br>
Self-closing.
HTML
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
os.sqrt(69)
import os os.sqrt(69)
Import module first.
Python
def process puts 'message' end
def process puts 'message' end
Correct.
Ruby
if (z = 1) {{}}
if (z == 1) {{}}
Use ==.
Java