-1

I was wondering how to check if a number is a permutation of another number in Javascript.

Ex:

perm(1234,2413); ---> True
perm(154,154); ---> True
perm(101,011); ---> False
perm(501,104); --->False

Any help would be appreciated :)

  • 1
    why is the first line -> `perm(1234,2413)` true ? – caramba May 18 '18 at 16:54
  • 1
    Stack Overflow isn't a code writing service. We will help you, but we won't write your code for you. Show us what you've tried and describe where, specifically, you're stuck. –  May 18 '18 at 16:54
  • 2
    @caramba The second argument is an anagram of the first. –  May 18 '18 at 16:55
  • @Amy thanks! But why isn't the third `perm(101,011)` also true then? Looks also like a naga ram to me – caramba May 18 '18 at 17:02
  • @caramba the leading `0` is getting dropped, and the number is treated as `11`, which isn't an anagram of `101`. –  May 18 '18 at 17:03
  • I see, thanks @Amy! learned something new today – caramba May 18 '18 at 17:04
  • 1
    @caramba Actually there are two issues at play there. The first is what I stated. The second is more subtle. Wrap your head around this: `099 === 99` and `011 === 11` do *not* give the same result (true and false, respectfully). A leading `0` with every digit less than 8 makes JS think it's an octal number. `011` in octal is `9`. Try `011 === 9`. –  May 18 '18 at 17:12
  • @Amy very interesting! I like! thanks again – caramba May 18 '18 at 17:28

2 Answers2

1

I would do this just by converting each input into an array of characters, then sorting them, and then just checking for equality. If two inputs are permutations of each other, then sorting them will put them into the same order, at which point it's easy to see if they're the same:

function perm(n1, n2) {
  return String(n1).split("").sort().join("") === String(n2).split("").sort().join("");
}

console.log(perm(1234,2413)); // ---> True
console.log(perm(154,154)); // ---> True
console.log(perm(101,011)); // ---> False
console.log(perm(501,104)); // --->False
CRice
  • 29,968
  • 4
  • 57
  • 70
-4

Here's a snippet of code that converts the two numbers to strings, then checks if the second number is contained in an array of permutations of the first number.

The permutation function is from this question: Permutations in JavaScript?. I did not duplicate the answer, I simply included a piece of code available online that permutes strings and I cited it appropriately. The other function and all of the comments/explanations are mine.

var permArr = [],
  usedChars = [];

function permute(input) {//Returns an array of all permutations of a string
  var i, ch, chars = input.split("");
  for (i = 0; i < chars.length; i++) {
    ch = chars.splice(i, 1);
    usedChars.push(ch);
    if (chars.length == 0)
      permArr[permArr.length] = usedChars.join("");
    permute(chars.join(""));
    chars.splice(i, 0, ch);
    usedChars.pop();
  }
  return permArr
}

console.log(permute("HI!"))
//Returns permutations of "HI!"

function perm(firstnum, secondnum) {//My original code!
  if (permute(firstnum.toString()).includes(secondnum.toString())) {//test if the second number, converted to a string, is in an array of permutations of the first number
    return true
  } else {
    return false
  }
}
//Converts both numbers to strings, checks if second number is a permutation of first number
console.log(perm(123, 321))//Returns true
Ben Gubler
  • 1,393
  • 3
  • 18
  • 32
  • 2
    Don't take answers from other questions and post them as your own. Either vote to close this as a dupe of that question, or make a comment with a link. – j08691 May 18 '18 at 16:59
  • Downvoters: please explain why when you downvote. I personally have been really helped out sometimes by other people's code, and have learned new techniques which I couldn't have figured out on my own. – Ben Gubler May 18 '18 at 17:00
  • Wow, I didn't notice this answer had been taken from another question. If you're going to take someone elses answer, make it obvious what you're doing and submit your answer as a community wiki so you avoid the appearance of stealing rep. Voting to close as a dupe. –  May 18 '18 at 17:01
  • @j08691 this is not a duplicate question and my answer isn't a dupe of theirs. I simply borrowed the `permute` function as a function that outputs an array with all permutations of a string, and cited it appropriately. The `perm` function is mine. – Ben Gubler May 18 '18 at 17:02
  • 2
    Not only does your answer not explain what any of the code is doing, it looks pretty inefficient. – dave May 18 '18 at 17:02
  • All your "original" code does is call the function you "borrowed" from another answer – j08691 May 18 '18 at 17:10
  • @Amy I didn't attempt to conceal that I was using a function from online, and I even included a link, even though the code wasn't even an answer to that SO question it was part of the question itself. If using code that is in print elsewhere is plagiarism, then anyone who's ever read a bit of documentation is plagiarizing. – Ben Gubler May 18 '18 at 17:12
  • @j08691 if you went to the link, you would see that I actually didn't borrow it from another answer, I borrowed code that was part of the question. – Ben Gubler May 18 '18 at 17:13
  • @BenGubler I didn't say you did. I said to "avoid the **appearance** of stealing rep". –  May 18 '18 at 17:13
  • @Amy how do you submit an answer as a community wiki? I don't want to experience this negative rep change again :) – Ben Gubler May 18 '18 at 17:16
  • @BenGubler When you submit an answer, there's a small checkbox below the text entry box, aligned to the right. I think that box can only be checked on new answers, but I've only ever used it once, myself. AFAIK moderators can convert existing answers to Community Wiki. –  May 18 '18 at 17:18
  • @BenGubler https://meta.stackexchange.com/questions/11740/what-are-community-wiki-posts –  May 18 '18 at 17:19
  • @Amy okay, thanks. Just made this question a community wiki just in case any other people downvote... – Ben Gubler May 18 '18 at 17:58