Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.
public int sum13(int[] nums) {
int sum=0;
for(int n:nums){
if(n!=13&&n!=12&&n!=14)
sum=sum+n;
}
return sum;
}
I am not passing all testsso it does not count and numbers that come immediately after a 13
Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.
sum13([1, 2, 2, 1]) → 6
sum13([1, 1]) → 2
sum13([1, 2, 2, 1, 13]) → 6
Expected Run
sum13([1, 2, 2, 1]) → 6 6 OK
sum13([1, 1]) → 2 2 OK
sum13([1, 2, 2, 1, 13]) → 6 6 OK
sum13([1, 2, 13, 2, 1, 13]) → 4 6 X //not sure how returning 4?
sum13([13, 1, 2, 13, 2, 1, 13]) → 3 6 X //not sure how returning 3?
sum13([]) → 0 0 OK
sum13([13]) → 0 0 OK
sum13([13, 13]) → 0 0 OK
sum13([13, 0, 13]) → 0 0 OK
sum13([13, 1, 13]) → 0 1 X
sum13([5, 7, 2]) → 14 14 OK
sum13([5, 13, 2]) → 5 7 X
sum13([0]) → 0 0 OK
sum13([13, 0]) → 0 0 OK
other tests
X
The number immediately following the 13 also doesn't count.
public int sum13(int[] nums) {
int sum=0;
int sumTotal=0;
int len=nums.length;
int sumNon13=0;
int sumNextTo13=0;
int sumAll=0;
int sumJunk=0;
int sumActual=0;
int result=0;
for(int i=0;i<len-1;i++){
if(nums[i]==13){
sumTotal=sumTotal+nums[i];
sumNon13=sumNon13+nums[i];
sumNextTo13=sumNextTo13+nums[i+1];
sumJunk=sumNon13+sumNextTo13;
sumActual=sumTotal-sumJunk;
result=sumActual;
} else {
sumAll=sumAll+nums[i];
result=sumAll;
//return sumAll;
}
}
return result;
}
Expected Run
sum13([1, 2, 2, 1]) → 6 5 X
sum13([1, 1]) → 2 1 X
sum13([1, 2, 2, 1, 13]) → 6 6 OK
sum13([1, 2, 13, 2, 1, 13]) → 4 6 X
sum13([13, 1, 2, 13, 2, 1, 13]) → 3 6 X
sum13([]) → 0 0 OK
sum13([13]) → 0 0 OK
sum13([13, 13]) → 0 -13 X
sum13([13, 0, 13]) → 0 0 OK
sum13([13, 1, 13]) → 0 1 X
sum13([5, 7, 2]) → 14 12 X
sum13([5, 13, 2]) → 5 -2 X
sum13([0]) → 0 0 OK
sum13([13, 0]) → 0 0 OK
other tests
X
I agree with Krakatoa that the description is not clear and subject to interpretation
public static int sum13(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 13)
sum += nums[i];
else if (nums[i] == 13 && i < nums.length - 1) {
nums[i] = 0;
nums[i + 1] = 0;
}
}
return sum;
}
A corollary to this exercise would be to sum all of the integers in the array, identify any that are 13 and any that follow 13 in the array and subtract them.i tried above approach passed almost all tests except one
nums = [13, 1, 2, 13, 2, 1, 13] ==> sum = 45
identify 13 plus following
[13, 1, 2, 13, 2, 1, 13] ==bold numbers add up to 42
so sum13[13, 1, 2, 13, 2, 1, 13] returns 3 (45 - 42)
public int sum13(int[] nums) {/*
* int sum = 0; for (int i = 0; i < nums.length;
* i++) { if (nums[i] != 13) sum += nums[i]; else if
* (nums[i] == 13 && i < nums.length - 1) { nums[i]
* = 0; nums[i + 1] = 0; } } return sum;
*/
int sum = 0;
int sumBold = 0;
for (int i = 0; i < nums.length; i++) {
/*
* if (i<nums.length){ sum += nums[i]; System.out.println(
* "sum is-->"+sum); } else
*/
sum += nums[i];
// System.out.println("sum is-->" + sum);
if (nums[i] == 13)
if (i < nums.length-1) {
sumBold += nums[i] + nums[i + 1];
// System.out.println("sumBold is-->" + sumBold);
} else {
sumBold += nums[i];
}
}
return (sum - sumBold);
}
Expected Run
sum13([1, 2, 2, 1]) → 6 6 OK
sum13([1, 1]) → 2 2 OK
sum13([1, 2, 2, 1, 13]) → 6 6 OK
sum13([1, 2, 13, 2, 1, 13]) → 4 4 OK
sum13([13, 1, 2, 13, 2, 1, 13]) → 3 3 OK
sum13([]) → 0 0 OK
sum13([13]) → 0 0 OK
sum13([13, 13]) → 0 -13 X
sum13([13, 0, 13]) → 0 0 OK
sum13([13, 1, 13]) → 0 0 OK
sum13([5, 7, 2]) → 14 14 OK
sum13([5, 13, 2]) → 5 5 OK
sum13([0]) → 0 0 OK
sum13([13, 0]) → 0 0 OK
other tests
OK
That does pass all tests, but, in an actual project, other developers you work with might get mad at your code for modifying the array that's passed in.
public class Sum13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = { 13, 13 };
System.out.println(sum13(ar));
}
// public static int sum13(int[] nums) {}
public static int sum13(
int[] nums) {/*
* int sum = 0; for (int i = 0; i < nums.length;
* i++) { if (nums[i] != 13) sum += nums[i]; else if
* (nums[i] == 13 && i < nums.length - 1) { nums[i]
* = 0; nums[i + 1] = 0; } } return sum;
*/
int sum = 0;
int sumBold = 0;
for (int i = 0; i < nums.length; i++) {
/*
* if (i<nums.length){ sum += nums[i]; System.out.println(
* "sum is-->"+sum); } else
*/
sum += nums[i];
System.out.println("sum is-->" + sum);
if (nums[i] == 13)
if (i < nums.length - 1) {
sumBold += nums[i] + nums[i + 1];
System.out.println("sumBold is-->" + sumBold);
} else {
sumBold += nums[i];
}
}
return (sum - sumBold);
}
}
public class Sum13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = { 13, 13 };
System.out.println(sum13(ar));
}
// public static int sum13(int[] nums) {}
public static int sum13(
int[] nums) {/*
* int sum = 0; for (int i = 0; i < nums.length;
* i++) { if (nums[i] != 13) sum += nums[i]; else if
* (nums[i] == 13 && i < nums.length - 1) { nums[i]
* = 0; nums[i + 1] = 0; } } return sum;
*/
int sum = 0;
int sumBold = 0;
for (int i = 0; i < nums.length; i++) {
/*
* if (i<nums.length){ sum += nums[i]; System.out.println(
* "sum is-->"+sum); } else
*/
sum += nums[i];
System.out.println("sum is-->" + sum);
if (nums[i] == 13)
if (i < nums.length - 1) {
sumBold += nums[i] + nums[i + 1];
System.out.println("sumBold is-->" + sumBold);
} /*else {
sumBold += nums[i];
}*/
}
return (sum - sumBold);
}
}
Expected Run
sum13([1, 2, 2, 1]) → 6 6 OK
sum13([1, 1]) → 2 2 OK
sum13([1, 2, 2, 1, 13]) → 6 19 X
sum13([1, 2, 13, 2, 1, 13]) → 4 17 X
sum13([13, 1, 2, 13, 2, 1, 13]) → 3 16 X
sum13([]) → 0 0 OK
sum13([13]) → 0 13 X
sum13([13, 13]) → 0 0 OK
sum13([13, 0, 13]) → 0 13 X
sum13([13, 1, 13]) → 0 13 X
sum13([5, 7, 2]) → 14 14 OK
sum13([5, 13, 2]) → 5 5 OK
sum13([0]) → 0 0 OK
sum13([13, 0]) → 0 0 OK
other tests
X
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Android development question | 2 | 55 | |
Delphi: barcode reading on android platform | 1 | 53 | |
Authenticate using sesu from script | 7 | 60 | |
Where to download python requests library | 6 | 34 |
Join the community of 500,000 technology professionals and ask your questions.