Skip to content

This repository is a dynamic collection that grows as I learn, test, and update my programming skills. It includes tasks from platforms like CodeWars, where developers enhance their coding abilities through gamified challenges, as well as various simple projects I’ve created.

Notifications You must be signed in to change notification settings

rokasauras/light_practice

Repository files navigation

Python Utility Functions

This repository contains multiple Python functions designed to solve distinct problems, listed below:


Converting a Binary Array to a Decimal Number - binary_array_decoder

Description

This function converts a binary array (series of 0s and 1s) to its corresponding decimal (base-10) value.

Features

  • Handles binary arrays of any length.
  • Efficiently calculates the decimal value using bitwise operations.

Finding the Complementary DNA Strand

Description

Automates finding the complementary DNA strand based on base pairing rules:

  • A pairs with T
  • C pairs with G

Features

  • Takes a DNA strand (string) as input.
  • Outputs the complementary DNA strand.
  • Handles both uppercase and lowercase letters.

Paino Tiles Game Bot

Description

Automates playing the Paino Tiles game by detecting black pixels at user-specified click locations.

Features

  • Allows the user to define click coordinates.
  • Analyses the screen for black pixels at those coordinates.

No Duplicates Script

Description

Ensures a sequence (list or array) contains no duplicate elements by comparing each element to the previous ones.

Features

  • Takes a sequence of elements as input.
  • Removes any duplicate elements from the sequence.
  • Maintains the order of unique elements.

Highest and Lowest Numbers

Description

The high_and_low function takes a string of numbers separated by spaces and returns the highest and lowest numbers in the sequence.

Features

  • 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() and max() functions for optimal performance.
  • String Formatting: Uses Python's f-string for clear and concise output formatting.

Hashtag Generator

Description

Generates a hashtag from a given string.

Features

  • Removes spaces from the input string.
  • Capitalises the first letter of each word.
  • Ensures non-empty input for hashtag generation.

Index Position

Description

Converts each alphabetic letter in the input text to its corresponding position in the alphabet.

Features

  • 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.

Space Identifier

Description

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.

Usage

Call solution(s) where s is the camelCase string you want to convert. It returns the converted string.

Example

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 perfect square

Description

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.

Features

  • 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.


Tail Swap

Description

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.

Features

  • 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.

Example

Given the input ["abc:123", "cde:456"], the function will return ["abc:456", "cde:123"].


RGB to HEX Color Converter

Description

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.

Features

  • 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.

Example Usage

Below are examples demonstrating the usage of the RGB to HEX color converter function:

Example 1: Convert RGB (255, 255, 255) to HEX

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

Example 2: Convert RGB (0, 128, 192) to HEX

hex_value = rgb(0, 128, 192)
console.log(`RGB (0, 128, 192) converts to HEX: ${hex_value}`);    // Output: 0080C0

Alphanumeric Checker

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.

Function Definition

alphanumeric(password)

Parameters

  • password (str): The input string to be checked.

Returns

  • bool: True if the input is a string and contains only alphanumeric characters, False otherwise.

How It Works

  1. Type Check: The function first checks if the input is an instance of the string class (str).
  2. Alphanumeric Check: It then checks if the string contains only alphanumeric characters using the isalnum() method.
  3. Return Value: The function returns True if both conditions are met, otherwise it returns False.

Example Usage

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)

Running the Script

  1. Ensure you have Python installed on your system.
  2. Copy the function definition and the test cases into a Python script file (e.g., alphanumeric_checker.py).
  3. Run the script using a Python interpreter:
python alphanumeric_checker.py

Additional Notes

  • The isalnum() method checks if all characters in the string are either alphabetic (letters) or numeric (digits). It returns False 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.

Number into words

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.

Usage

To use the parse_int function:

  1. Ensure you have Python installed on your machine.
  2. Copy the function into your Python script or import it from a module.
  3. Call the function with a string parameter representing the number you want to parse.
  4. The function will return the integer representation of the input string.

Example

Example usage:

print(parse_int("Ninety-nine million nine hundred ninety-nine"))  # Output: 99999999

About

This repository is a dynamic collection that grows as I learn, test, and update my programming skills. It includes tasks from platforms like CodeWars, where developers enhance their coding abilities through gamified challenges, as well as various simple projects I’ve created.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages