My GetValidWords

I imported my words from the text file following the optional instructions.

I used the for(Type : Collection) loop for my method

TArray<FString> UBullCowCartridge::GetValidWords(TArray<FString> WordList) const
{
    TArray<FString> ValidWords;
	
	for (FString WordFound : WordList)
	{
		if(IsIsogram(WordFound) && WordFound.Len() >= 4 && WordFound.Len() <=8)
		{
			ValidWords.Emplace(WordFound);
		}
	}

    return ValidWords;
}

This also works fine.

1 Like

here is my code

TArray<FString> UBullCowCartridge::GetValidWords(TArray<FString> RawWords) const {
    //Empty array of string
    TArray<FString> ValidWords;

    for (int32 i = 0; i < RawWords.Num(); i++)
    {
        // PrintLine(TEXT("Print out word %i is %s"), i, *Words[i]);
        if (Words[i].Len() >= 4 && Words[i].Len() <= 8 && IsIsogram(Words[i]) == false) {
            //Append to array
            ValidWords.Emplace(Words[i]);
            
            // PrintLine(TEXT("it's length is: %i, the word is: %s."), Words[i].Len(), *Words[i]);
        }
    }
    return ValidWords;
}
1 Like

As i understand, each time you call .Len() C++ perform new operation and recalculate the length of given string. And it’s true for taking word from list with WordList[Index]. So i pass this two operations to variables, and in my code this operations calls once per word.

TArray<FString> UBullCowCartridge::GetValidWords(TArray<FString> WordList) const
{
	TArray<FString> ValidWords;

	for (int32 Index = 0; Index < WordList.Num(); Index++)
	{
		const FString CurrentWord = WordList[Index];
		const int32 WordLength = CurrentWord.Len();

		if (WordLength >= 4 && WordLength <= 8 && IsIsogram(CurrentWord))
		{
			ValidWords.Emplace(CurrentWord);
		}
	}

	return ValidWords;
}

And this seems more readable for me XD

I imported my words from the text file following the optional instructions.
I used the for(Type : Collection) loop for my method

  • If we combine our ways we can get nice readable code
TArray<FString> UBullCowCartridge::GetValidWords(TArray<FString> WordList) const
{
	TArray<FString> ValidWords;

	for (FString CurrentWord : WordList)
	{
		int32 Length = CurrentWord.Len();
		if (IsIsogram(CurrentWord) && Length >= 4 && Length <= 8)
		{
			ValidWords.Emplace(CurrentWord);
		}
	}

	return ValidWords;
}
1 Like

Yeah Fair Play. The Collection Loop is nice code to read. When you need to be selective of the index then you would need the “for loop” to be optimized. in this case we need to read the entirety to check length and type.

1 Like