Add lua-format config and auto-format all exercises

This commit is contained in:
Camden Dixie O'Brien 2023-12-11 12:11:47 +00:00
parent 467ca0446a
commit 017ef8bac4
9 changed files with 79 additions and 54 deletions

33
.lua-format Normal file
View File

@ -0,0 +1,33 @@
column_limit: 80
indent_width: 1
use_tab: true
tab_width: 4
continuation_indent_width: 1
spaces_before_call: 1
keep_simple_control_block_one_line: true
keep_simple_function_one_line: false
align_args: true
break_after_functioncall_lp: false
break_before_functioncall_rp: false
spaces_inside_functioncall_parens: false
spaces_inside_functiondef_parens: false
align_parameter: true
chop_down_parameter: false
break_after_functiondef_lp: false
break_before_functiondef_rp: false
align_table_field: true
break_after_table_lb: true
break_before_table_rb: true
chop_down_table: false
chop_down_kv_table: true
table_sep: ","
extra_sep_at_table_end: false
column_table_limit: 0
column_table_limit_kv: 0
spaces_inside_table_braces: false
break_after_operator: true
double_quote_to_single_quote: false
single_quote_to_double_quote: false
spaces_around_equals_in_field: true
line_breaks_after_function_body: 1
line_separator: input

View File

@ -1,7 +1,6 @@
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function f() function f()
-- Your implementation here -- Your implementation here
end end
-- Tests ----------------------------------------------------------------------- -- Tests -----------------------------------------------------------------------

View File

@ -1,7 +1,6 @@
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_anagram(str1, str2) function is_anagram(str1, str2)
-- Your implementation here -- Your implementation here
end end
-- Tests ----------------------------------------------------------------------- -- Tests -----------------------------------------------------------------------
@ -9,31 +8,31 @@ end
local luaunit = require("luaunit.luaunit") local luaunit = require("luaunit.luaunit")
function test_listen_is_anagram_of_silent() function test_listen_is_anagram_of_silent()
luaunit.assertTrue(is_anagram("listen", "silent")) luaunit.assertTrue(is_anagram("listen", "silent"))
end end
function test_astronomers_is_anagram_of_moon_starers() function test_astronomers_is_anagram_of_moon_starers()
luaunit.assertTrue(is_anagram("astronomers", "moon starers")) luaunit.assertTrue(is_anagram("astronomers", "moon starers"))
end end
function test_dormitory_is_anagram_of_dirty_room() function test_dormitory_is_anagram_of_dirty_room()
luaunit.assertTrue(is_anagram("dormitory", "dirty room")) luaunit.assertTrue(is_anagram("dormitory", "dirty room"))
end end
function test_foo_is_not_anagram_of_bar() function test_foo_is_not_anagram_of_bar()
luaunit.assertFalse(is_anagram("foo", "bar")) luaunit.assertFalse(is_anagram("foo", "bar"))
end end
function test_foo_is_not_anagram_of_of() function test_foo_is_not_anagram_of_of()
luaunit.assertFalse(is_anagram("foo", "of")) luaunit.assertFalse(is_anagram("foo", "of"))
end end
function test_bar_is_not_anagram_of_barn() function test_bar_is_not_anagram_of_barn()
luaunit.assertFalse(is_anagram("bar", "barn")) luaunit.assertFalse(is_anagram("bar", "barn"))
end end
function test_barn_is_not_anagram_of_bar() function test_barn_is_not_anagram_of_bar()
luaunit.assertFalse(is_anagram("barn", "bar")) luaunit.assertFalse(is_anagram("barn", "bar"))
end end
os.exit(luaunit.LuaUnit.run()) os.exit(luaunit.LuaUnit.run())

View File

@ -1,7 +1,6 @@
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function greatest_common_divisor(x, y) function greatest_common_divisor(x, y)
-- Your implementation here -- Your implementation here
end end
-- Tests ----------------------------------------------------------------------- -- Tests -----------------------------------------------------------------------
@ -9,15 +8,15 @@ end
local luaunit = require("luaunit.luaunit") local luaunit = require("luaunit.luaunit")
function test_greatest_common_divisor_of_1386_and_3213_is_63() function test_greatest_common_divisor_of_1386_and_3213_is_63()
luaunit.assertEquals(greatest_common_divisor(1386, 3213), 63) luaunit.assertEquals(greatest_common_divisor(1386, 3213), 63)
end end
function test_greatest_common_divisor_of_1470_and_3234_is_294() function test_greatest_common_divisor_of_1470_and_3234_is_294()
luaunit.assertEquals(greatest_common_divisor(1470, 3234), 294) luaunit.assertEquals(greatest_common_divisor(1470, 3234), 294)
end end
function test_greatest_common_divisor_of_931_and_399_is_133() function test_greatest_common_divisor_of_931_and_399_is_133()
luaunit.assertEquals(greatest_common_divisor(931, 399), 133) luaunit.assertEquals(greatest_common_divisor(931, 399), 133)
end end
os.exit(luaunit.LuaUnit.run()) os.exit(luaunit.LuaUnit.run())

View File

@ -1,7 +1,6 @@
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function hamming_distance(str1, str2) function hamming_distance(str1, str2)
-- Your implementation here -- Your implementation here
end end
-- Tests ----------------------------------------------------------------------- -- Tests -----------------------------------------------------------------------
@ -9,11 +8,11 @@ end
local luaunit = require("luaunit.luauint") local luaunit = require("luaunit.luauint")
function test_distance_between_foo_and_bar_is_3() function test_distance_between_foo_and_bar_is_3()
luaunit.assertEquals(hamming_distance("foo", "bar"), 3) luaunit.assertEquals(hamming_distance("foo", "bar"), 3)
end end
function test_distance_between_bar_and_baz_is_1() function test_distance_between_bar_and_baz_is_1()
luaunit.assertEquals(hamming_distance("bar", "baz"), 1) luaunit.assertEquals(hamming_distance("bar", "baz"), 1)
end end
os.exit(luaunit.LuaUnit.run()) os.exit(luaunit.LuaUnit.run())

View File

@ -1,7 +1,6 @@
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_leap_year(year) function is_leap_year(year)
-- Your implementation here -- Your implementation here
end end
-- Tests ----------------------------------------------------------------------- -- Tests -----------------------------------------------------------------------
@ -9,19 +8,19 @@ end
local luaunit = require("luaunit.luaunit") local luaunit = require("luaunit.luaunit")
function test_2004_is_leap_year() function test_2004_is_leap_year()
luaunit.assertTrue(is_leap_year(2004)) luaunit.assertTrue(is_leap_year(2004))
end end
function test_1993_is_not_leap_year() function test_1993_is_not_leap_year()
luaunit.assertFalse(is_leap_year(1993)) luaunit.assertFalse(is_leap_year(1993))
end end
function test_1900_is_not_leap_year() function test_1900_is_not_leap_year()
luaunit.assertFalse(is_leap_year(1900)) luaunit.assertFalse(is_leap_year(1900))
end end
function test_2000_is_leap_year() function test_2000_is_leap_year()
luaunit.assertTrue(is_leap_year(2000)) luaunit.assertTrue(is_leap_year(2000))
end end
os.exit(luaunit.LuaUnit.run()) os.exit(luaunit.LuaUnit.run())

View File

@ -1,7 +1,6 @@
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_prime(x) function is_prime(x)
-- Your implementation here -- Your implementation here
end end
-- Tests ----------------------------------------------------------------------- -- Tests -----------------------------------------------------------------------
@ -9,23 +8,23 @@ end
local luaunit = require("luaunit.luauint") local luaunit = require("luaunit.luauint")
function test_1_is_not_prime() function test_1_is_not_prime()
luaunit.assertFalse(is_prime(1)) luaunit.assertFalse(is_prime(1))
end end
function test_2_is_prime() function test_2_is_prime()
luaunit.assertTrue(is_prime(2)) luaunit.assertTrue(is_prime(2))
end end
function test_15_is_not_prime() function test_15_is_not_prime()
luaunit.assertFalse(is_prime(15)) luaunit.assertFalse(is_prime(15))
end end
function test_441_is_not_prime() function test_441_is_not_prime()
luaunit.assertFalse(is_prime(441)) luaunit.assertFalse(is_prime(441))
end end
function test_563_is_prime() function test_563_is_prime()
luaunit.assertTrue(is_prime(563)) luaunit.assertTrue(is_prime(563))
end end
os.exit(luaunit.LuaUnit.run()) os.exit(luaunit.LuaUnit.run())

View File

@ -1,7 +1,6 @@
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_queen_threat(queen_position, pawn_position) function is_queen_threat(queen_position, pawn_position)
-- Your implementation here -- Your implementation here
end end
-- Tests ----------------------------------------------------------------------- -- Tests -----------------------------------------------------------------------
@ -9,31 +8,31 @@ end
local luaunit = require("luaunit.luaunit") local luaunit = require("luaunit.luaunit")
function test_B4_theatens_G4() function test_B4_theatens_G4()
luaunit.assertTrue(is_queen_threat("B4", "G4")) luaunit.assertTrue(is_queen_threat("B4", "G4"))
end end
function test_D7_theatens_D1() function test_D7_theatens_D1()
luaunit.assertTrue(is_queen_threat("D7", "D1")) luaunit.assertTrue(is_queen_threat("D7", "D1"))
end end
function test_C5_theatens_F8() function test_C5_theatens_F8()
luaunit.assertTrue(is_queen_threat("C5", "F8")) luaunit.assertTrue(is_queen_threat("C5", "F8"))
end end
function test_B3_theatens_D1() function test_B3_theatens_D1()
luaunit.assertTrue(is_queen_threat("B3", "D1")) luaunit.assertTrue(is_queen_threat("B3", "D1"))
end end
function test_B4_does_not_threaten_F3() function test_B4_does_not_threaten_F3()
luaunit.assertFalse(is_queen_threat("B4", "F3")) luaunit.assertFalse(is_queen_threat("B4", "F3"))
end end
function test_E2_does_not_threaten_G1() function test_E2_does_not_threaten_G1()
luaunit.assertFalse(is_queen_threat("E2", "G1")) luaunit.assertFalse(is_queen_threat("E2", "G1"))
end end
function test_A8_does_not_threaten_G3() function test_A8_does_not_threaten_G3()
luaunit.assertFalse(is_queen_threat("A8", "G3")) luaunit.assertFalse(is_queen_threat("A8", "G3"))
end end
os.exit(luaunit.LuaUnit.run()) os.exit(luaunit.LuaUnit.run())

View File

@ -1,5 +1,4 @@
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
-- Give the colours whatever values are convenient -- Give the colours whatever values are convenient
black = undefined black = undefined
brown = undefined brown = undefined
@ -15,7 +14,7 @@ gold = undefined
silver = undefined silver = undefined
function decode_resistance(colours) function decode_resistance(colours)
-- Your implementation here -- Your implementation here
end end
-- Tests ----------------------------------------------------------------------- -- Tests -----------------------------------------------------------------------
@ -23,28 +22,28 @@ end
local luaunit = require("luaunit.luaunit") local luaunit = require("luaunit.luaunit")
function test_violet_orange_black_is_73_ohms() function test_violet_orange_black_is_73_ohms()
local resistance = decode_resistance({violet, orange, black}) local resistance = decode_resistance({violet, orange, black})
luaunit.assertEquals(resistance, 73) luaunit.assertEquals(resistance, 73)
end end
function test_brown_green_black_is_15_ohms() function test_brown_green_black_is_15_ohms()
local resistance = decode_resistance({brown, green, black}) local resistance = decode_resistance({brown, green, black})
luaunit.assertEquals(resistance, 15) luaunit.assertEquals(resistance, 15)
end end
function test_green_blue_orange_is_56_kiloohms() function test_green_blue_orange_is_56_kiloohms()
local resistance = decode_resistance({green, blue, orange}) local resistance = decode_resistance({green, blue, orange})
luaunit.assertEquals(resistance, 56000) luaunit.assertEquals(resistance, 56000)
end end
function test_white_yellow_yellow_is_940_kiloohms() function test_white_yellow_yellow_is_940_kiloohms()
local resistance = decode_resistance({white, yellow, yellow}) local resistance = decode_resistance({white, yellow, yellow})
luaunit.assertEquals(resistance, 940000) luaunit.assertEquals(resistance, 940000)
end end
function test_orange_red_silver_is_320_milliohms() function test_orange_red_silver_is_320_milliohms()
local resistance = decode_resistance({orange, red, silver}) local resistance = decode_resistance({orange, red, silver})
luaunit.assertEquals(resistance, 0.32) luaunit.assertEquals(resistance, 0.32)
end end
os.exit(luaunit.LuaUnit.run()) os.exit(luaunit.LuaUnit.run())