Python Logic for Sequences and Base Conversions

Classified in Computers

Written on in English with a size of 3.82 KB

Tile Sequence Validation Logic

The following function determines if a sequence of tiles is valid by checking their compatibility and adjusting their orientation if necessary.

def is_valid_sequence(seq: list[Tile]) -> bool:
    if len(seq) < 2:
        res = True
    else:
        if not are_compatible(seq[0], seq[1]):
            seq[0] = seq[0][1], seq[0][0]
        i = 1
        while i < len(seq) and are_compatible(seq[i - 1], seq[i]):
            if seq[i - 1][1] != seq[i][0]:
                seq[i] = seq[i][1], seq[i][0]
            i += 1
        res = i == len(seq)
    return res

Square-Free Number Verification

This function checks if a positive integer is square-free, meaning no prime factor has an exponent greater than one.

def is_free(num: int) -> bool:
    assert num > 0
    still_free = True
    factor = 2
    while num > 1 and still_free:
        exp = 0
        while num % factor == 0:
            num = num // factor
            exp += 1
        still_free = exp <= 1
        factor += 1
    return still_free

Limited Coin Change Algorithm

A greedy approach to calculate change using a limited supply of specific coin denominations.

def change_limited(amount: int, coins: list[int], limit: list[int]) -> tuple[bool, list[int]]:
    change = []
    i = 0
    while i < len(coins):
        ncoins = amount // coins[i]
        if ncoins > limit[i]:
            ncoins = limit[i]
        change.append(ncoins)
        amount = amount - coins[i] * ncoins
        i += 1
    return amount == 0, change

String Membership and Word Extraction

These utility functions handle character searches and word parsing within strings.

def is_in(s: str, ltr: str) -> bool:
    assert len(ltr) == 1, f'Error, la \'{ltr}\' cadena debería tener longitud 1'
    i = 0
    while i < len(s) and s[i] != ltr:
        i = i + 1
    return i < len(s)

def pos_next_word(txt: str, pos: int) -> tuple[str, int]:
    i = pos_next_letter(txt, pos)
    word = ''
    while i < len(txt) and not is_punctuation(txt[i]):
        word += txt[i]
        i += 1
    return word, i

Unique Word Filtering and Splitting

Functions to extract unique words and split text into lists based on custom logic.

def different_words(words: list[str]) -> list[str]:
    different = []
    for w in words:
        lw = lower(w)
        if index(different, lw) == -1:
            different.append(lw)
    return different

def split(txt: str) -> list[str]:
    i = 0
    word_lst = []
    while i < len(txt):
        word, i = pos_next_word(txt, i)
        if word != '':
            word_lst.append(word)
    return word_lst

Numerical Base Conversion Functions

The following functions convert integers to different bases and vice versa, supporting bases up to 10 and extended formats.

def rbase(num: int, base: int) -> str:
    assert 1 <= base <= 10 and num >= 0
    result = ''
    digits = '0123456789'
    if num == 0:
        result = '0'
    while num > 0:
        remainder = num % base
        result = digits[remainder] + result
        num = num // base
    return result

def nbase(repre: str, base: int) -> int:
    assert len(repre) > 0 and base > 1
    res = 0
    i = 0
    while i < len(repre):
        res = res * base + int(repre[i])
        i += 1
    return res

def rbase_extended(num: int, base: int) -> str:
    # digits = "0123456789ABCDEF..." o hay que ponerlos todos
    digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    res = ""
    if num == 0:
        res = "0"
    while num > 0:
        res = digits[num % base] + res
        num = num // base
    return res

Related entries: