Skip to main content

Poridhi: Basic Programming & Math


Basic Programming and math related problems:

Extracting digits means breaking a number like 1234 into [1, 2, 3, 4], often using string conversion or modulo/division. Counting digits involves finding how many digits are in a number using length of a string or repeated division. Reversing an integer flips its digits (e.g., 123 becomes 321), with care for signs and 32-bit limits. A palindrome number reads the same forward and backward, often checked by comparing the original and reversed number. Armstrong numbers are those equal to the sum of their digits each raised to the number of digits, like 153 = 1³ + 5³ + 3³.

To find the sum of all divisors of a number, loop through integers and check which divide the number without a remainder. Prime numbers are only divisible by 1 and themselves, and checking up to the square root is efficient. GCD, the greatest common divisor of two numbers, can be found using a simple loop or more efficiently with the Euclidean algorithm, which uses repeated modulo operations.

Extract Digits from a Given Number

def extract_digits(n):
return [int(d) for d in str(abs(n))] print(extract_digits(12345)) # Output: [1, 2, 3, 4, 5]

✅ Count Digits from a Given Number

def count_digits(n):
return len(str(abs(n))) print(count_digits(12345)) # Output: 5

✅ LeetCode Reverse Integer

def reverse(x):

INT_MAX = 2**31 - 1 INT_MIN = -2**31 sign = -1 if x < 0 else 1 x = abs(x) rev = int(str(x)[::-1]) rev *= sign if rev < INT_MIN or rev > INT_MAX: return 0 return rev print(reverse(123)) # Output: 321 print(reverse(-120)) # Output: -21

✅ LeetCode Palindrome Number

def is_palindrome(x):

if x < 0: return False return str(x) == str(x)[::-1] print(is_palindrome(121)) # Output: True print(is_palindrome(-121)) # Output: False

✅ Armstrong Number

(A number equal to the sum of its digits each raised to the power of number of digits)

def is_armstrong(n): digits = [int(d) for d in str(n)] power = len(digits) return n == sum(d ** power for d in digits) print(is_armstrong(153)) # Output: True (1³ + 5³ + 3³ = 153)

Sum of All the Divisors of a Given Number

import math def sum_of_divisors(n): return sum((i + (n//i)) for i in range(1, int(math.sqrt(n)) + 1) if n % i == 0) print(sum_of_divisors(12)) # Output: 28 (1+2+3+4+6+12)

✅ Prime Number Check

def is_prime(n):
if n <= 1: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True # Example print(is_prime(29)) # Output: True


GCD (Greatest Common Divisor: Brute Force)


def gcd_linear(a, b): gcd = 1 for i in range(1, min(a, b) + 1): if a % i == 0 and b % i == 0: gcd = i return gcd
print(gcd_linear(48, 18)) # Output: 6

GCD (Greatest Common Divisor: Euclidean Algorithm)


def gcd(a, b): while b: a, b = b, a % b return a print(gcd(48, 18)) # Output: 6

Comments

Popular posts from this blog

Poridhi: Stacks & Queues

  Stacks & Queues related problems: This collection of solutions tackles three classic problems often encountered in technical interviews and competitive programming. The Valid Parentheses problem checks whether a string has properly matched and ordered brackets using a stack. The Sliding Window Maximum efficiently finds the maximum value in every window of size k across an array using a deque, a popular sliding window pattern that ensures optimal performance. The Stock Span Problem simulates a real-world stock analysis scenario and calculates the number of consecutive days before today for which the stock price was less than or equal to today's, also utilizing a stack for efficient computation. These problems test understanding of stacks, queues, and sliding window techniques. ✅ Valid Parentheses def isValid ( s: str ) -> bool : stack = [] mapping = { ')' : '(' , '}' : '{' , ']' : '[' } for char in s: ...

Data Recovery: DevOps pre-state learning

  Data Backup Data backup is the practice of copying data from a primary to a secondary location, to protect it in case of equipment failure, cyberattack, natural disaster or other data loss events. This can include documents, media files, configuration files, machine images, operating systems, and registry files. Essentially, any data that we want to preserve can be stored as backup data. Data Restore Data restore is the process of copying backup data from secondary storage and restoring it to its original location or a new location. A restore is performed to return data that has been lost, stolen or damaged to its original condition or to move data to a new location. Pilot Light A pilot light approach minimizes the ongoing cost of disaster recovery by minimizing the active resources, and simplifies recovery at the time of a disaster because the core infrastructure requirements are all in place. Warm Standby The warm standby approach involves ensuring that there is a scaled down, ...