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]()))
}









share|improve this question




















  • 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? 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

















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]()))
}









share|improve this question




















  • 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? 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















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]()))
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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? 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
















  • 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? 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










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

















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Список кардиналов, возведённых папой римским Каликстом III

Deduzione

Mysql.sock missing - “Can't connect to local MySQL server through socket”