Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is String Array Circle of Chained Words in least amount of characters?

Asked by
Filipalla 504 Moderation Voter
4 years ago
Edited 4 years ago

The Challenge:

Two words can be 'chained' if the last character of the first word is the same as the first character of the second word.

Given a list of words, determine if there is a way to 'chain' all the words in a circle.

Example:

Input: {"eggs", "karat", "apple", "snack", "tuna"}
Output: True

Print statements are not part of your character count. Your code should explicitly state if the array of words are chained or not.

Shortest and most valid answer within a week gets the solution.

If you ask in Discord or on the Website for a solution you are disqualified

I'll update the question with my implementation within due time.

Inspired by this post

Please do not moderate this Question as it doesn't break any rules

0
Note to self :3 People that have submitted solutions: Abandion, iAmMew24, ioannovna Filipalla 504 — 4y
0
This challenge is kinda vague, what if there's two words that start with the same letter? SoftlockedUnderZero 668 — 4y
0
nor whether two chain circles that appear but cant not merge with the other SoftlockedUnderZero 668 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

My solution.

please check to see if it passes through your testcases or not.

EDIT: Found a bug when in an odd number of elements, n-1 elements form a chain but the last one is something like "wow" where if it is flipped, it would still count as chain. This has been fixed now.

local words = {"apple", "estonia", "wow"}

local function isChained () 
    local copy = {};

    for i, word in pairs(words) do
        copy[i] = word:reverse();
    end

    table.sort(copy);
    table.sort(words);

    for i = 1, #words do
        if (words[i]:sub(1, 1) ~= copy[i]:sub(1, 1) or copy[i] == words[i]:reverse()) then
            return false;
        end
    end
    return true;
end

print(isChained());

0
Passed Filipalla 504 — 4y
Ad
Log in to vote
1
Answered by
Abandion 118
4 years ago
Edited 4 years ago

Can't be bothered to optimize it or make it shorter

--words = {"eggs", "karat", "apple", "no", "snack", "tuna"}
words = {"monkey","moan","neh","neat","hort","team","gah"}
--words = {"eggs", "sea", "apple"}
--words = {"apple", "earth", "roda"}

function intable(table, value)
    for i, v in pairs(table) do
        if type(v) == "table" and type(value) == "table" then
            for i2, v2 in pairs(v) do
                if value[i2] ~= v2 then return false end
            end
            for i2, v2 in pairs(value) do
                if v[i2] ~= v2 then return false end
            end

            return true
        else
            if v == value then
                return true
            end
        end
    end
    return false
end


function chain()
    foundlinks = {}

    for i, v in pairs(words) do
        for i2, v2 in pairs(words) do
            if i ~= i2 then 
                if string.sub(v,1,1) == string.sub(v2, #v2) and
                (not intable(foundlinks, {v, 1})) and (not intable(foundlinks, {v2, 2})) then
                    print("Found link: "..v2.. " and "..v)
                    table.insert(foundlinks, {v, 1})
                    table.insert(foundlinks, {v2, 2})
                end

                if string.sub(v,#v) == string.sub(v2,1,1) and 
                (not intable(foundlinks, {v2, 1})) and (not intable(foundlinks, {v, 2})) then
                    print("Found link: "..v.. " and "..v2)
                    table.insert(foundlinks, {v2, 1})
                    table.insert(foundlinks, {v, 2})
                end
            end
        end

        if #foundlinks == #words * 2 then return true end
    end

    return false --if we get this far
end

--print( intable({2, 5, 3, {3, 3}}, {3, 3}) )

print(chain(words))

Answer this question