This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def heap_sort(arr): | |
| # Build a max heap from the input list | |
| heapify(arr) | |
| # Extract the root element (the maximum value) and place it at the end of the sorted list | |
| sorted_arr = [] | |
| while arr: | |
| sorted_arr.append(heappop(arr)) | |
| # Return the sorted list | |
| return sorted_arr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def radix_sort(arr): | |
| """ | |
| Radix sort starting from the least significant digit(LSD) | |
| :param arr: The list needs to be sorted | |
| :return: A sorted list | |
| """ | |
| # Find the maximum number of digits in the list | |
| max_digits = max([len(str(x)) for x in arr]) | |
| # Set the base (radix) to 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def prime_factorization(num: int): | |
| k = 2 | |
| factors = [] | |
| while k * k <= num: | |
| while num % k == 0: | |
| factors.append(k) | |
| num //= k | |
| k += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def productQueries(self, n: int, queries): | |
| bn = bin(n)[2:][::-1] | |
| powers = [] | |
| for i, v in enumerate(bn): | |
| if v == '1': | |
| powers.append(i) | |
| ans = [] | |
| for l, r in queries: | |
| t = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function isMobile() { | |
| try { | |
| object.addEventListener("touchstart", myScript); | |
| return true; | |
| } catch(e) { | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function ResponsiveUI() { | |
| return ( | |
| window.screen.width >= 600 ? ( | |
| // an UI component for desktop browsers | |
| ) : ( | |
| // an UI component for mobile browsers | |
| ) | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function onClick(){ | |
| if(window.screen.width>=600){ | |
| // do sth for desktop browsers | |
| } | |
| else{ | |
| // do sth for mobile browsers | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function onClick() { | |
| if ((/Android/i.test(navigator.userAgent))) { | |
| // go to Google Play Store | |
| } | |
| if (/iPad|iPhone|iPod/i.test(navigator.userAgent)) { | |
| // go to App Store | |
| } | |
| else { | |
| // go to another website | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import Counter | |
| author = "Yang Zhou" | |
| chars = Counter(author) | |
| print(chars) | |
| # Counter({'Y': 1, 'a': 1, 'n': 1, 'g': 1, ' ': 1, 'Z': 1, 'h': 1, 'o': 1, 'u': 1}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cities = {'London': '2', 'Tokyo': '3', 'New York': '1'} | |
| print(sorted(cities.items(),key=lambda d:d[1])) | |
| # [('New York', '1'), ('London', '2'), ('Tokyo', '3')] |
NewerOlder