Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sum with recursion to 03_recursion, java folder #254

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ highlights/atom-language-perl6/
.DS_store
highlights/package-lock.json
zig-cache
assignments
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you undo this change?


# IDE specific
.scala_dependencies
Expand Down
20 changes: 20 additions & 0 deletions 03_recursion/java/04_sum/src/Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.*;

public class Sum {
public static int sum(ArrayList<Integer> num_list) {

if (num_list.size() == 0) {
return 0;
} else {
int num = num_list.get(0);
num_list.remove(0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you write a version that doesn't modify the list?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIP

return num + sum(num_list);
}

}

public static void main(String[] args) {
int total = sum(new ArrayList<Integer>(Arrays.asList(2, 4, 6)));
System.out.println(total);
}
}