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
5 years ago
Edited 5 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:

1Input: {"eggs", "karat", "apple", "snack", "tuna"}
2Output: 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 — 5y
0
This challenge is kinda vague, what if there's two words that start with the same letter? SoftlockedUnderZero 668 — 5y
0
nor whether two chain circles that appear but cant not merge with the other SoftlockedUnderZero 668 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 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.

01local words = {"apple", "estonia", "wow"}
02 
03local function isChained ()
04    local copy = {};
05 
06    for i, word in pairs(words) do
07        copy[i] = word:reverse();
08    end
09 
10    table.sort(copy);
11    table.sort(words);
12 
13    for i = 1, #words do
14        if (words[i]:sub(1, 1) ~= copy[i]:sub(1, 1) or copy[i] == words[i]:reverse()) then
15            return false;
View all 21 lines...
0
Passed Filipalla 504 — 5y
Ad
Log in to vote
1
Answered by
Abandion 118
5 years ago
Edited 5 years ago

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

01--words = {"eggs", "karat", "apple", "no", "snack", "tuna"}
02words = {"monkey","moan","neh","neat","hort","team","gah"}
03--words = {"eggs", "sea", "apple"}
04--words = {"apple", "earth", "roda"}
05 
06function intable(table, value)
07    for i, v in pairs(table) do
08        if type(v) == "table" and type(value) == "table" then
09            for i2, v2 in pairs(v) do
10                if value[i2] ~= v2 then return false end
11            end
12            for i2, v2 in pairs(value) do
13                if v[i2] ~= v2 then return false end
14            end
15 
View all 57 lines...

Answer this question