Loading...
More Python Posts
def hex_to_rgb(hex):return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))hex_to_rgb('FFA501') # (255, 165, 1)
class Solution(object):def floodFill(self, image, sr, sc, newColor):R, C = len(image), len(image[0])color = image[sr][sc]if color == newColor: return imagedef dfs(r, c):if image[r][c] == color:image[r][c] = newColorif r >= 1: dfs(r-1, c)if r+1 < R: dfs(r+1, c)if c >= 1: dfs(r, c-1)if c+1 < C: dfs(r, c+1)dfs(sr, sc)return image
# Function to check Armstrong numberdef is_armstrong_number(number):# Convert number to string to iterate over its digitsnum_str = str(number)# Calculate the sum of the cubes of each digitdigit_sum = sum(int(digit) ** len(num_str) for digit in num_str)# Compare the sum with the original numberif digit_sum == number:return Trueelse:return False# Prompt user for a numbernumber = int(input("Enter a number: "))# Check if the number is an Armstrong numberif is_armstrong_number(number):print(number, "is an Armstrong number.")else:print(number, "is not an Armstrong number.")
from math import pidef rads_to_degrees(rad):return (rad * 180.0) / pirads_to_degrees(pi / 2) # 90.0
# Python Program to calculate the square rootnum = float(input('Enter a number: '))num_sqrt = num ** 0.5print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
def to_roman_numeral(num):lookup = [(1000, 'M'),(900, 'CM'),(500, 'D'),(400, 'CD'),(100, 'C'),(90, 'XC'),(50, 'L'),(40, 'XL'),(10, 'X'),(9, 'IX'),(5, 'V'),(4, 'IV'),(1, 'I'),]res = ''for (n, roman) in lookup:(d, num) = divmod(num, n)res += roman * dreturn resto_roman_numeral(3) # 'III'to_roman_numeral(11) # 'XI'to_roman_numeral(1998) # 'MCMXCVIII'