Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions c-distance/credit.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
#include <lua.h>
#include <lauxlib.h>

// implement it here
int distance(lua_State* L) {
size_t len1, len2;
const char* str1 = luaL_checklstring(L, 1, &len1);
const char* str2 = luaL_checklstring(L, 2, &len2);
luaL_argcheck(L, len1 == len2, 2,
"Expect strings of equal length");
char* result = lua_newuserdata(L, len1);
int mismatches = 0;
int i;
for (i = 0; i < len1; i++) {
char a = str1[i];
char b = str2[i];
luaL_argcheck(L, a == '0' || a == '1', 1,
"Only characters '0' and '1' are allowed");
luaL_argcheck(L, b == '0' || b == '1', 1,
"Only characters '0' and '1' are allowed");
if (a == b) {
result[i] = a;
} else {
if (mismatches % 2 == 0) {
result[i] = a;
} else {
result[i] = b;
}
mismatches += 1;
}
}
if (mismatches % 2 == 1) {
// result = impossible
lua_pushnil(L);
return 1;
}
lua_pushlstring(L, result, len1);
return 1;
}

int luaopen_distance(lua_State* L) {
// return it here
return 0;
lua_pushcfunction(L, distance);
return 1;
}
12 changes: 6 additions & 6 deletions c-distance/spec.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
describe("distance (implementation)", function()
pending("loads module", function()
it("loads module", function()
local f = require 'distance'
end)

pending("returns equally distant string", function()
it("returns equally distant string", function()
local f = require 'distance'
local middle = f('00', '11')
assert(middle == '01' or middle == '10')
end)

pending("returns nil if no equally distant string exists",
it("returns nil if no equally distant string exists",
function()
local f = require 'distance'
assert(f('0', '1') == nil)
end)

pending("throws error if length if input strings differ",
it("throws error if length if input strings differ",
function()
local f = require 'distance'
assert.has_error(function()
f('0', '11')
end)
end)

pending("throws error if an argument is not string",
it("throws error if an argument is not string",
function()
local f = require 'distance'
assert.has_error(function()
Expand All @@ -34,7 +34,7 @@ describe("distance (implementation)", function()
end)
end)

pending("throws error if input contains non-0-or-1",
it("throws error if input contains non-0-or-1",
function()
local f = require 'distance'
assert.has_error(function()
Expand Down
39 changes: 38 additions & 1 deletion lua-distance/credit.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
local function dist(a, b)
local mismatches = 0
for i = 1, #a do
if a:sub(i, i) ~= b:sub(i, i) then
mismatches = mismatches + 1
end
end
return mismatches
end

local function has_error(f)
local status, err = pcall(f)
return not status
end

local function g(f)
-- implement it here
local p = f('00', '11')
assert(p == '01' or p == '10')
--
local p = f('0', '1')
assert(p == nil)
--
assert(has_error(function()
f()
end))
assert(has_error(function()
f('', '0')
end))
assert(has_error(function()
f('a', '0')
end))
assert(has_error(function()
f('00', '0')
end))
--
local p = f('00101', '10100')
assert(#p == 5)
assert(p:match('^[01]*$'))
assert(dist(p, '00101') == dist(p, '10100'))
end

return g
8 changes: 4 additions & 4 deletions lua-distance/spec.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
describe("distance (unit test)", function()
pending("loads module", function()
it("loads module", function()
local f = require 'distance.credit'
end)

pending("accepts true solution", function()
it("accepts true solution", function()
local f = require 'distance.credit'
assert.has_no_error(function()
f(function(A, B)
Expand Down Expand Up @@ -36,7 +36,7 @@ describe("distance (unit test)", function()
end)
end)

pending("accepts true solution 2", function()
it("accepts true solution 2", function()
local f = require 'distance.credit'
assert.has_no_error(function()
f(function(A, B)
Expand Down Expand Up @@ -70,7 +70,7 @@ describe("distance (unit test)", function()
end)
end)

pending("throws on bad solutions", function()
it("throws on bad solutions", function()
local f = require 'distance.credit'
assert.has_error(function()
f(function(A, B)
Expand Down