❔ Need help translating this psuedocode

Hello beginner here for my assignment of translating some psuedocode. i am not sure how to check if a list is empty in the while loop
4 Replies
Angius
Angius15mo ago
I think it's supposed to merge two lists Not two integers So both left and right should be lists of... something
theo
theo15mo ago
Couldn't you make it generic and contrain T to INumber<T> and make the two Lists be List<T>?
static List<T> Merge<T>(List<T> left, List<T> right)
where T : INumber<T>
{
List<T> result = new();

while (left.Any() && right.Any())
{
if (left.First() <= right.First())
{
result.Add(left.First());
left.Remove(left.First());
}
else
{
result.Add(right.First());
right.Remove(right.First());
}
}

return result;
}
static List<T> Merge<T>(List<T> left, List<T> right)
where T : INumber<T>
{
List<T> result = new();

while (left.Any() && right.Any())
{
if (left.First() <= right.First())
{
result.Add(left.First());
left.Remove(left.First());
}
else
{
result.Add(right.First());
right.Remove(right.First());
}
}

return result;
}
Perhaps something like this
scoat2
scoat215mo ago
Yes I agree. Then to check if a list is empty just do if(left.Count == 0) Though it is kinda strange they decided to name the two input lists “left” and “right” and not something more descriptive, like listA and listB, imo
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.