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
92 changes: 90 additions & 2 deletions c-gaps/credit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,96 @@
#include <lauxlib.h>

// implement it here
#include <stdlib.h>


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;
}
12 changes: 6 additions & 6 deletions c-gaps/spec.lua
Original file line number Diff line number Diff line change
@@ -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({''}))
Expand All @@ -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({'-', '-'}))
Expand All @@ -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()
Expand All @@ -41,15 +41,15 @@ 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()
f({{1,2,3}, {}})
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()
Expand Down
36 changes: 35 additions & 1 deletion lua-shuf/credit.lua
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions lua-shuf/spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down