Skip to content

Mohammed almakki exercies#88

Open
MohamedAlmaki wants to merge 7 commits into
mainfrom
mohammed_almakki_exercies
Open

Mohammed almakki exercies#88
MohamedAlmaki wants to merge 7 commits into
mainfrom
mohammed_almakki_exercies

Conversation

@MohamedAlmaki

Copy link
Copy Markdown

This PR contains the solution of c++ exercise 1 and 2

@Harrietbrown Harrietbrown left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exercise 1


std::vector<std::string> tokenizeFile(const std::string &file)
{
std::regex re("[-\\s]");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the inent of this that the experssion match a hyphen of a whitespace, but a double backslash exits the special character.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex is used to split the string by two delimiters, the space, and the hyphen. It will match a space or a hyphen. After that, the expression is used in a reverse way, to get all the words that don't match it i.e. the split words.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Hyphen is also a special character and will need to be exited as well

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will be removed. For example, the string "lines are hard-coded" will be transformed into a vector of ["lines", "are", "hard", "coded"] by this expression.

word.begin(), word.end(), word.begin(),
[](unsigned char c)
{ return std::tolower(c); });
word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task specifies only needing to consider .,?'"!(): you might have considered using regex to strip these away and replacing them with a whitespace at the same time as doing the hyphen.

@MohamedAlmaki MohamedAlmaki Dec 16, 2022

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understood, the comparison should be punctation insensitive. So for example, "what's" should be equal to "whats" but replacing it with whitespace will result in the word not being included (not longer than 4). So in this line, I just remove every punctuation mark.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I think you may be right

word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());
if (word.length() > 4)
{
wordsCounts[word]++;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should have a check to see if word is currently in the wordCounts map

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, why is it needed?
If there is a get operation, then I need a check, but this is just used as a counter.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimatly it is not required for it to work, but it is better practice to have the check when performing an action on an object in a map where it is not certain that the object will exist.

{
wordsCountsPairs.push_back(pair);
}
std::sort(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good sorting process.

@MialLewis MialLewis left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job - a few comments on the second exercise. Some of the comments apply in more than one place, but I've only pointed out the first occurrence.

Comment thread exercises-cpp/mohammed_almakki/ex02_oo_basics/src/Shape.h Outdated
Shape(int noOfSides) : noOfSides{noOfSides} {}

private:
int noOfSides;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this exercise, this could be const as there's no requirement to mutate the shapes after construction.

Comment thread exercises-cpp/mohammed_almakki/ex02_oo_basics/src/Shape.h Outdated
int noOfSides;
};

class Square : public Shape

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have been tempted to have square inherit from rectangle to avoid some code duplication. Override functions where necessary.

Comment thread exercises-cpp/mohammed_almakki/ex02_oo_basics/src/Shape.h Outdated
Comment thread exercises-cpp/mohammed_almakki/ex02_oo_basics/src/Shape.h Outdated
return "Square";
}

int getSidesCount()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have this defined in the Shape parent class, simply returning its member noOfSides? We then would not have to redefine it in every child.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is really bad. Doesn't know why I did it like this.

Comment thread exercises-cpp/mohammed_almakki/ex02_oo_basics/src/ShapeSorter.h Outdated

void printShapesWithSides(const int &sidesCount)
{
for (auto shape : shapes)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely needless complication in this instance, but could be interesting to see if you could reduce code duplication (use of function pointers?)

@MohamedAlmaki MohamedAlmaki Dec 19, 2022

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can make a general function that prints shapes that meet a specific condition. The function will accept a lambda filter function as a parameter. The lambda function will accept a parameter of type shape and return a Boolean value. Also, we can get rid of the for loop and use for_each. But I think it is too complex for this small example.

return left->getArea() > right->getArea();
});

for (auto shape : sortedShapes)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the above - try to refactor to reduce the code duplication here.

Change types to enum instead of raw strings
Reduce code duplication by abstracting a general printing function

@MialLewis MialLewis left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvements - your solution using the lambda filter functions is really neat.

@MohamedAlmaki

MohamedAlmaki commented Dec 19, 2022

Copy link
Copy Markdown
Author

Thanks for the improvements - your solution using the lambda filter functions is really neat.

Thanks for your great feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants