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 largest_pow_of_two_le_num #9374

Conversation

namansharma18899
Copy link
Contributor

Describe your change:

Fixes #9347

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 2, 2023
@namansharma18899 namansharma18899 force-pushed the add/largest_pow_of_two_le_num branch from 6925fa1 to 768e506 Compare October 2, 2023 07:43
Copy link

@rahbdd rahbdd left a comment

Choose a reason for hiding this comment

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

def find(a):
if a<=0:
return 0;
for i in range(a+1):
if (2**i <= a):
c=i;
else :
return c;
b=int(input())
print(find(b))
this would be a simple and effective code i guess

16 0b10000 (Exit)
"""


Copy link

Choose a reason for hiding this comment

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

i dont think we need so long one instead we can use
def find(a):
if a<=0:
return 0;
for i in range(a+1):
if (2**i <= a):
c=i;
else :
return c;
b=int(input())
print(find(b))

Copy link
Contributor Author

@namansharma18899 namansharma18899 Oct 4, 2023

Choose a reason for hiding this comment

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

Hey @rahbdd thanks but there are a couple of issues with your suggestion

  1. It's actually not about the size of the code but how verbose yet briefly it explains the algo. The actual code I have written is in fact only 5 lines, rest is explanation.
  2. The syntax of your logic ??
  3. Your code is remarkably slow compared to the proposed solution. Check out this performance analysis.

image
image

I would suggest you to look at all the other algo's in the repository. The goal of the TheAlgorithms as described by their authors is to document model beautiful, helpful and interesting algorithms using code

Choose a reason for hiding this comment

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

Thank you , I will try to modify it

DIRECTORY.md Outdated
@@ -51,6 +51,7 @@
* [Index Of Rightmost Set Bit](bit_manipulation/index_of_rightmost_set_bit.py)
* [Is Even](bit_manipulation/is_even.py)
* [Is Power Of Two](bit_manipulation/is_power_of_two.py)
* [Is Power Of Two](bit_manipulation/largest_pow_of_two_less_than_or_equal_to_a_number.py)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please DO NOT edit DIRECTORY.md. We have a workflow to do this for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood, I'll revert this change.

@namansharma18899 namansharma18899 force-pushed the add/largest_pow_of_two_le_num branch 2 times, most recently from 90e0c82 to 3bf61e7 Compare October 5, 2023 01:39
@namansharma18899 namansharma18899 force-pushed the add/largest_pow_of_two_le_num branch from 3bf61e7 to 582ee5f Compare October 5, 2023 01:46
@ChrisO345 ChrisO345 merged commit f3be0ae into TheAlgorithms:master Oct 5, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 5, 2023
@ChrisO345 ChrisO345 mentioned this pull request Oct 5, 2023
14 tasks
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add largest power of 2 less than or equal to a given number
4 participants