From 9beaa93c5b06290654f20a027521621927dd351e Mon Sep 17 00:00:00 2001 From: dmitrypenzar1996 Date: Sat, 23 May 2015 13:10:09 +0300 Subject: [PATCH 1/6] My solution --- c-gaps/credit.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 2 deletions(-) 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; } From c4e0b265dceb4f82989bc4c8ccfe5360bcd93a9e Mon Sep 17 00:00:00 2001 From: dmitrypenzar1996 Date: Sat, 23 May 2015 15:40:08 +0300 Subject: [PATCH 2/6] Pending to it --- c-gaps/spec.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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() From 135797afb14a0d0baccbd743d5aa13e904aa2214 Mon Sep 17 00:00:00 2001 From: dmitrypenzar1996 Date: Sat, 23 May 2015 15:57:07 +0300 Subject: [PATCH 3/6] Pending to it --- lua-shuf/spec.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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) From 4ab015c4734ecb8b1e527409b02f07d5f5c09828 Mon Sep 17 00:00:00 2001 From: dmitrypenzar1996 Date: Sat, 23 May 2015 15:58:22 +0300 Subject: [PATCH 4/6] Solution shulf --- lua-shuf/credit.lua | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lua-shuf/credit.lua b/lua-shuf/credit.lua index 068643b..cd6ebe6 100644 --- a/lua-shuf/credit.lua +++ b/lua-shuf/credit.lua @@ -1,5 +1,34 @@ local function g(f) - -- implement it here + local n = 1000 + local occurense = {} + for i = 1, n do + n[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 ipairs(result) do + len = len + 1 + end + + assert(len == n) + assert(f(0) == {}) + + assert(pcall(f('hjhg'))) + assert(pcall(f('1'))) + assert(pcall(f(1))) end return g From 445a0a86ebe4a97d04f24ecc9f3be2b2eaf44616 Mon Sep 17 00:00:00 2001 From: dmitrypenzar1996 Date: Sat, 23 May 2015 16:08:39 +0300 Subject: [PATCH 5/6] Solution shulf --- lua-shuf/credit.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lua-shuf/credit.lua b/lua-shuf/credit.lua index cd6ebe6..d85edbc 100644 --- a/lua-shuf/credit.lua +++ b/lua-shuf/credit.lua @@ -19,16 +19,23 @@ local function g(f) assert(sum == n) local len = 0 - for k, y in ipairs(result) do + for k, y in pairs(result) do len = len + 1 end assert(len == n) assert(f(0) == {}) - assert(pcall(f('hjhg'))) - assert(pcall(f('1'))) - assert(pcall(f(1))) + + assert(not pcall(function() + f('sdads') + end)) + assert(not pcall(function() + f('1') + end)) + assert(not pcall(function() + f({0}) + end)) end return g From d4625bdbf6e4d8f7b4c72681ef0554f630c72d95 Mon Sep 17 00:00:00 2001 From: dmitrypenzar1996 Date: Sat, 23 May 2015 16:21:21 +0300 Subject: [PATCH 6/6] Solution shulf --- lua-shuf/credit.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lua-shuf/credit.lua b/lua-shuf/credit.lua index d85edbc..efaba4e 100644 --- a/lua-shuf/credit.lua +++ b/lua-shuf/credit.lua @@ -2,7 +2,7 @@ local function g(f) local n = 1000 local occurense = {} for i = 1, n do - n[i] = 0 + occurense[i] = 0 end local result = f(n) @@ -24,14 +24,12 @@ local function g(f) end assert(len == n) - assert(f(0) == {}) - assert(not pcall(function() f('sdads') end)) assert(not pcall(function() - f('1') + f('1ddd') end)) assert(not pcall(function() f({0})