Loading...
More Python Posts
import calendar# Prompt user for year and monthyear = int(input("Enter the year: "))month = int(input("Enter the month: "))# Create a calendar objectcal = calendar.monthcalendar(year, month)# Display the calendarprint(calendar.month_name[month], year)print("Mon Tue Wed Thu Fri Sat Sun")for week in cal:for day in week:if day == 0:print(" ", end="")else:print(str(day).rjust(2), " ", end="")print()
import anytree as atimport random as rm# Generate a tree with node_count many nodes. Each has a number key that shows when it was made and a randomly selected color, red or white.def random_tree(node_count):# Generates the list of nodesnodes = []for i in range(node_count):test = rm.randint(1,2)if test == 1:nodes.append(at.Node(str(i),color="white"))else:nodes.append(at.Node(str(i),color="red"))#Creates the various main branchesfor i in range(node_count):for j in range(i, len(nodes)):test = rm.randint(1,len(nodes))if test == 1 and nodes[j].parent == None and (not nodes[i] == nodes[j]):nodes[j].parent = nodes[i]#Collects all the main branches into a single tree with the first node being the rootfor i in range(1, node_count):if nodes[i].parent == None and (not nodes[i] == nodes[0]):nodes[i].parent = nodes[0]return nodes[0]
magnitude = lambda bits: 1_000_000_000_000_000_000 % (2 ** bits)sign = lambda bits: -1 ** (1_000_000_000_000_000_000 // (2 ** bits))print("64 bit sum:", magnitude(64) * sign(64))print("32 bit sum:", magnitude(32) * sign(32))print("16 bit sum:", magnitude(16) * sign(16))
# 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]),
from statistics import median, mean, modedef print_stats(array):print(array)print("median =", median(array))print("mean =", mean(array))print("mode =", mode(array))print()print_stats([1, 2, 3, 3, 4])print_stats([1, 2, 3, 3])
def when(predicate, when_true):return lambda x: when_true(x) if predicate(x) else xdouble_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)print(double_even_numbers(2)) # 4print(double_even_numbers(1)) # 1