diff --git a/c-gaps/credit.c b/c-gaps/credit.c index 2a60e39..3fc3adc 100644 --- a/c-gaps/credit.c +++ b/c-gaps/credit.c @@ -2,8 +2,96 @@ #include // implement it here +#include + +int clear_gaps(lua_State* L) +{ + luaL_argcheck(L, lua_istable(L, -1), -1, "Error, table_required\n"); + + size_t table_len = lua_objlen(L, -1); + + if ( table_len == 0) + { + lua_newtable(L); + return 1; + } + + const char** in_table = (const char**)malloc(sizeof(char*) * table_len); + + // get first string + size_t f_size; + lua_rawgeti(L, 1, 1); + in_table[0] = luaL_checklstring(L, -1, &f_size); + lua_pop(L, 1); + + //lua_istable + + int i = 2; + for (; i <= table_len; i++) + { + size_t str_size; + lua_rawgeti(L, 1, i); + + in_table[i - 1] = luaL_checklstring(L, -1, &str_size); + lua_pop(L, 1); + + luaL_argcheck(L, str_size == f_size, -1, "Error, sequences must have the same length\n"); + } + + // create place for out table + char** out_table = (char**)malloc(sizeof(char*) * table_len); + for (i = 0; i < table_len; i++) + { + out_table[i] = (char*)malloc(sizeof(char) * f_size); + } + + int j = 0; + size_t new_size = 0; + + // delete only gaps columns + for (; j < f_size; j++) + { + char only_gaps = 1; + for (i = 0; i < table_len; i++) + { + if (in_table[i][j] != '-') + { + only_gaps = 0; + } + } + + + if (! only_gaps) + { + for (i = 0; i < table_len; i++) + { + out_table[i][new_size] = in_table[i][j]; + } + new_size++; + } + } + + + lua_newtable(L); // create lua table + + for (i = 0; i < table_len; i++) + { + lua_pushlstring(L, out_table[i], new_size); + lua_rawseti(L, -2, i + 1); + } + + // free memory + + for (i = 0; i < table_len; i++) + { + free(out_table[i]); + } + free(in_table); + free(out_table); + return 1; // function returns one argument +} int luaopen_gaps(lua_State* L) { - // return it here - return 0; + lua_pushcfunction(L, clear_gaps); + return 1; } diff --git a/c-gaps/spec.lua b/c-gaps/spec.lua index 2129000..498c005 100644 --- a/c-gaps/spec.lua +++ b/c-gaps/spec.lua @@ -1,9 +1,9 @@ describe("gaps (implementation)", function() - pending("loads module", function() + it("loads module", function() local f = require 'gaps' end) - pending("doesn't change good alignment", function() + it("doesn't change good alignment", function() local f = require 'gaps' assert.same({}, f({})) assert.same({''}, f({''})) @@ -14,7 +14,7 @@ describe("gaps (implementation)", function() assert.same({'A'}, f({'A-'})) end) - pending("removes gap columns", function() + it("removes gap columns", function() local f = require 'gaps' assert.same({''}, f({'-'})) assert.same({'', ''}, f({'-', '-'})) @@ -30,7 +30,7 @@ describe("gaps (implementation)", function() })) end) - pending("throws error if input is not table", + it("throws error if input is not table", function() local f = require 'gaps' assert.has_error(function() @@ -41,7 +41,7 @@ describe("gaps (implementation)", function() end) end) - pending("throws error if table members are not strings", + it("throws error if table members are not strings", function() local f = require 'gaps' assert.has_error(function() @@ -49,7 +49,7 @@ describe("gaps (implementation)", function() end) end) - pending("throws error if strings' lengths differ", + it("throws error if strings' lengths differ", function() local f = require 'gaps' assert.has_error(function() diff --git a/lua-shuf/credit.lua b/lua-shuf/credit.lua index 068643b..efaba4e 100644 --- a/lua-shuf/credit.lua +++ b/lua-shuf/credit.lua @@ -1,5 +1,39 @@ local function g(f) - -- implement it here + local n = 1000 + local occurense = {} + for i = 1, n do + occurense[i] = 0 + end + local result = f(n) + + assert(type(result) == "table") + assert(#result == n) + for i = 1, n do + occurense[result[i]] = 1 + end + + local sum = 0 + for i = 1, n do + sum = sum + occurense[i] + end + assert(sum == n) + + local len = 0 + for k, y in pairs(result) do + len = len + 1 + end + + assert(len == n) + + assert(not pcall(function() + f('sdads') + end)) + assert(not pcall(function() + f('1ddd') + end)) + assert(not pcall(function() + f({0}) + end)) end return g diff --git a/lua-shuf/spec.lua b/lua-shuf/spec.lua index be744df..467fa47 100644 --- a/lua-shuf/spec.lua +++ b/lua-shuf/spec.lua @@ -40,32 +40,32 @@ local function shufRangeBad3(n) end describe("shuf (unit test)", function() - pending("loads module", function() + it("loads module", function() local g = require 'shuf.credit' end) - pending("accepts good shufRange", function() + it("accepts good shufRange", function() local g = require 'shuf.credit' assert.has_no_error(function() g(shufRange) end) end) - pending("don't accept bad shufRangeBad", function() + it("don't accept bad shufRangeBad", function() local g = require 'shuf.credit' assert.has_error(function() g(shufRangeBad) end) end) - pending("don't accept bad shufRangeBad2", function() + it("don't accept bad shufRangeBad2", function() local g = require 'shuf.credit' assert.has_error(function() g(shufRangeBad2) end) end) - pending("don't accept bad shufRangeBad3", function() + it("don't accept bad shufRangeBad3", function() local g = require 'shuf.credit' assert.has_error(function() g(shufRangeBad3)