Untitled

 avatar
unknown
plain_text
a year ago
2.9 kB
6
Indexable
week 3 exp1 
def is_palindrome(s):
    # Convert the string to lowercase to ignore case sensitivity
    s = s.lower()
    # Remove non-alphanumeric characters from the string
    s = ''.join(char for char in s if char.isalnum())
    # Check if the string is equal to its reverse
    return s == s[::-1]

# Get input from the user
user_input = input("Enter a string: ")

# Check if the input string is a palindrome
if is_palindrome(user_input):
    print("Yes, it's a palindrome!")
else:
    print("No, it's not a palindrome.")

exp2
def is_power_of_b(a, b):
    # Base case: If a is less than b, it cannot be a power of b
    if a < b:
        return False
    # If a is equal to b, it is definitely a power of b (b^1 = b)
    if a == b:
        return True
    # Recursively check if a/b is a power of b
    return is_power_of_b(a // b, b) if a % b == 0 else False

# Get input from the user
a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))

# Check if a is a power of b
if is_power_of_b(a, b):
    print(f"{a} is a power of {b}.")
else:
    print(f"{a} is not a power of {b}.")

exp3 
def calculate_length(s):
    length = 0
    # Iterate over each character in the string
    for _ in s:
        length += 1  # Increment length for each character
    # Print the length concatenated with the string, aligned towards the extreme right
    print(f"{length:>{len(s)}} {s}")

# Get input from the user
user_input = input("Enter a string: ")

# Call the function with user input as argument
calculate_length(user_input)



wek 4 exp1
def factorial(n):
    # Base case: factorial of 0 is 1
    if n == 0:
        return 1
    # Recursive case: factorial of n is n multiplied by factorial of (n-1)
    return n * factorial(n - 1)

# Get input from the user
num = int(input("Enter a number to calculate its factorial: "))

# Calculate and print the factorial of the input number
print(f"The factorial of {num} is {factorial(num)}")

exp 2
def sum_nested_lists(nested_list):
    total_sum = 0

    # Iterate through each element in the nested list
    for element in nested_list:
        # If the element is a list (nested list), recursively call the function
        if isinstance(element, list):
            total_sum += sum_nested_lists(element)
        else:
            # If the element is an integer, add it to the total sum
            total_sum += element

    return total_sum

# Example nested list
nested_list = [[1, 2, 3], [4, 5], [6, [7, 8, 9]]]

# Calculate and print the sum of all elements in the nested list
print("Sum of all elements in the nested list:", sum_nested_lists(nested_list))


exp3
def middle(lst):
    # Check if the list has at least two elements
    if len(lst) < 3:
        return []
    # Return a new list containing all elements except the first and last
    return lst[1:-1]

# Test the function
print(middle([1, 2, 3, 4]))  # Output: [2, 3]
Editor is loading...
Leave a Comment