Solved

scoreUp challenge

Posted on 2016-08-30
14
61 Views
Last Modified: 2016-09-09
Hi,

I am working on below challenge
http://codingbat.com/prob/p180365



The "key" array is an array containing the correct answers to an exam, like {"a", "a", "b", "b"}. the "answers" array contains a student's answers, with "?" representing a question left blank. The two arrays are not empty and are the same length. Return the score for this array of answers, giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer.


i was not clear on above description.

i was not clear how below result is 6 etc. please advise
scoreUp(["a", "a", "b", "b"], ["a", "c", "b", "c"]) → 6
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "c"]) → 11
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "b"]) → 16
0
Comment
Question by:gudii9
  • 7
  • 4
  • 3
14 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 41777566
sorry, but I wonder what is not clear about this exercice.
simply looping the array, comparing the values at the same indices, and depending on the values, sum up the scores...
0
 
LVL 7

Author Comment

by:gudii9
ID: 41780968
giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer.

simply looping the array, comparing the values at the same indices, and depending on the values, sum up the scores...
i think i got it now


scoreUp(["a", "a", "b", "b"], ["a", "c", "b", "c"]) → 6

as 'a' is same in both array give +4
then a not same as c so -1
then b same as b +4
then b not same as c -1

total is 6

let me think on pseudo code and code now
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41781145
yes, exactly
don't forget to code for the "blank" answers
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

by:gudii9
ID: 41788984
public int scoreUp(String[] key, String[] answers) {
  int count=0;
  for(int i=0;i<key.length;i++){
    if(( (!key[i].equals("?")) &&(!answers.equals("?")) &&(key[i]==answers[i]))){
      count=count+4;
    }else{
      count=count-1;
    }
  }
  return count;
}

Open in new window


above failing below tests. please advise
Expected      Run            
scoreUp(["a", "a", "b", "b"], ["a", "c", "b", "c"]) → 6      6      OK      
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "c"]) → 11      11      OK      
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "b"]) → 16      16      OK      
scoreUp(["a", "a", "b", "b"], ["?", "c", "b", "?"]) → 3      1      X      
scoreUp(["a", "a", "b", "b"], ["?", "c", "?", "?"]) → -1      -4      X      
scoreUp(["a", "a", "b", "b"], ["c", "?", "b", "b"]) → 7      6      X      
scoreUp(["a", "a", "b", "b"], ["c", "?", "b", "?"]) → 3      1      X      
scoreUp(["a", "b", "c"], ["a", "c", "b"]) → 2      2      OK      
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "a", "c", "a", "c"]) → 4      4      OK      
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "?", "?", "a", "c"]) → 6      4      X      
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "?", "?", "c", "c"]) → 11      9      X      
scoreUp(["a", "b", "c"], ["a", "b", "c"]) → 12      12      OK      
other tests
X
0
 
LVL 27

Expert Comment

by:rrz
ID: 41789053
The challenge states  
"?" representing a question left blank.
this will appear in the "answers" array. Therefore the following  
(!key[i].equals("?"))

Open in new window

is not necessary. Your errors occur when there is a "?" in the answers. When it is found you add -1. That is wrong.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41789068
I confirm, and that is what I meant by:

don't forget to code for the "blank" answers
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790652
The challenge states  
"?" representing a question left blank.
this will appear in the "answers" array. Therefore the following  
(!key[i].equals("?"))


is not necessary. Your errors occur when there is a "?" in the answers. When it is found you add -1. That is wrong.

Open in new window



what i supposed to do when i see question mark(?)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790664
public int scoreUp(String[] key, String[] answers) {
  int count=0;
  for(int i=0;i<key.length;i++){
    if(( (!key[i].equals("?")) &&(!answers.equals("?")) &&(key[i]==answers[i]))){
      count=count+4;
    }else if(key[i].equals("?")){
      count=0;
    }else if(answers[i].equals("?")){
      count=0;
    }else{
      count=count-1;
    }
  }
  return count;
}

Open in new window


something like above?
I am failing some tests. please advise
Expected      Run            
scoreUp(["a", "a", "b", "b"], ["a", "c", "b", "c"]) → 6      6      OK      
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "c"]) → 11      11      OK      
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "b"]) → 16      16      OK      
scoreUp(["a", "a", "b", "b"], ["?", "c", "b", "?"]) → 3      0      X      
scoreUp(["a", "a", "b", "b"], ["?", "c", "?", "?"]) → -1      0      X      
scoreUp(["a", "a", "b", "b"], ["c", "?", "b", "b"]) → 7      8      X      
scoreUp(["a", "a", "b", "b"], ["c", "?", "b", "?"]) → 3      0      X      
scoreUp(["a", "b", "c"], ["a", "c", "b"]) → 2      2      OK      
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "a", "c", "a", "c"]) → 4      4      OK      
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "?", "?", "a", "c"]) → 6      3      X      
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "?", "?", "c", "c"]) → 11      8      X      
scoreUp(["a", "b", "c"], ["a", "b", "c"]) → 12      12      OK      
other tests
X
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790668
public int scoreUp(String[] key, String[] answers) {
  int count=0;
  for(int i=0;i<key.length;i++){
    if(( (!key[i].equals("?")) &&(!answers.equals("?")) &&(key[i]==answers[i]))){
      count=count+4;
    }else if(key[i].equals("?")){
      count=0;
    }else if(answers[i].equals("?")){
      count=count+0;
    }else if(!answers[i].equals(key[i])){
      count=count-1;
    }
  }
  return count;
}

Open in new window

something like above.
Above passes all tests.
any improvements or alternate approaches? please advise
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41790709
That code works. But, the challenge states
"key" array is an array containing the correct answers to an exam
Therefore a "?" will  never be found in the key array.     So,
(!key[i].equals("?"))

Open in new window

and
else if(key[i].equals("?")){
      count=0;

Open in new window

are not necessary.

count=count+0;

Open in new window

That is confusing for the reader. You could just use
 continue;

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41791397
i see your point.

public int scoreUp(String[] key, String[] answers) {
  int count=0;
  for(int i=0;i<key.length;i++){
    if(( (!answers.equals("?")) &&(key[i]==answers[i]))){
      count=count+4;
    }/*else if(key[i].equals("?")){
      continue;
    }*/else if(answers[i].equals("?")){
      count=count+0;
    }else if(!answers[i].equals(key[i])){
      count=count-1;
    }
  }
  return count;
}

Open in new window


above looks ok? any improvements, alternate approaches?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41791450
above looks ok?
It looks ok, but you could clean it up to improve readability.
public int scoreUp(String[] key, String[] answers) {
  int count = 0;
  for(int i=0;i<key.length;i++){
    if(answers[i].equals("?"))continue;
    if(key[i]==answers[i]){
      count = count + 4;
    }else count--;
  }
  return count;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41791522
why you chose if with if then else approach rather than
if with else if with else approach as i did ?
any specific advantage or disadvantage?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41791637
any specific advantage or disadvantage?
I just thought it was clearer.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat not starting 6 45
Eclipse with gitlab 1 37
null output 3 24
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 31
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question