Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

3 comments

0

Note to self :3 People that have submitted solutions: Abandion, iAmMew24, ioannovna

0

This challenge is kinda vague, what if there's two words that start with the same letter?

0

nor whether two chain circles that appear but cant not merge with the other

2 answers

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());

1 comment

0

Passed

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))

This discussion is preserved as it was. New answers, comments, and votes are closed.