Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.6 kB
6
Indexable
Never
race_distance_km = 10
race_time_minutes = 43
race_time_seconds = 30

race_time_hours=(race_time_minutes/60)+(race_time_seconds/3600)
race_distance_miles=race_distance_km/1.61
average_time_per_mile=race_time_hours/race_distance_miles
average_speed_mph=race_distance_miles/race_time_hours

print("Average time per mile:",average_time_per_mile,"hours")
print("Average Speed:",average_speed_mph,"miles per hours")

..................................................................................................................

def calculate_simple_interest(principal, time, rate):
    interest = (principal * time * rate) / 100
    return interest
    
principal = float(input("Enter the principal amount (P): "))
time = float(input("Enter the time period in years (T): "))
rate = float(input("Enter the rate of interest per annum (R): "))
    
simple_interest = calculate_simple_interest(principal, time, rate)
print("Simple Interest:", simple_interest)

...................................................................................................................

def generate_fibonacci(n):
    fibonacci_sequence = [0, 1]
    
    for i in range(2, n):
        next_fibonacci = fibonacci_sequence[-1] + fibonacci_sequence[-2]
        fibonacci_sequence.append(next_fibonacci)
        
    return fibonacci_sequence

n = int(input("Enter the number of Fibonacci numbers to generate: "))
fibonacci_numbers = generate_fibonacci(n)

print("First", n, "Fibonacci numbers:", fibonacci_numbers)

...................................................................................................................

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

n = int(input("Enter a number to calculate its factorial: "))
result = factorial(n)
print("Factorial of", n, "is", result)

...................................................................................................................

def square_of_nat_numbers(n):
    return sum(i**2 for i in range(1,n+1))
n=7
result= square_of_nat_numbers(n)
print("Square of the first",n,"natural no is:",result)

...................................................................................................................

def Sum_of_numbers(*args):
    return sum(args)
    
result = Sum_of_numbers(1,2,3,4,5)
print("Sum of numbers:",result)

....................................................................................................................

def check_format(a,b,c,n):
    if n<=2:
        print("the value of n must be greater than 2.")
        return
    
    if a**n + b**n == c**n:
        print("Holy Smokes,Fermet was wromg!")
    else:
        print("No,that doesn't work.")
    
a=3
b=4
c=4
n=7
check_format(a,b,c,n)

...................................................................................................................

def is_palindrome(s:str)->bool:
    
    s = s.lower()
    s = ''.join(c for c in s if c.isalnum())
    return s==s[::-1]
    
print(is_palindrome('racecar'))
print(is_palindrome('hello'))
print(is_palindrome('A man a plan a canal Panama'))

...................................................................................................................

def is_power(a: int, b:int)->bool:
    if a == 0:
        return False
    while a % b ==0:
        a = a//b
    return a == 1 or a == b
    
print(is_power(8,2))
print(is_power(27,3))
print(is_power(1024,4))
print(is_power(12,3))
print(is_power(343,7))
print(is_power(256,6))

...................................................................................................................

def count_chars(stri):
    count = {}
    for char in stri:
        if char in count:
            count[char] += 1
        else:
            count[char] = 1
    return count

print(count_chars("The function tries to print a count of each character in given 'string'"))

...................................................................................................................

def factorial(n):
    
    if n<0:
        raise ValueError
    elif n ==0:
        return 1
    else:
        return n * factorial(n-1)
        
print(factorial(5))
print(factorial(9))

...................................................................................................................

def add_nested_elements(nested_list):
    total = 0
    for element in nested_list:
        if isinstance(element, list):
            total += add_nested_elements(element)
        elif isinstance(element, int):
            total += element
        else:
            raise ValueError("Nested list contains non-integer values.")
    return total

nested_list = [1, 2, [3, 4], [5, [6, 7], 8], 9]
print(add_nested_elements(nested_list))

...................................................................................................................

def middle(lst):
    if len(lst) < 2:
        raise ValueError("List must contain at least two elements")
    else:
        return lst[1:-1]

lst = [5, 6, 7, 8]
print(middle(lst))

...................................................................................................................

char_list=['h','e','l','l','o']

char_string=''.join(char_list)

print(char_string)

...................................................................................................................
 
my_list = [5, 10, 40, 15, 20, 25, 30]
item = 20

index = my_list.index(item)
print("The index of", item, "in my_list is:", index)

..................................................................................................................

Leave a Comment