Posts

Showing posts from September, 2023

Celsius_to_Fahrenheit (beginner)

Write a python program/function asking if you want to convert Fahrenheit to Celsius or Celsius to Fahrenheit. Then convert. Temperature in degrees  Fahrenheit (°F) = (Temperature in degrees Celsius (°C) * 9/5) + 32 Temperature in degrees Celsius  (°C) =  (Temperature in degrees Fahrenheit (°F) * 5/9) - 32 [this should be VERY easy] scroll down to see my code... def CtoF_FtoC(inpt, cORf): if inpt == 'C to F': c = cORf f = (c * 9/5) + 32 return f if inpt == 'F to C': f = cORf c = (f * 5/9) - 32 return c

Sieve_of_Erathosthenes

Write an algorithm (function) that uses the sieve of Erathosthenes to calculate/find all prime numbers in the range of the parameter (int). Information on the sieve of Erathosthenes (famous Greek mathematician and geographer):  https://byjus.com/maths/sieve-of-eratosthenes/ scroll down to see my code... def print_primes_sieveErathosthenes(n: int): import math prime_num = [True for _ in range(n + 1)] prime_num[0] = prime_num[1] = False i = 2 while i <= (math.sqrt(n) + 1): if prime_num[i]: for a in range(i * 2, n + 1, i): prime_num[a] = False i += 1 for p in range(n + 1): if prime_num[p]: print(p, end=', ')

check_duplicate

Write a function in Python to check duplicate letters. It must accept a string, i.e., a sentence. The function should return True and the letters repeated if the sentence has any word with duplicate letters, else return False.  Possible inputs/outputs: 'mississippi': 'True, i, s, p' , 'hello': 'l', 'bonjour': 'o', 'twinkle': 'False' if anyone has a different solution or a slightly altered version, please comment! Scroll down to view my solution... _____________________ def check_duplicate(a: str): global i, j x = sorted(list(a)) duplicate = [] j = 'no' for i in range(len(x) - 1): if x[i] == x[i + 1]: i += 1 j = 'yes' elif j == 'yes': duplicate.append(x[i]) j = 'no' if j == 'yes': duplicate.append(x[i]) if not duplicate: return False else: return True, duplicate