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
Nov 27 at 19:04
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
Nov 27 at 19:10
2
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
Nov 27 at 19:12
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
Nov 27 at 19:15
2
Part of problem solving is asking questions to make sure you understand the problem to be solved. Why does"0123" == 0
? If0
is not a valid letter number, what about the other combinations? Do we throw out everything if one combination is invalid? What about"10"
?10
is valid, and1
is valid, but0
is invalid so....? And"" == 1
? How does that make any sense?
– jwvh
Dec 3 at 8:18
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 Nov 27 at 19:07
asked Nov 27 at 18:46
vikrant
334
334
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
Nov 27 at 19:04
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
Nov 27 at 19:10
2
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
Nov 27 at 19:12
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
Nov 27 at 19:15
2
Part of problem solving is asking questions to make sure you understand the problem to be solved. Why does"0123" == 0
? If0
is not a valid letter number, what about the other combinations? Do we throw out everything if one combination is invalid? What about"10"
?10
is valid, and1
is valid, but0
is invalid so....? And"" == 1
? How does that make any sense?
– jwvh
Dec 3 at 8:18
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
Nov 27 at 19:04
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
Nov 27 at 19:10
2
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
Nov 27 at 19:12
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
Nov 27 at 19:15
2
Part of problem solving is asking questions to make sure you understand the problem to be solved. Why does"0123" == 0
? If0
is not a valid letter number, what about the other combinations? Do we throw out everything if one combination is invalid? What about"10"
?10
is valid, and1
is valid, but0
is invalid so....? And"" == 1
? How does that make any sense?
– jwvh
Dec 3 at 8:18
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
Nov 27 at 19:04
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
Nov 27 at 19:04
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
Nov 27 at 19:10
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
Nov 27 at 19:10
2
2
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
Nov 27 at 19:12
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
Nov 27 at 19:12
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
Nov 27 at 19:15
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
Nov 27 at 19:15
2
2
Part of problem solving is asking questions to make sure you understand the problem to be solved. Why does
"0123" == 0
? If 0
is not a valid letter number, what about the other combinations? Do we throw out everything if one combination is invalid? What about "10"
? 10
is valid, and 1
is valid, but 0
is invalid so....? And "" == 1
? How does that make any sense?– jwvh
Dec 3 at 8:18
Part of problem solving is asking questions to make sure you understand the problem to be solved. Why does
"0123" == 0
? If 0
is not a valid letter number, what about the other combinations? Do we throw out everything if one combination is invalid? What about "10"
? 10
is valid, and 1
is valid, but 0
is invalid so....? And "" == 1
? How does that make any sense?– jwvh
Dec 3 at 8:18
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
Nov 27 at 19:04
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
Nov 27 at 19:10
2
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
Nov 27 at 19:12
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
Nov 27 at 19:15
2
Part of problem solving is asking questions to make sure you understand the problem to be solved. Why does
"0123" == 0
? If0
is not a valid letter number, what about the other combinations? Do we throw out everything if one combination is invalid? What about"10"
?10
is valid, and1
is valid, but0
is invalid so....? And"" == 1
? How does that make any sense?– jwvh
Dec 3 at 8:18