Given a phone number of integer length N (1 ≤ N ≤ 10) as a number string (e.g. 555-555-5555 would be passed as "5555555555"), return an array of al...
For further actions, you may consider blocking this person and/or reporting abuse
Huh, this one was harder than I expected it to be... that probably means I missed something 🤣
Also, TIL that setting a character of a string in JS with square brackets doesn't throw an error - but also doesn't do anything! example:
Solution in javascript:
Strings in javascript are immutable, so even if you change a character using the
[]
notation, it always returns a new string.This instead work as you expected
Makes sense - thanks! (forgot (or never really knew?) that strings in javascript were immutable)
Attempting to learn and understand more functional paradigms. Is this right way to solve this problem functionally?
Way to go buddy!
From my perspective, to format the phone number in 3 segments, I would have used this instead of the 3 calls you made.
This leverage the regular expressions to match three characters at most. I know regular expressions is not the easiest thing to use but in this case it can be quite handy. As it returns an array, joining the items will produce a string back so the signature of the function can be written as follow in a purely functional programming language like PureScript.
Unfortunately, currying (partially applying parameters) is not built-in in JavaScript. You could implement your own curry function and use it on all your future function definitions. But this would be overkill for this challenge. But if you would ask me how I would implement a currying function in JavaScript, here is my take.
Last tip, when creating a function, try to put the data structure at the end, that way you can construct partial application of your function without pain, even in JavaScript. In this case, this means puting the phone number to format at the very end of the parameter's list in your function definition. Hope that helps you in your functional programing journey pal.
Hey thanks for the response I will look at regexes next time for this kind of problem. Also I thought currying was supported in ES6?
via
What you did is indeed currying but it is not transparent in the sense that if you want to pass all three arguments at once you will have to make three calls. The curry implementation allows you to either pass all arguments at once like a regular function call or partially calling the function. I kinda dislike calling my function multiple times and the only two reasons are aesthetic and the fact that it is less natural to define functions like that but arrow functions syntax makes it a little bit cleaner.
Here goes some ugly code!
It works like that:
One line JS generate all possible neighbor number for any position.
My take on this challenge with JS
My solution in js
Simple ruby solution(with edge cases):
I liked the "any number can differ by 1" approach more - it's a but more work :)
Rust:
And a test (I usually omit them from my submissions, but I think they have a place here):
My take at the challenge written in Elm.
Try it online.
Python one-liner to the rescue again. (Horribly formatted this time tho)
f = lambda x: print(*[x[:len(x)-1]+str(int(x[len(x)-1])+1), x[:len(x)-1]+str(int(x[len(x)-1])-1)])
🤔
Most people interpreted that as only a single number could vary at a time, others took the example too literally as the full number +- 1...
So I thought...
What if any numberS (plural) means any number of digits can vary...?
This was a little harder, at least to me it was.
phone_number.go
phone_number_test.go
python
So... something like this?