This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Hi,

I am working on below challenge

http://codingbat.com/prob/p111327

I wrote my code as below

I am not passing all tests

xpected Run

sum67([1, 2, 2]) → 5 5 OK

sum67([1, 2, 2, 6, 99, 99, 7]) → 5 203 X

sum67([1, 1, 6, 7, 2]) → 4 4 OK

sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 204 X

sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 204 X

sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 6 X

sum67([2, 7, 6, 2, 6, 2, 7]) → 9 6 X

sum67([1, 6, 7, 7]) → 8 1 X

sum67([6, 7, 1, 6, 7, 7]) → 8 1 X

sum67([6, 8, 1, 6, 7]) → 0 9 X

sum67([]) → 0 0 OK

sum67([6, 7, 11]) → 11 11 OK

sum67([11, 6, 7, 11]) → 22 22 OK

sum67([2, 2, 6, 7, 7]) → 11 4 X

other tests

X

How to improve my design, approach, code? please advise

I am working on below challenge

http://codingbat.com/prob/p111327

I wrote my code as below

```
public int sum67(int[] nums) {
int sum=0;
for(int n:nums){
if(n!=6&&n!=12&&n!=7)
sum=sum+n;
}
return sum;
}
```

I am not passing all tests

xpected Run

sum67([1, 2, 2]) → 5 5 OK

sum67([1, 2, 2, 6, 99, 99, 7]) → 5 203 X

sum67([1, 1, 6, 7, 2]) → 4 4 OK

sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 204 X

sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 204 X

sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 6 X

sum67([2, 7, 6, 2, 6, 2, 7]) → 9 6 X

sum67([1, 6, 7, 7]) → 8 1 X

sum67([6, 7, 1, 6, 7, 7]) → 8 1 X

sum67([6, 8, 1, 6, 7]) → 0 9 X

sum67([]) → 0 0 OK

sum67([6, 7, 11]) → 11 11 OK

sum67([11, 6, 7, 11]) → 22 22 OK

sum67([2, 2, 6, 7, 7]) → 11 4 X

other tests

X

How to improve my design, approach, code? please advise

Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.

sum67([1, 2, 2]) → 5

sum67([1, 2, 2, 6, 99, 99, 7]) → 5

sum67([1, 1, 6, 7, 2]) → 4

i was not very clear on this challenge description.

Are they saying every 6 must immediately followed by one 7 (we have to return such 6 count??)

sum67([1, 2, 2, 6, 99, 99, 7]) → 5

how above is 5?

i see they ignored 6 and 7 but why they ignored both 99?

```
public int sum67(int[] nums) {
int sum=0;
int len=nums.length;
for(int i=0;i<len;i++){
if(nums[i]!=6&&nums[i]!=7){
sum=sum+nums[i];
}
else if(nums[i]==6&&nums[i+1]==7){
sum=sum+nums[i];
}
}
return sum;
}
```

i got challenge. i improved success rate still need make it more granular code to pass all

Expected Run

sum67([1, 2, 2]) → 5 5 OK

sum67([1, 2, 2, 6, 99, 99, 7]) → 5 203 X

sum67([1, 1, 6, 7, 2]) → 4 10 X

sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 204 X

sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 204 X

sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 12 X

sum67([2, 7, 6, 2, 6, 2, 7]) → 9 6 X

sum67([1, 6, 7, 7]) → 8 7 X

sum67([6, 7, 1, 6, 7, 7]) → 8 13 X

sum67([6, 8, 1, 6, 7]) → 0 15 X

sum67([]) → 0 0 OK

sum67([6, 7, 11]) → 11 17 X

sum67([11, 6, 7, 11]) → 22 28 X

sum67([2, 2, 6, 7, 7]) → 11 10 X

other tests

X

If you are not using a debugger, then you must learn how to use one ASAP.

In programming there are really only two success rates 0 and 100. I feel you need to improve more on your ability to think logically than on the various classes and methods with their syntax that Java can provide to generate solutions. In this exercise, there are basically two things to look for - any

Do you know the answer to this case?

1 1 6 2 3 4 5 8 9 7 1

If so, starting from left to right, by hand, look at one digit at a time, and write down the current sum. While doing that, step through the program one digit at a time. The sumby hand and in your debugger should match as you look at each digit. If it doesn't match, then spend a good deal amount of time thinking about what you did by hand and then keep modifying your code until you get it right. If you still have problems, then post several attempts at getting the solution and tell us on which digit did the program fail to match the sum when you do it by hand.

```
public class Sum67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
System.out.println("value is" + sum67(ar));
}
public static int sum67(int[] nums) {
int sum = 0;
int len = nums.length;
int sixPos=0;
int sevenPos=0;
int sum1=0;
int sum2=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 6){
sixPos=i;
}
if (nums[i] ==7){
sevenPos=i;
}
}
for (int i = 0; i < sixPos; i++) {
sum1=sum1+nums[i];
}
for (int i = sevenPos; i < len; i++) {
sum2=sum2+nums[i];
}
return sum1+sum2;
}
}
```

something like above?
```
public int sum67(int[] nums) {
int sum = 0;
int len = nums.length;
int sixPos=0;
int sevenPos=0;
int sum1=0;
int sum2=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 6){
sixPos=i;
}
if (nums[i] ==7){
sevenPos=i;
}
}
for (int i = 0; i < sixPos; i++) {
sum1=sum1+nums[i];
}
for (int i = sevenPos; i < len; i++) {
sum2=sum2+nums[i];
}
return sum1+sum2;
}
```

failing few tests```
Expected Run
sum67([1, 2, 2]) → 5 5 OK
sum67([1, 2, 2, 6, 99, 99, 7]) → 5 12 X
sum67([1, 1, 6, 7, 2]) → 4 11 X
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 26 X
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 32 X
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 24 X
sum67([2, 7, 6, 2, 6, 2, 7]) → 9 24 X
sum67([1, 6, 7, 7]) → 8 8 OK
sum67([6, 7, 1, 6, 7, 7]) → 8 21 X
sum67([6, 8, 1, 6, 7]) → 0 22 X
sum67([]) → 0 0 OK
sum67([6, 7, 11]) → 11 18 X
sum67([11, 6, 7, 11]) → 22 29 X
sum67([2, 2, 6, 7, 7]) → 11 11 OK
other tests
X
```

i have to remind about psedo code description

1. Loop through each element of given array

2. Find index position of six

3. find index position of seven.

4. find sum1 which is sum of all integers till six index position

5. find sum2 which is sum of all intergers starting from seven index till end

6. return sum of both sextions sum1 plus sum2

```
public class Sum67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
System.out.println("value is" + sum67(ar));
}
public static int sum67(int[] nums) {
int sum = 0;
int len = nums.length;
int sixPos=0;
int sevenPos=0;
int sum1=0;
int sum2=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 6){
sixPos=i;
System.out.println("sixPos is-->"+sixPos);
}
if (nums[i] ==7){
sevenPos=i;
System.out.println("sevenPos is-->"+sevenPos);
}
}
for (int i = 0; i < sixPos; i++) {
sum1=sum1+nums[i];
}
for (int i = sevenPos+1; i < len; i++) {
sum2=sum2+nums[i];
}
System.out.println("sum1 is-->"+sum1);
System.out.println("sum2 is-->"+sum2);
return sum1+sum2;
}
}
```

i corrected one error

got more accurate output

sixPos is-->2

sevenPos is-->9

sum1 is-->2

sum2 is-->1

value is3

xpected Run

sum67([1, 2, 2]) → 5 4 X

sum67([1, 2, 2, 6, 99, 99, 7]) → 5 5 OK

sum67([1, 1, 6, 7, 2]) → 4 4 OK

sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 19 X

sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 25 X

sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 17 X

sum67([2, 7, 6, 2, 6, 2, 7]) → 9 17 X

sum67([1, 6, 7, 7]) → 8 1 X

sum67([6, 7, 1, 6, 7, 7]) → 8 14 X

sum67([6, 8, 1, 6, 7]) → 0 15 X

sum67([]) → 0 0 OK

sum67([6, 7, 11]) → 11 11 OK

sum67([11, 6, 7, 11]) → 22 22 OK

sum67([2, 2, 6, 7, 7]) → 11 4 X

other tests

X

You can probably get away with a single pass over the array if you have a boolean that indicates whether you're between a 6 and a 7. The pseudocode of the loop could look like this:let me check on above

If the current element is a 6, set the boolean to true.

If the boolean is false, add the current element to the sum.

If the current element is a 7, set the boolean to false.

```
public class Sum67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
// int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
int[] ar = { 1, 2, 2 };// sixPos 0 sevenPos 0
System.out.println("value is" + sum67(ar));
}
public static int sum67(int[] nums) {
int sum = 0;
int len = nums.length;
int sixPos = 0;
int sevenPos = 0;
int sum1 = 0;
int sum2 = 0;
int sumExclude = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 6) {
sixPos = i;
System.out.println("sixPos is-->" + sixPos);
}
if (nums[i] == 7) {
sevenPos = i;
System.out.println("sevenPos is-->" + sevenPos);
}
}
for (int i = 0; i < sixPos; i++) {
sum1 = sum1 + nums[i];
}
for (int i = sevenPos + 1; i < len; i++) {
sum2 = sum2 + nums[i];
}
System.out.println("sum1 is-->" + sum1);
System.out.println("sum2 is-->" + sum2);
if (sixPos == 0 && sevenPos == 0) {
for (int i = 0; i < len; i++) {
sumExclude = sumExclude + nums[i];
}
return sumExclude;
} else if (sixPos > 0 && sevenPos > 0) {
return sum1 + sum2;
} else {
return 0;
}
}
}
```

It seems like you are making some progress. But, did you do the debugger step-by-step approach with the manual method of your looking at one card at a time, and when the sum differs, then you try to figure out what your program is doing differently than what you are doing by hand.

Given the difficulties you are having with some of these challenges, may I suggest that you first put a question strictly in the algorithms zone and post your pseudo-code (language independent) and explain where it goes wrong when you compare with your manual approach.

Once you get the design right (i.e., the pseudo-code), then it will be easier to code up the challenge.

>> In programming there are really only two success rates 0 and 100.

Heh, you are certainly right for this type of challenge.

But in a free online U.C.S.D. course, you get three chances to fail when you submit your program:

1) incorrect answers

2) program exceeds the time-limit

3) program takes up too much memory

- It's a pretty tough checker with lots of edge corner-cases.

Come to think of it, if the author is getting less errors and figuring out on his own how to correct them, then the challenges are proving very valuable. On the other hand, if the author just shows his progress and gets tips from the experts, then he may understand, but may not know. Knowing is a requirement for most software development positions.

But in a free online U.C.S.D. course

i wonder where i can register and learn, practice them? please advise

like

1 7 5 6 xxxxx7 3 9

```
public class Sum67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
// int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
int[] ar = { 1, 2, 2 };// sixPos 0 sevenPos 0
System.out.println("value is" + sum67(ar));
}
public static int sum67(int[] nums) {
/*
* If the current element is a 6, set the boolean to true.
If the boolean is false, add the current element to the sum.
If the current element is a 7, set the boolean to false.*/
int sum=0;
boolean ind=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==6){
ind=true;
}
if(ind==false){
sum+=nums[i];
}
if(nums[i]==7){
ind=true;
}
}
return sum;
}
}
```

Expected Run

sum67([1, 2, 2]) → 5 5 OK

sum67([1, 2, 2, 6, 99, 99, 7]) → 5 5 OK

sum67([1, 1, 6, 7, 2]) → 4 2 X

sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 1 X

sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 1 X

sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 9 X

sum67([2, 7, 6, 2, 6, 2, 7]) → 9 9 OK

sum67([1, 6, 7, 7]) → 8 1 X

sum67([6, 7, 1, 6, 7, 7]) → 8 0 X

sum67([6, 8, 1, 6, 7]) → 0 0 OK

sum67([]) → 0 0 OK

sum67([6, 7, 11]) → 11 0 X

sum67([11, 6, 7, 11]) → 22 11 X

sum67([2, 2, 6, 7, 7]) → 11 4 X

other tests

X

you mean like above. I am passing few more but failing few. please advise

If the current element is a 6, set the boolean to true.

If the boolean is false, add the current element to the sum.

If the current element is a 7, set the boolean to false

```
public int sum67(int[] nums) {int sum=0;
boolean ind=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==6){
ind=true;
}
if(ind==false){
sum+=nums[i];
}
if(nums[i]==7){
ind=false;
}
}
return sum;
}
```

```
public int sum67(int[] nums) {int sum=0;
boolean ind=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==6){
ind=true;
}
if(ind==false){
sum+=nums[i];
}
if(nums[i]==7&&ind==true){
ind=false;
}
}
return sum;
}
```

above pass all

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

true.false, add the current element to the sum.false.