This repository contains multiple Python functions designed to solve distinct problems, listed below:
This function converts a binary array (series of 0s and 1s) to its corresponding decimal (base-10) value.
- Handles binary arrays of any length.
- Efficiently calculates the decimal value using bitwise operations.
Automates finding the complementary DNA strand based on base pairing rules:
- A pairs with T
- C pairs with G
- Takes a DNA strand (string) as input.
- Outputs the complementary DNA strand.
- Handles both uppercase and lowercase letters.
Automates playing the Paino Tiles game by detecting black pixels at user-specified click locations.
- Allows the user to define click coordinates.
- Analyses the screen for black pixels at those coordinates.
Ensures a sequence (list or array) contains no duplicate elements by comparing each element to the previous ones.
- Takes a sequence of elements as input.
- Removes any duplicate elements from the sequence.
- Maintains the order of unique elements.
The high_and_low
function takes a string of numbers separated by spaces and returns the highest and lowest numbers in the sequence.
- Input: Accepts a string of numbers separated by spaces.
- Output: Returns a string containing the highest and lowest numbers separated by a space.
- Error Handling: Assumes valid input format (numbers separated by spaces).
- Efficiency: Utilises Python's built-in
min()
andmax()
functions for optimal performance. - String Formatting: Uses Python's f-string for clear and concise output formatting.
Generates a hashtag from a given string.
- Removes spaces from the input string.
- Capitalises the first letter of each word.
- Ensures non-empty input for hashtag generation.
Converts each alphabetic letter in the input text to its corresponding position in the alphabet.
- Iterates over each character in the string.
- Checks if the character is an alphabetic letter.
- Converts each alphabetic letter to lowercase.
- Computes the position using the ASCII value.
- Returns the list of alphabetic positions.
The solution
function in Python converts a camelCase formatted string into a spaced format where each uppercase letter (except the first) is preceded by a space.
Call solution(s)
where s
is the camelCase string you want to convert. It returns the converted string.
print(solution("camelCasing")) # Output: "camel Casing"
This function is useful for formatting camelCase strings into a more readable format, often used in display or user interface contexts where separation between words is needed.
The find_next_square
function calculates and returns the next perfect square greater than a given number sq
or returns -1
if sq
is not a perfect square.
- Identifies the next perfect square greater than a given number.
- Returns
-1
if the input number is not a perfect square.
Example usage:
print(find_next_square(1234)) # Output: 1296 (36^2)
This function is handy for scenarios where determining the next perfect square number is required, such as in mathematical computations or algorithms.
This is a Python function that swaps the parts of two input strings after the colon (:
). The function takes a list of two strings as input and returns a list of two strings with the parts after the colon swapped.
- Efficiently swaps the parts after the colon in two strings.
- Utilizes list comprehensions for concise and readable code.
- Demonstrates the use of list indexing and string manipulation in Python.
Given the input ["abc:123", "cde:456"]
, the function will return ["abc:456", "cde:123"]
.
This project provides a simple Python function to convert RGB color values to their hexadecimal (HEX) representation. The function ensures the RGB values are within the valid range (0-255) and accurately converts them to a two-digit hexadecimal format for each color component.
- Converts individual RGB values to a two-digit hexadecimal string.
- Ensures RGB values are clamped within the 0-255 range.
- Combines the hexadecimal values of Red, Green, and Blue components into a single HEX color code.
- Easy to integrate and use in other Python projects.
<p>Below are examples demonstrating the usage of the RGB to HEX color converter function:</p>
<h3>Example 1: Convert RGB (255, 255, 255) to HEX</h3>
<pre><code>
def rgb(r, g, b): def to_hex(value): # Ensure the value is within the range 0-255 value = max(0, min(value, 255)) # Convert the integer part to a hexadecimal string digit1 = value // 16 # Convert the remainder to a hexadecimal string digit2 = value % 16 # Convert both to hexadecimal characters and combine them return f'{digit1:X}{digit2:X}'
# Combine all three parts
return to_hex(r) + to_hex(g) + to_hex(b)
hex_value = rgb(255, 255, 255)
console.log(RGB (255, 255, 255) converts to HEX: ${hex_value}
); // Output: FFFFFF
<h3>Example 2: Convert RGB (0, 128, 192) to HEX</h3>
<pre><code>
// Example 2
hex_value = rgb(0, 128, 192)
console.log(RGB (0, 128, 192) converts to HEX: ${hex_value}
); // Output: 0080C0