Coming from Python, I ended up writing the IsIsogram loop a bit differently

In Python, if you want to check if some element is in a list or array or string or anything, and see if it’s inside another list or array or string or etc, all you really have to do is write

for character in word:
    if character in seen:
        # do something

This would let you find repeated letters by making a new empty list called something like “Seen”, and loop over your Word (list of Characters). If that Character is in Seen already, then it’s not an isogram. If it isn’t in Seen, then you add it to Seen and move onto the next Character.

But in C++ writing something like that is not so simple haha. But after a lot of Googling I ended up making a couple of functions and writing this to try to get closer to Python.

First we make a function/method to do the equivalent of “if character in seen”:

#include <algorithm>
#include <list>
using namespace std;    // Using this is to cut down on having to write std:: everywhere.

/// See if Element is in SearchList.
/// https://stackoverflow.com/questions/24139428/check-if-element-is-in-the-list-contains
bool UBullCowCartridge::Contains(const TCHAR Element, list<TCHAR> SearchList)
{
    // Python says "if Element in SearchList". C++ says:
    return find(SearchList.begin(), SearchList.end(), Element) != SearchList.end();
}

This searches for the Element provided, from the beginning of SearchList to the end of Searchlist, and checks that it doesn’t equal the end of the Searchlist.
This is the equivalent of writing “If Element in SearchList”, giving us true or false.

Then we can implement it like this:

/// Check if Word is an isogram (does not contain more than 1 of each letter).
bool UBullCowCartridge::IsIsogram(FString Word)
{
    // Make empty list "Seen" for keeping track of our letters we're checking.
    list<TCHAR> Seen;
    // For Character in Word:
    for (TCHAR Character: Word) {
        // if Character in Seen:
        if (Contains(Character, Seen)) {
            return false;
        }
        // Otherwise append Character to the end of Seen list.
        Seen.push_back(Character);
    }
    // Making it here means we went through every Character in Word, and nothing repeated in Seen.
    return true;
}

In the end I was happier with reading that, but it did require additional includes, and making a dedicated method “Contains” to make it more readable when it was used in the IsIsogram method.

This might also be slower / more inefficient than the one in the course, I’m not sure to be honest. But working with such tiny words and data, I don’t think it matters. I’m not sure how much these sorts of changes might affect compile time as well. Either way, I just wanted something more familiar and thus more readable to me coming from Python, and it was good practice figuring it out.

2 Likes

You did a fantastic job. What do you like more python or C++?

Thank you so much!

It’s really hard to say… I’m so fresh into C++ with only a little exposure to C# before in Unity, that I feel really slow in it… But the fact that I have to be very deliberate in how I structure code as far as being very explicit with types and how data is handled, that does feel satisfying to get it working.

C++ is so much more visually busy that it’s a lot harder/slower for me to parse what I just wrote the day before, but again maybe that’s just extreme inexperience.

On the other hand, I can come up with ideas really fast in Python, iterate quickly and then I can type-hint after in Python if I want to be explicit to myself with types. But I don’t know if that speed is just because I have a year in python and a few days now in C++, or if python is just so easy to write. Most likely both right?

A part of me wishes Python was just as performant as C++ but with the same Pythonic syntax that way I could use Unity/Unreal and do all the coding in Python, but then I’m sure as I learn more C++ I’ll realize why that isn’t a possibility (especially since much of Python was written on C beneath it).

Either way I’m really enjoying C++ and I’m going to stick with it long-term because that’s the only way to make full use of Unreal, but I think reading and writing in Python is a lot more pleasant. But maybe that’ll change. It’s all still very fresh for me. Maybe in the end it won’t really matter, and I’ll love them all for different reasons.

2 Likes

I can relate to that so much. I messed with C# in the Unity courses and felt so lost. I had no idea what I was doing. Then I decided to dive dee into C++. That was a mess. I thought I got the hand of it but after working with a programmer I realized I didn’t know anything or really understand C++. Learning programming is difficult but not as hard as learning how C++ works. Now I do realize that once I understand C++ I’ll be able to do so much more than let’s say Python or C#. They all have their strengths and weaknesses. I love Unreal so that’s why I wanted to go all in on C++.

2 Likes

Privacy & Terms