the image above shows that the order in which it is checking is a invalid order and make it impossible to properly check and give a correct answer
(i am ordering the list of allergies by the original list)
Please include more information : your track, your code, it would make it easier to help with troubleshooting.
I did a test run on a different track with the value 216, and the output matched with what expected.
A list of:
['strawberries', 'tomatoes', 'pollen', 'cats']
and this is the original data for the list of allergens
- eggs (1)
- peanuts (2)
- shellfish (4)
- strawberries (8)
- tomatoes (16)
- chocolate (32)
- pollen (64)
- cats (128)
However in your screenshot, cats appear before tomatoes. So I’m pretty certain the problem is from your end and not the test itself.
What language is this? In Python, {...}
is a set which is unordered. This language might be different … but it’s hard to know without seeing the language.
Can you share your code as text (in a code block and not an image) along with the error as text?
The expected list matches the allergen ordering:
...
strawberries (8)
tomatoes (16)
...
pollen (64)
cats (128)
--!MY FUNCTIONS I HAVE TO ADD!--
function table.find(list,lookfor)
for i,v in pairs(list) do
if v == lookfor then
return i
else
end
end
end
function insert(list,position,value)
local newlist = {
}
if value then
newlist[position] = value
else
table.insert(newlist,value)
end
return newlist
end
--back to the code
local function format(list)
local alergies_list =
{
[1] = "eggs",
[2] = "peanuts",
[4] = "shellfish",
[8] = "strawberries",
[16] = "tomatoes",
[32] = "chocolate",
[64] = "pollen",
[128] = "cats",
}
local starting_list = list
local final_list = {}
for i,v in pairs(list) do
if table.find(list,v) then
local item = insert(final_list,table.find(list,v),v)
final_list[table.find(item,v)] = item[table.find(item,v)]
end
end
return final_list
end
local function list(score)
local alergies =
{
[1] = "eggs",
[2] = "peanuts",
[4] = "shellfish",
[8] = "strawberries",
[16] = "tomatoes",
[32] = "chocolate",
[64] = "pollen",
[128] = "cats",
}
local remaining_score = score
local list_compiled = {}
if alergies[score] then
return {alergies[score]}
else
for i=-score,0 do
i=i*-1
if alergies[i] and remaining_score>0 and remaining_score-i >= 0 then
list_compiled[i] = alergies[i]
--table.insert(list_compiled,alergies[i])
remaining_score=remaining_score-i
end
end
local final_list = {}
if list_compiled ~= {} then
for i,v in pairs(list_compiled) do
table.insert(final_list,v)
end
return format(final_list)
else
return {}
end
end
end
local function allergic_to(score, which)
local complete_list = {}
for i,v in pairs(which) do
end
end
return {
list = list,
allergic_to = allergic_to
}
i am specifically making it format the list but it does it out of order when I’m reordering them in list of original
the issue is that shouldnt matter when i am matching it to the order of original and returning that list re-ordered to match the original, it’s only mixing up the 2 and 4 positions every time (every other then 2 & 4 are correct)
for example on mine it swapped 2 to 4 and position 3 and 4(on the passed in) match the 2 and 3 (on the expected) meaning it’s swapping 2 and 4
using the ipairs would return the indexes which are not the correct things to use as the code extracts data out of order then re-orders it by the values,
ipairs would do it in the inverse order and make it not work
during the process this happens: lets say we are using the image:
it starts with { [128] = "cats", [64]="pollen",[16]="tomatoes",[8]="strawberries" }
then it orders it to { [1] = "strawberries" , [2]="tomatoes",[3]="pollen",[4] ="strawberries"}
(USING THE VALUES NOT INDEXES)
@glob The order that things are expected to be returned is the order that are defined in the README:
- eggs (1)
- peanuts (2)
- shellfish (4)
- strawberries (8)
- tomatoes (16)
- chocolate (32)
- pollen (64)
- cats (128)
As @glaxxie mentioned, your code is returning them in a different order to that. (you’re returning strawberries, pollen, cats, tomatoes).
I’m unclear if you’re suggesting that your order is correct and the README is wrong, or something else. Could you write one post that clearly explains why you think your order is correct please?
if you look in the code, it will show that inside the format() function it has that exact list as well as inside the list() function meaning it should be ordering it correct and not swapping them
So you are saying your code is wrong and you are asking for help with it? It’s unclear what you’re asking for because you started saying that the exercise has a bug in it, but your code is returning the list in the wrong order.
why would my code be ordering it out of order if its using the exact list?
or is the interpreter reading it wrong?
Well, probably because you have a bug in your code Have you tried what Glenn suggested as a starting point?
i can try it but where would i use ipairs inside of it, cause ipairs uses indexes instead of values
sence max messages reached up to date code:
--!MY FUNCTIONS I HAVE TO ADD!--
function table.find(list,lookfor)
for i,v in pairs(list) do
if v == lookfor then
return i
else
end
end
end
function insert(list,position,value)
local newlist = {
}
if value then
newlist[position] = value
else
table.insert(newlist,value)
end
return newlist
end
--back to the code
local function format(list)
local alergies_list =
{
[1] = "eggs",
[2] = "peanuts",
[4] = "shellfish",
[8] = "strawberries",
[16] = "tomatoes",
[32] = "chocolate",
[64] = "pollen",
[128] = "cats",
}
local starting_list = list
local final_list = {}
for i,v in pairs(list) do
if table.find(list,v) then
local item = insert(final_list,table.find(list,v),v)
final_list[table.find(item,v)] = item[table.find(item,v)]
end
end
return final_list
end
local function list(score)
local alergies =
{
[1] = "eggs",
[2] = "peanuts",
[4] = "shellfish",
[8] = "strawberries",
[16] = "tomatoes",
[32] = "chocolate",
[64] = "pollen",
[128] = "cats",
}
local remaining_score = score
local list_compiled = {}
if alergies[score] then
return {alergies[score]}
else
for i=-score,0 do
i=i*-1
if alergies[i] and remaining_score>0 and remaining_score-i >= 0 then
list_compiled[i] = alergies[i]
--table.insert(list_compiled,alergies[i])
remaining_score=remaining_score-i
end
end
local final_list = {}
if list_compiled ~= {} then
for i,v in pairs(list_compiled) do
table.insert(final_list,v)
end
return format(final_list)
else
return {}
end
end
end
local function allergic_to(score, which)
local complete_list = {}
for i,v in pairs(which) do
end
end
return {
list = list,
allergic_to = allergic_to
}
and i can’t look at them as i can’t complete it
Can you share the latest code?
Please use codeblock to share text (code and errors). Please do not use screenshots.
@glob at this point I would encourage you to have a look at some of the community solutions. Your code can be simplified quite a lot. Particularly, this exercise is a good opportunity to use Bitwise Operators.