Calculate number of possible messages from a coded string
up vote
-1
down vote
favorite
Question
Alphabets are assigned numbers like -
a = 1
b = 2
c = 3
.
.
z = 26
Given a number like 1234
as input calculate count of possible different combination of character out of this string.
Example
input "123"
output = 3 {(1,2,3), (12, 3),(1,23) }
input = "0123"
output = 0 // as 0 is not a valid number for this case a = 1
input = ""
output = 1
Please review this scala code for this problem -
import scala.language.postfixOps
import scala.collection.mutable.HashMap
object Decode extends App {
def validTwoDigit(number: Int) : Boolean = (9 < number) && (27 > number)
def countMessage(message: String, lookup: HashMap[String, Int]): Int = {
if (message.isEmpty) 1
else if (message.head == '0') 0
else if (lookup contains message) lookup(message)
else {
val count = countMessage(message.drop(1), lookup) +
( if (validTwoDigit(message.take(2).toInt)) countMessage(message.drop(2), lookup) else 0 )
lookup += (message -> count)
count
}
}
val message = args(0)
println(message)
println(countMessage(message, HashMap[String, Int]()))
}
programming-challenge interview-questions scala combinatorics dynamic-programming
add a comment |
up vote
-1
down vote
favorite
Question
Alphabets are assigned numbers like -
a = 1
b = 2
c = 3
.
.
z = 26
Given a number like 1234
as input calculate count of possible different combination of character out of this string.
Example
input "123"
output = 3 {(1,2,3), (12, 3),(1,23) }
input = "0123"
output = 0 // as 0 is not a valid number for this case a = 1
input = ""
output = 1
Please review this scala code for this problem -
import scala.language.postfixOps
import scala.collection.mutable.HashMap
object Decode extends App {
def validTwoDigit(number: Int) : Boolean = (9 < number) && (27 > number)
def countMessage(message: String, lookup: HashMap[String, Int]): Int = {
if (message.isEmpty) 1
else if (message.head == '0') 0
else if (lookup contains message) lookup(message)
else {
val count = countMessage(message.drop(1), lookup) +
( if (validTwoDigit(message.take(2).toInt)) countMessage(message.drop(2), lookup) else 0 )
lookup += (message -> count)
count
}
}
val message = args(0)
println(message)
println(countMessage(message, HashMap[String, Int]()))
}
programming-challenge interview-questions scala combinatorics dynamic-programming
2
Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
– 200_success
2 days ago
fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
– vikrant
2 days ago
1
I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
– 200_success
2 days ago
This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
– vikrant
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Question
Alphabets are assigned numbers like -
a = 1
b = 2
c = 3
.
.
z = 26
Given a number like 1234
as input calculate count of possible different combination of character out of this string.
Example
input "123"
output = 3 {(1,2,3), (12, 3),(1,23) }
input = "0123"
output = 0 // as 0 is not a valid number for this case a = 1
input = ""
output = 1
Please review this scala code for this problem -
import scala.language.postfixOps
import scala.collection.mutable.HashMap
object Decode extends App {
def validTwoDigit(number: Int) : Boolean = (9 < number) && (27 > number)
def countMessage(message: String, lookup: HashMap[String, Int]): Int = {
if (message.isEmpty) 1
else if (message.head == '0') 0
else if (lookup contains message) lookup(message)
else {
val count = countMessage(message.drop(1), lookup) +
( if (validTwoDigit(message.take(2).toInt)) countMessage(message.drop(2), lookup) else 0 )
lookup += (message -> count)
count
}
}
val message = args(0)
println(message)
println(countMessage(message, HashMap[String, Int]()))
}
programming-challenge interview-questions scala combinatorics dynamic-programming
Question
Alphabets are assigned numbers like -
a = 1
b = 2
c = 3
.
.
z = 26
Given a number like 1234
as input calculate count of possible different combination of character out of this string.
Example
input "123"
output = 3 {(1,2,3), (12, 3),(1,23) }
input = "0123"
output = 0 // as 0 is not a valid number for this case a = 1
input = ""
output = 1
Please review this scala code for this problem -
import scala.language.postfixOps
import scala.collection.mutable.HashMap
object Decode extends App {
def validTwoDigit(number: Int) : Boolean = (9 < number) && (27 > number)
def countMessage(message: String, lookup: HashMap[String, Int]): Int = {
if (message.isEmpty) 1
else if (message.head == '0') 0
else if (lookup contains message) lookup(message)
else {
val count = countMessage(message.drop(1), lookup) +
( if (validTwoDigit(message.take(2).toInt)) countMessage(message.drop(2), lookup) else 0 )
lookup += (message -> count)
count
}
}
val message = args(0)
println(message)
println(countMessage(message, HashMap[String, Int]()))
}
programming-challenge interview-questions scala combinatorics dynamic-programming
programming-challenge interview-questions scala combinatorics dynamic-programming
edited 2 days ago
asked 2 days ago
vikrant
214
214
2
Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
– 200_success
2 days ago
fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
– vikrant
2 days ago
1
I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
– 200_success
2 days ago
This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
– vikrant
2 days ago
add a comment |
2
Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
– 200_success
2 days ago
fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
– vikrant
2 days ago
1
I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
– 200_success
2 days ago
This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
– vikrant
2 days ago
2
2
Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
– 200_success
2 days ago
Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
– 200_success
2 days ago
fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
– vikrant
2 days ago
fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
– vikrant
2 days ago
1
1
I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
– 200_success
2 days ago
I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
– 200_success
2 days ago
This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
– vikrant
2 days ago
This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
– vikrant
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208556%2fcalculate-number-of-possible-messages-from-a-coded-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
– 200_success
2 days ago
fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
– vikrant
2 days ago
1
I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
– 200_success
2 days ago
This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
– vikrant
2 days ago