Skip to content

Conversation

@Lacrit
Copy link
Collaborator

@Lacrit Lacrit commented May 24, 2018

No description provided.

Copy link
Collaborator

@ssancho2 ssancho2 left a 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!

#include <boost/algorithm/string.hpp>


// input : string a , string b || output : bool
Copy link
Collaborator

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.

// 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)
Copy link
Collaborator

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.

std::unordered_map<char,int> count;

//count char occurences
for (size_t i = 0; i < a.length(); i++)
Copy link
Collaborator

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)

std::getline(std::cin, b);
remove_spaces(a);
remove_spaces(b);
std::cin >> c;
Copy link
Collaborator

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.

remove_spaces(a);
remove_spaces(b);
std::cin >> c;
std::cout << isAnagram(a, b, c) << std::endl;
Copy link
Collaborator

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>
Copy link
Collaborator

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;
Copy link
Collaborator

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)
{
Copy link
Collaborator

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?

template<class T>
void push(Node<T>** head, T data)
{
Node<T>* new_node =(Node<T>*) malloc(sizeof(Node<T>));
Copy link
Collaborator

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?

@Lacrit Lacrit closed this May 29, 2018
@Lacrit Lacrit reopened this May 29, 2018
Copy link
Collaborator

@ssancho2 ssancho2 left a 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";
Copy link
Collaborator

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);
Copy link
Collaborator

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);
Copy link
Collaborator

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);
Copy link
Collaborator

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)
Copy link
Collaborator

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;
Copy link
Collaborator

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
Copy link
Collaborator

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)
Copy link
Collaborator

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?

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