Loading...
More Python Posts
# 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.")
# Python code to demonstrate# method to remove i'th character# Naive Method# Initializing Stringtest_str = "CodeCatch"# Printing original stringprint ("The original string is : " + test_str)# Removing char at pos 3# using loopnew_str = ""for i in range(len(test_str)):if i != 2:new_str = new_str + test_str[i]# Printing string after removalprint ("The string after removal of i'th character : " + new_str)
def generate_floyds_triangle(num_rows):triangle = []number = 1for row in range(num_rows):current_row = []for _ in range(row + 1):current_row.append(number)number += 1triangle.append(current_row)return triangledef display_floyds_triangle(triangle):for row in triangle:for number in row:print(number, end=" ")print()# Prompt the user for the number of rowsnum_rows = int(input("Enter the number of rows for Floyd's Triangle: "))# Generate Floyd's Trianglefloyds_triangle = generate_floyds_triangle(num_rows)# Display Floyd's Triangledisplay_floyds_triangle(floyds_triangle)
import pandas as pdx = pd.read_excel(FILE_NAME)print(x)
# Python program for implementation of Radix Sort# A function to do counting sort of arr[] according to# the digit represented by exp.def countingSort(arr, exp1):n = len(arr)# The output array elements that will have sorted arroutput = [0] * (n)# initialize count array as 0count = [0] * (10)# Store count of occurrences in count[]for i in range(0, n):index = (arr[i]/exp1)count[int((index)%10)] += 1# Change count[i] so that count[i] now contains actual# position of this digit in output arrayfor i in range(1,10):count[i] += count[i-1]# Build the output arrayi = n-1while i>=0:index = (arr[i]/exp1)output[ count[ int((index)%10) ] - 1] = arr[i]count[int((index)%10)] -= 1i -= 1# Copying the output array to arr[],# so that arr now contains sorted numbersi = 0for i in range(0,len(arr)):arr[i] = output[i]# Method to do Radix Sortdef radixSort(arr):# Find the maximum number to know number of digitsmax1 = max(arr)# Do counting sort for every digit. Note that instead# of passing digit number, exp is passed. exp is 10^i# where i is current digit numberexp = 1while max1/exp > 0:countingSort(arr,exp)exp *= 10# Driver code to test abovearr = [ 170, 45, 75, 90, 802, 24, 2, 66]radixSort(arr)for i in range(len(arr)):print(arr[i]),
print('hello, world')