-
Notifications
You must be signed in to change notification settings - Fork 2
Lacrit 1 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
ssancho2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very concise and readable!
Lacrit/assignment1/q1.cpp
Outdated
| #include <boost/algorithm/string.hpp> | ||
|
|
||
|
|
||
| // input : string a , string b || output : bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the comment to the function it applies to.
Lacrit/assignment1/q1.cpp
Outdated
| // input : string a , string b || output : bool | ||
| // determine if one is an anagram of another | ||
|
|
||
| std::unordered_map<char, int> count_occurences(std::string& a, const bool c) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameters can use better names :)
Specially bools, that could be one thing or the exact opposite.
Lacrit/assignment1/q1.cpp
Outdated
| std::unordered_map<char,int> count; | ||
|
|
||
| //count char occurences | ||
| for (size_t i = 0; i < a.length(); i++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do: for (char c : a) { ... } (if you rename the bool parameter, of course)
Lacrit/assignment1/q1.cpp
Outdated
| std::getline(std::cin, b); | ||
| remove_spaces(a); | ||
| remove_spaces(b); | ||
| std::cin >> c; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: You could print a line explaining to the user to enter two lines and then if he/she wants case sensitive or case insensitive anagram checking. For the last one it'll be better to use a char input, like 'c' for case sensitive and 'i' for case insensitive.
Lacrit/assignment1/q1.cpp
Outdated
| remove_spaces(a); | ||
| remove_spaces(b); | ||
| std::cin >> c; | ||
| std::cout << isAnagram(a, b, c) << std::endl; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Similarly for the output here, a textual output like "They are anagrams"/ "They aren't anagrams". Or even "Yes" / "No" :)
| #include <iostream> | ||
|
|
||
|
|
||
| template<class T> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A note on interface design, letting the user modify next freely it's risky.
| push(&head, 'r'); | ||
|
|
||
| std::cout << findNode(head, 3) << std::endl; | ||
| return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to free all nodes.
|
|
||
| template<class T> | ||
| T findNode (Node<T> *listHead, int n) | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if n is out of range?
What if the list is empty?
Lacrit/assignment1/q2.cpp
Outdated
| template<class T> | ||
| void push(Node<T>** head, T data) | ||
| { | ||
| Node<T>* new_node =(Node<T>*) malloc(sizeof(Node<T>)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why malloc and not new?
ssancho2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the comments are style/design, and optional. The main/simple corrections are marked with '(*)'
| std::cout << isAnagram(a, b, c) << std::endl; | ||
| std::cout << "Case Sensitive?(c|i) (c => sensitive || i => insensitive)" << std::endl; | ||
| std::cin >> case_sensitive; | ||
| const std::string word = "Words aren`t anagrams"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(*) word is a weird name for the message.
| std::cout << "Case Sensitive?(c|i) (c => sensitive || i => insensitive)" << std::endl; | ||
| std::cin >> case_sensitive; | ||
| const std::string word = "Words aren`t anagrams"; | ||
| remove_spaces(a); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(*) Please don't change the indentation level in the same block.
| std::cin >> case_sensitive; | ||
| const std::string word = "Words aren`t anagrams"; | ||
| remove_spaces(a); | ||
| remove_spaces(b); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both remove_space calls and the check if they are the same length should be part of isAnagram
| remove_spaces(b); | ||
| if (a.length() == b.length()) | ||
| { | ||
| bool res = isAnagram(a, b, case_sensitive); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(*) case_sensitive is a char ('c' or 'i'), the function expects a bool.
| { | ||
| return i; | ||
| } | ||
| void set_next(Node *new_next) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user can still modify next :)
One solution is to make a LinkedList class, and mark is as a friend of Node, that way only there you can modify it.
| template<class T> | ||
| T findNode (Node<T> *listHead, int n) | ||
| { | ||
| if (listHead == NULL) return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nullptr is usually preferred to NULL, since nullptr is typed, while NULL is a int with value 0
Also, returning 0 severely limits the types T can be :)
| new_node->next = (*head); | ||
| //Node<T>* new_node =(Node<T>*) malloc(sizeof(Node<T>)) | ||
| //Just left the upper row for reference. Correction: | ||
| Node<T>* new_node = new Node<T>(); // no need to pass the size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you add a constructor to Node, you can replace this 3 lines with:
Node<T>* new_node = new Node<T>(data, *head);
Or even the whole function with:
head = new Node<T>(data, *head);
:)
| ptr1 = ptr2 = listHead; // set the pointers to point to the list head initially | ||
|
|
||
| while(ptr1->next != NULL) // keep looping until we reach the tail (next will be NULL for the last node) | ||
| while(ptr1->get_next() != NULL) // keep looping until we reach the tail (next will be NULL for the last node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when n is less than 0 or bigger (or equal) to the list size?
No description provided.