DeltaX Coding Python Sol
DeltaX Coding Python Sol
Example:
Input: [2, 4, 7, 11, 15], target=9 → Output: (2, 7)
# Example
print(find_pair([2, 4, 7, 11, 15], 9))
Output: (2, 7)
def is_palindrome(s):
s = ''.join(c.lower() for c in s if c.isalnum())
return s == s[::-1]
# Example
print(is_palindrome("A man a plan a canal Panama")) # True
3️
)Count Number of Set Bits
Given an integer, count how many bits are set to 1.
def count_set_bits(n):
count = 0
while n:
count += n & 1
n >>= 1
return count
print(count_set_bits(9)) # Output: 2
def find_missing_number(nums):
n = len(nums) + 1
expected_sum = n * (n+1) // 2
return expected_sum - sum(nums)
print(find_missing_number([1,2,4,5])) # Output: 3
5) Longest Word in Sentence
Return the longest word (if tie, return first).
def longest_word(sentence):
words = sentence.split()
return max(words, key=len)
print(fib(10)) # Output: 55
7) Print Elements in Spiral Order (Matrix)
Input: 2D matrix; Output: list in spiral order.
def spiral_order(matrix):
res = []
while matrix:
res += matrix.pop(0)
if matrix and matrix[0]:
for row in matrix: res.append(row.pop())
if matrix:
res += matrix.pop()[::-1]
if matrix and matrix[0]:
for row in matrix[::-1]: res.append(row.pop(0))
return res
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
print(spiral_order(matrix)) # [1,2,3,6,9,8,7,4,5]