Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

77%
+5 −0
Challenges Is it a near-anagram?

JavaScript (Node.js), 131 bytes Returns true and false g=(s,t,c=[...t])=>([...s].forEach(v=>(y=c.indexOf(v),c.splice(y,y>=0))),c) f=(s,t)=>(x=g(s,t).length)<2&&(y=g(t,s).l...

posted 3y ago by Moshi‭  ·  edited 3y ago by Moshi‭

Answer
#2: Post edited by user avatar Moshi‭ · 2021-02-18T19:26:17Z (about 3 years ago)
Update to make the solution valid
  • # [JavaScript (Node.js)], 129 bytes
  • Returns truthy and falsey values
  • ```javascript
  • g=(s,t,c=[...t])=>([...s].forEach(v=>(y=c.indexOf(v),c.splice(y,y>=0))),c)
  • f=(s,t)=>(x=g(s,t).length)<2&&(y=g(t,s).length)<2&&x+y
  • ```
  • ```
  • ADOBE ABODE -> 0 (proper anagram)
  • TUPLE TULIP -> 2 (near-anagram)
  • ABCDE DADBC -> 2 (two Ds)
  • BAR BARN -> 1 (one extra letter)
  • BARN BARREN -> false (too different)
  • ```
  • [Try it online!][TIO-kl91jx22]
  • ## Explanation
  • `g` computes a sort of set (Array?) difference between `t` and `s`, running through the characters of `s` and removing any matches in `t`, then returning the remaining characters in `t`.
  • `f` calls `g` to compute both `s-t` and `t-s` and checks that both are less than 2 (i.e. have only one letter difference either way). The third check, which gets returned, is the sum of the difference to check that there *is* a difference, which I needed to add in to make perfect anagrams return zero.[^1]
  • [^1]: Without the restriction on perfect anagrams being falsey, I could have removed all of the x and y logic to get a [116 char solution] (that also actually returns booleans!)
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kl91jx22]: https://tio.run/##dYxBC4IwAIXv/opOutEa0bUmbOkhEBXRU3iQOZchLtoQ/fWmdqmw03u89/Hdi67Q/Fk/zK5VpRhHSYBGBnFyxRibHBIXzE3nuFJPv@A30E3TQDiu21L0UQU6iDjWj6bmAgxocMkewmmCVrWoZkNP5FJxI1ppbvB0sO3JIYFB@mvst8PIVatVI3CjJKiAQ1nk@Q7aONSLmO9AeLR@iDSLg4VIs@ASrxGUnd8Oj3rsvEYwmsz/FOGfO/HDT2J8AQ "JavaScript (Node.js) – Try It Online"
  • [116 char solution]: https://tio.run/##dYxBC4IwAIXv/opOusEa0bUmbOohEA3RU3iQOachTtyQ/PVmdijCTu/x3sd3L8ZC86Hpzb5TpZhnSYBGBnFywxibHBIXvJrOcaWGoOA1GJdpIhw3XSkecQVGiDjWfdtwASY0ueQA4TJBq1pVi0GuiVvRSVOfj7YtgUH6M8xcdVq1ArdKggo4lMV@4KCdQ/2YBQ6EJ@uHSLNruBJpFl6uWwRl3tvhU595WwSjyetfIvpzJ0H0TcxP "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], 131 bytes
  • Returns true and false
  • ```javascript
  • g=(s,t,c=[...t])=>([...s].forEach(v=>(y=c.indexOf(v),c.splice(y,y>=0))),c)
  • f=(s,t)=>(x=g(s,t).length)<2&&(y=g(t,s).length)<2&&x+y
  • ```
  • [Try it online!][TIO-klb94gwz]
  • ## Explanation
  • `g` computes a sort of set (Array?) difference between `t` and `s`, running through the characters of `s` and removing any matches in `t`, then returning the remaining characters in `t`.
  • `f` calls `g` to compute both `s-t` and `t-s` and checks that both are less than 2 (i.e. have only one letter difference either way). Then it checks the sum of the difference is non-zero to check that there *is* a difference, which I needed to add in to make perfect anagrams false.[^1]
  • [^1]: Without the restriction on perfect anagrams being falsey, I could have removed all of the x and y logic to get a [116 char solution]
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-klb94gwz]: https://tio.run/##dYxBC4IwAIXv/YpOudEa0bUmbLlDIBqhp@ggcy5DNmlD9Neb2qXCTu/x3sf3yJrMimdZu402uex7RYBFDglyxRi7GyQ@GJu94cI8eSbuoBmmjghc6ly2cQEaiAS2dVUKCTrU@WQL4TDBRTGpRkNL1FRxJbVyd3jYrVaDQwGH7NfYrjt/2wujrakkrowCBfAoiwPuoaVHg5hxD8L94odI0nM4EUkans5zBGXHtyOgATvOEYxexn@I6M994dEn0b8A "JavaScript (Node.js) – Try It Online"
  • [116 char solution]: https://tio.run/##dYxBC4IwAIXv/opOusEa0bUmbOohEA3RU3iQOachTtyQ/PVmdijCTu/x3sd3L8ZC86Hpzb5TpZhnSYBGBnFywxibHBIXvJrOcaWGoOA1GJdpIhw3XSkecQVGiDjWfdtwASY0ueQA4TJBq1pVi0GuiVvRSVOfj7YtgUH6M8xcdVq1ArdKggo4lMV@4KCdQ/2YBQ6EJ@uHSLNruBJpFl6uWwRl3tvhU595WwSjyetfIvpzJ0H0TcxP "JavaScript (Node.js) – Try It Online"
#1: Initial revision by user avatar Moshi‭ · 2021-02-17T06:59:10Z (about 3 years ago)
# [JavaScript (Node.js)], 129 bytes

Returns truthy and falsey values

```javascript
g=(s,t,c=[...t])=>([...s].forEach(v=>(y=c.indexOf(v),c.splice(y,y>=0))),c)
f=(s,t)=>(x=g(s,t).length)<2&&(y=g(t,s).length)<2&&x+y
```
```
ADOBE ABODE   -> 0     (proper anagram)
TUPLE TULIP   -> 2     (near-anagram)
ABCDE DADBC   -> 2     (two Ds)
BAR   BARN    -> 1     (one extra letter)
BARN  BARREN  -> false (too different)
```

[Try it online!][TIO-kl91jx22]

## Explanation

`g` computes a sort of set (Array?) difference between `t` and `s`, running through the characters of `s` and removing any matches in `t`, then returning the remaining characters in `t`.

`f` calls `g` to compute both `s-t` and `t-s` and checks that both are less than 2 (i.e. have only one letter difference either way). The third check, which gets returned, is the sum of the difference to check that there *is* a difference, which I needed to add in to make perfect anagrams return zero.[^1]

[^1]: Without the restriction on perfect anagrams being falsey, I could have removed all of the x and y logic to get a [116 char solution] (that also actually returns booleans!)

[JavaScript (Node.js)]: https://nodejs.org
[TIO-kl91jx22]: https://tio.run/##dYxBC4IwAIXv/opOutEa0bUmbOkhEBXRU3iQOZchLtoQ/fWmdqmw03u89/Hdi67Q/Fk/zK5VpRhHSYBGBnFyxRibHBIXzE3nuFJPv@A30E3TQDiu21L0UQU6iDjWj6bmAgxocMkewmmCVrWoZkNP5FJxI1ppbvB0sO3JIYFB@mvst8PIVatVI3CjJKiAQ1nk@Q7aONSLmO9AeLR@iDSLg4VIs@ASrxGUnd8Oj3rsvEYwmsz/FOGfO/HDT2J8AQ "JavaScript (Node.js) – Try It Online"

[116 char solution]: https://tio.run/##dYxBC4IwAIXv/opOusEa0bUmbOohEA3RU3iQOachTtyQ/PVmdijCTu/x3sd3L8ZC86Hpzb5TpZhnSYBGBnFywxibHBIXvJrOcaWGoOA1GJdpIhw3XSkecQVGiDjWfdtwASY0ueQA4TJBq1pVi0GuiVvRSVOfj7YtgUH6M8xcdVq1ArdKggo4lMV@4KCdQ/2YBQ6EJ@uHSLNruBJpFl6uWwRl3tvhU595WwSjyetfIvpzJ0H0TcxP "JavaScript (Node.js) – Try It Online"