Last active
March 12, 2016 06:56
-
-
Save abhinaykumar/07af4272df2d497d8875 to your computer and use it in GitHub Desktop.
challenge is to add ',' at the end of every number which is not pin-code. Pincodes are the number lesser or equal to 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# challenge is to add ',' at the end of every number which is not pin-code. Pincodes are the number lesser or equal to 5. | |
# Also, make sure they all are one space apart. | |
# ex: "123 23424324 34534 2342442234" | |
# function should return: "123 23424324, 34534 2342442234," | |
# | |
a = "1234 23434242 232342442 345 2342341234" | |
a.scan(/[\w']+/) do |w| | |
if w.length > 5 | |
b << "#{w}," | |
next | |
end | |
b << w | |
end | |
=> "1234 23434242 232342442 345 2342341234" | |
b | |
=> ["1234", "23434242,", "232342442,", "345", "2342341234,"] | |
b.join(' ') | |
=> "1234 23434242, 232342442, 345 2342341234," |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment