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

Create subset_sum.cpp #1517

Merged
merged 51 commits into from
Jul 13, 2021
Merged

Create subset_sum.cpp #1517

merged 51 commits into from
Jul 13, 2021

Conversation

Swastyy
Copy link
Contributor

@Swastyy Swastyy commented Jun 28, 2021

Description of Change I have created subset_sum.cpp to return the number of subsets(both continuous and non-continuous subarrays) whose sum equals the given sum value in case if there doesn't exists any subset with given sum the function will return 0.

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

@Swastyy
Copy link
Contributor Author

Swastyy commented Jun 28, 2021

@fhlasek I have added self test cases. Please have a look.

P.S. It returns the number of subsets with given sum since storing all the subsets and then returning them was taking much more space leading to bad space complexity so I preferred returning an integer.

Copy link
Contributor

@fhlasek fhlasek left a comment

Choose a reason for hiding this comment

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

Nice, simple, clear and well documented and tested. Thanks!

backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
@Swastyy
Copy link
Contributor Author

Swastyy commented Jun 29, 2021

@fhlasek done the changes. Pl have a look.

fhlasek
fhlasek previously approved these changes Jun 29, 2021
Copy link
Contributor

@fhlasek fhlasek left a comment

Choose a reason for hiding this comment

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

Code looks great now! Happy to approve. Consider some suggestions about the documentation. A better description of the algorithm might be helpful.

backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
@Swastyy
Copy link
Contributor Author

Swastyy commented Jun 29, 2021

Code looks great now! Happy to approve. Consider some suggestions about the documentation. A better description of the algorithm might be helpful.

I just commited the suggestions, then why there is statement like "Swastyy dismissed fhlasek’s stale review via a32b32f 3 minutes ago" in the thread?

@Swastyy Swastyy requested a review from fhlasek June 29, 2021 19:03
@Swastyy
Copy link
Contributor Author

Swastyy commented Jun 29, 2021

Code looks great now! Happy to approve. Consider some suggestions about the documentation. A better description of the algorithm might be helpful.

I just commited the suggestions, then why there is statement like "Swastyy dismissed fhlasek’s stale review via a32b32f 3 minutes ago" in the thread?

Also I can't see the approved tick. Did I made some mistake while committing suggestions?

P.S. I am newbie in open source and I don't know how these silly mistakes happens 😅

fhlasek
fhlasek previously approved these changes Jun 29, 2021
Copy link
Contributor

@fhlasek fhlasek left a comment

Choose a reason for hiding this comment

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

No worries, I guess that was my mistakes. I haven't realised that this repo is set up to invalidate the approval after code changes are committed. Here is another ✅ 👍🏼

@Swastyy
Copy link
Contributor Author

Swastyy commented Jul 1, 2021

@fhlasek how to get 1 more approvel? 😅

fhlasek
fhlasek previously approved these changes Jul 2, 2021
Copy link
Contributor

@fhlasek fhlasek left a comment

Choose a reason for hiding this comment

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

💯

@Swastyy
Copy link
Contributor Author

Swastyy commented Jul 4, 2021

@mishraabhinn Can you see this PR. It already got 1 approval, 1 more will be appreciated 😃

Copy link
Member

@mishraabhinn mishraabhinn left a comment

Choose a reason for hiding this comment

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

What should be output if the sum =2 and arr=[1,1,1] @Swastyy

@mishraabhinn
Copy link
Member

@mishraabhinn Can you see this PR. It already got 1 approval, 1 more will be appreciated 😃

I think you can add a specific description, subset_sum can cause ambiguity that whether this algorithm is for continuous subarrays or non continuous subarrays.
For the sum =2 and arr =[1,1,1] if former case is taken then outut will be 2 and if latter is taken than 3.

@Swastyy
Copy link
Contributor Author

Swastyy commented Jul 5, 2021

What should be output if the sum =2 and arr=[1,1,1] @Swastyy

Yea, it will return 3. I have taken a similar test case (Test 4) with arr = [3,3,3,3] and sum = 6

@Swastyy
Copy link
Contributor Author

Swastyy commented Jul 5, 2021

@mishraabhinn Can you see this PR. It already got 1 approval, 1 more will be appreciated 😃

I think you can add a specific description, subset_sum can cause ambiguity that whether this algorithm is for continuous subarrays or non continuous subarrays.
For the sum =2 and arr =[1,1,1] if former case is taken then outut will be 2 and if latter is taken than 3.

Generally subsets contain both the continuous and non-continuous subarrays. I will add it in the description. 👍

@Swastyy
Copy link
Contributor Author

Swastyy commented Jul 10, 2021

@mishraabhinn we cannot take unsigned int as data type because we are taking negative weights array in the test case

@mishraabhinn
Copy link
Member

@mishraabhinn we cannot take unsigned int as data type because we are taking negative weights array in the test case

int8_t
int16_t
int32_t
int64_t
Then you should be using these specifically.

@Swastyy
Copy link
Contributor Author

Swastyy commented Jul 11, 2021

@mishraabhinn we cannot take unsigned int as data type because we are taking negative weights array in the test case

int8_t
int16_t
int32_t
int64_t
Then you should be using these specifically.

Now I feel like using int is not a good practice. Does it matters when we are adding our own test cases
I have changed the code accordingly. Pl see once.

@mishraabhinn
Copy link
Member

@mishraabhinn we cannot take unsigned int as data type because we are taking negative weights array in the test case

int8_t
int16_t
int32_t
int64_t
Then you should be using these specifically.

Now I feel like using int is not a good practice. Does it matters when we are adding our own test cases
I have changed the code accordingly. Pl see once.

I will tell you one thing, writing code for any problem is simple. But what we should learn is to write clean code. You should write code in such a way that if you/someone someday come back to the code, you don't have to go line by line in the code to understand it, rather seeing the name of file , function name and variable name code should speak itself.

@Swastyy
Copy link
Contributor Author

Swastyy commented Jul 11, 2021

I will tell you one thing, writing code for any problem is simple. But what we should learn is to write clean code. You should write code in such a way that if you/someone someday come back to the code, you don't have to go line by line in the code to understand it, rather seeing the name of file , function name and variable name code should speak itself.

Yea, this used to happen with me. But now I make proper doc. 😃

backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
mishraabhinn
mishraabhinn previously approved these changes Jul 11, 2021
@Swastyy
Copy link
Contributor Author

Swastyy commented Jul 11, 2021

@Panquesito7 Pl review this PR.

backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Outdated Show resolved Hide resolved
backtracking/subset_sum.cpp Show resolved Hide resolved
Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

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

Great work, @Swastyy! Thank you for your patience, dedication, and contributions to our community! 👍 🎉

@Panquesito7 Panquesito7 added approved Approved; waiting for merge and removed requested changes changes required labels Jul 13, 2021
@Panquesito7 Panquesito7 merged commit e059765 into TheAlgorithms:master Jul 13, 2021
@Swastyy Swastyy deleted the patch-2 branch July 15, 2021 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Approved; waiting for merge enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants