Skip to content

Conversation

@altay9
Copy link
Collaborator

@altay9 altay9 commented May 16, 2021

@ErdemT09
Copy link
Collaborator

Instead of just calling it "-Faster", this time, it's better to call it "-Array" and the previous solution "-HashMap".

@ErdemT09
Copy link
Collaborator

ErdemT09 commented May 17, 2021

Uising 3 valued Java-logic ( Optional<Boolean> )might also be helpful here:
EDIT: We can just do it with normal booleans:

boolean sawSingle = false;
            for (int key = 0; key < map.length; key++) {
                if (map[key] % 2 == 1) {
                    if (sawSingle) {
                        return false;
                    }
                    sawSingle = true;
                }
            }
            return true;
        }

@altay9
Copy link
Collaborator Author

altay9 commented May 17, 2021

Instead of just calling it "-Faster", this time, it's better to call it "-Array" and the previous solution "-HashMap".

Ok, this is done.

@ErdemT09
Copy link
Collaborator

Uising 3 valued Java-logic ( Optional )might also be helpful here:
EDIT: We can just do it with normal booleans:

boolean sawSingle = false;
            for (int key = 0; key < map.length; key++) {
                if (map[key] % 2 == 1) {
                    if (sawSingle) {
                        return false;
                    }
                    sawSingle = true;
                }
            }
            return true;
        }

What do you think about the idea here? Would it work?

@altay9
Copy link
Collaborator Author

altay9 commented May 17, 2021

Uising 3 valued Java-logic ( Optional )might also be helpful here:
EDIT: We can just do it with normal booleans:

boolean sawSingle = false;
            for (int key = 0; key < map.length; key++) {
                if (map[key] % 2 == 1) {
                    if (sawSingle) {
                        return false;
                    }
                    sawSingle = true;
                }
            }
            return true;
        }

What do you think about the idea here? Would it work?

Yeah, of course. Nice idea:

image

Even if, Optional class is not that useful in this context, It would be something like that:


           for (int key = 0; key < map.length; key++) {
               if (map[key] % 2 == 1) {
                  if (anExternalBooleanParameter.orElse(false)) {
                           return false;
                      }
                  anExternalBooleanParameter = Optional.of(true);
               }
           }
           return true;

In what kind of context would an Optional be meaningful?
In some cases like; reading the value of anExternalBooleanParameter (or another boolean) from a database or getting it via a REST service; we might not know if it is empty.
For example, It would have been created like this and sent our API method as a parameter in an external service:

Optional<Boolean> anExternalBooleanParameter= Optional.empty();

In that case, we should have given a default value using orElse.

@ErdemT09
Copy link
Collaborator

Shall we add the boolean approach to the solution then?

@altay9
Copy link
Collaborator Author

altay9 commented May 19, 2021

Shall we add the boolean approach to the solution then?

Of course. I will add it too.

@altay9
Copy link
Collaborator Author

altay9 commented May 20, 2021

Shall we add the boolean approach to the solution then?

Oke, it is done.
https://github.com/spiralgo/algorithms/pull/251/files#diff-0e1e5a4f3753c0ec2228dc6e5d217756506e5cf50310c02360bfac10229a19f6

@ErdemT09
Copy link
Collaborator

Approved then.

@altay9 altay9 merged commit 908a5b3 into master May 20, 2021
@altay9 altay9 deleted the PalindromePermutation branch May 21, 2021 00:11
@ErdemT09 ErdemT09 mentioned this pull request Jun 13, 2021
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