• May 31, 2023 •CodeCatch
0 likes • 0 views
# Function to check Armstrong number def is_armstrong_number(number): # Convert number to string to iterate over its digits num_str = str(number) # Calculate the sum of the cubes of each digit digit_sum = sum(int(digit) ** len(num_str) for digit in num_str) # Compare the sum with the original number if digit_sum == number: return True else: return False # Prompt user for a number number = int(input("Enter a number: ")) # Check if the number is an Armstrong number if is_armstrong_number(number): print(number, "is an Armstrong number.") else: print(number, "is not an Armstrong number.")
• Nov 19, 2022 •CodeCatch
from time import sleep def delay(fn, ms, *args): sleep(ms / 1000) return fn(*args) delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second
• Jun 1, 2023 •CodeCatch
0 likes • 1 view
filename = "data.txt" data = "Hello, World!" with open(filename, "a") as file: file.write(data)
0 likes • 4 views
from collections import Counter def find_parity_outliers(nums): return [ x for x in nums if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0] ] find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3]
• Nov 18, 2022 •AustinLeath
0 likes • 8 views
#Python 3: Fibonacci series up to n def fib(n): a, b = 0, 1 while a < n: print(a, end=' ') a, b = b, a+b print() fib(1000)
# Python code to find the URL from an input string # Using the regular expression import re def Find(string): # findall() has been used # with valid conditions for urls in string regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))" url = re.findall(regex,string) return [x[0] for x in url] # Driver Code string = 'My Profile: https://codecatch.net' print("Urls: ", Find(string))