Wednesday, March 22, 2023

Python ispalindrome function

 def is_palindrome(input_string):

    new_string = ""
    reverse_string = ""

    for letter in input_string:

       if letter != " ":

            new_string = new_string + letter
            reverse_string = letter + reverse_string
 
    if new_string.replace(" """).lower() == reverse_string.replace(" """).lower():

        return True

    return False


****************************************************************
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

Thursday, March 16, 2023

Python : Calculate_storage program

Question : If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096 bytes of storage. A file made up of 4097 bytes will use 4096*2=8192 bytes of storage. Knowing this, can you prepare a function which calculates the total number of bytes needed to…

def calculate_storage(filesize):
    block_size = 4096
    full_blocks = filesize//4096
    partial_block_remainder = filesize%4096
    if partial_block_remainder > 0:
        return (full_blocks+1)*4096
    return full_blocks*4096

Output:
print(calculate_storage(1)) # Should be 4096 print(calculate_storage(4096)) # Should be 4096 print(calculate_storage(4097)) # Should be 8192 print(calculate_storage(6000)) # Should be 8192