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.
Below are examples demonstrating the usage of the RGB to HEX color converter function:
def rgb(r, g, b): # Function code here return hex_value
hex_value = rgb(255, 255, 255) console.log(
RGB (255, 255, 255) converts to HEX: ${hex_value}
); // Output: FFFFFF
hex_value = rgb(0, 128, 192)
console.log(`RGB (0, 128, 192) converts to HEX: ${hex_value}`); // Output: 0080C0
This Python script defines a function to check if a given password is both a string and alphanumeric (i.e., contains only letters and digits). The script also includes test cases to demonstrate how the function works.
password
(str): The input string to be checked.
bool
:True
if the input is a string and contains only alphanumeric characters,False
otherwise.
- Type Check: The function first checks if the input is an instance of the string class (
str
). - Alphanumeric Check: It then checks if the string contains only alphanumeric characters using the
isalnum()
method. - Return Value: The function returns
True
if both conditions are met, otherwise it returnsFalse
.
print(alphanumeric("something123")) # Should return True (all characters are alphanumeric)
print(alphanumeric("something!")) # Should return False (contains a non-alphanumeric character '!')
print(alphanumeric(12345)) # Should return False (input is not a string)
- Ensure you have Python installed on your system.
- Copy the function definition and the test cases into a Python script file (e.g.,
alphanumeric_checker.py
). - Run the script using a Python interpreter:
python alphanumeric_checker.py
- The
isalnum()
method checks if all characters in the string are either alphabetic (letters) or numeric (digits). It returnsFalse
if the string contains any non-alphanumeric characters (such as spaces, punctuation, or symbols) or if the string is empty. - This function is useful for validating inputs like usernames, passwords, or any other field that requires only letters and digits.
The parse_int
function is a Python function designed to convert a string representation of a number into its integer form. It supports parsing numbers from zero to nineteen, multiples of ten from twenty to ninety, and handles words like "hundred", "thousand", and "million" to compute the total numerical value of the input string.
To use the parse_int
function:
- Ensure you have Python installed on your machine.
- Copy the function into your Python script or import it from a module.
- Call the function with a string parameter representing the number you want to parse.
- The function will return the integer representation of the input string.
Example usage:
print(parse_int("Ninety-nine million nine hundred ninety-nine")) # Output: 99999999
This repository contains a Python function to check the state of a Tic-Tac-Toe game board. The function determines if the game has been won by a player, is still ongoing, or has ended in a draw.
The is_solved
function evaluates a 3x3 Tic-Tac-Toe board represented as a list of lists. It returns:
1
if player 1 has won2
if player 2 has won-1
if the game is not finished (i.e., there are empty cells)0
if the game is a draw (i.e., no winner and no empty cells)
# Example board configuration
board = [
[1, 2, 2],
[2, 0, 1],
[1, 1, 2]
]