') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Added largest_pow_of_two_le_num by namansharma18899 · Pull Request #9374 · TheAlgorithms/Python · GitHub
Skip to content

Added largest_pow_of_two_le_num - #9374

Merged
chriso345 merged 1 commit into
TheAlgorithms:masterfrom
namansharma18899:add/largest_pow_of_two_le_num
Oct 5, 2023
Merged

Added largest_pow_of_two_le_num#9374
chriso345 merged 1 commit into
TheAlgorithms:masterfrom
namansharma18899:add/largest_pow_of_two_le_num

Conversation

@namansharma18899

Copy link
Copy Markdown
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-keeperalgorithms-keeperBot added the awaiting reviews This PR is ready to be reviewed label Oct 2, 2023
@namansharma18899
namansharma18899force-pushed the add/largest_pow_of_two_le_num branch from 6925fa1 to 768e506CompareOctober 2, 2023 07:43

@rahbddrahbdd left a comment

Copy link
Copy Markdown

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
Copy Markdown

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))

@namansharma18899namansharma18899Oct 4, 2023

Copy link
Copy Markdown
ContributorAuthor

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

Copy link
Copy Markdown

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

Comment threadDIRECTORY.md Outdated
* [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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
namansharma18899force-pushed the add/largest_pow_of_two_le_num branch 2 times, most recently from 90e0c82 to 3bf61e7CompareOctober 5, 2023 01:39
@namansharma18899
namansharma18899force-pushed the add/largest_pow_of_two_le_num branch from 3bf61e7 to 582ee5fCompareOctober 5, 2023 01:46
@chriso345
chriso345 merged commit f3be0ae into TheAlgorithms:masterOct 5, 2023
@algorithms-keeperalgorithms-keeperBot removed the awaiting reviews This PR is ready to be reviewed label Oct 5, 2023
@chriso345chriso345 mentioned this pull request Oct 5, 2023
14 tasks
@isidroasisidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for freeto 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

@namansharma18899@chriso345@rahboddeda@rahbdd