Loading...
More Python Posts
# Prompt user for base and heightbase = float(input("Enter the base of the triangle: "))height = float(input("Enter the height of the triangle: "))# Calculate the areaarea = (base * height) / 2# Display the resultprint("The area of the triangle is:", area)
#SetsU = {0,1,2,3,4,5,6,7,8,9}P = {1,2,3,4}Q = {4,5,6}R = {3,4,6,8,9}def set2bits(xs,us) :bs=[]for x in us :if x in xs :bs.append(1)else:bs.append(0)assert len(us) == len(bs)return bsdef union(set1,set2) :finalSet = set()bitList1 = set2bits(set1, U)bitList2 = set2bits(set2, U)for i in range(len(U)) :if(bitList1[i] or bitList2[i]) :finalSet.add(i)return finalSetdef intersection(set1,set2) :finalSet = set()bitList1 = set2bits(set1, U)bitList2 = set2bits(set2, U)for i in range(len(U)) :if(bitList1[i] and bitList2[i]) :finalSet.add(i)return finalSetdef compliment(set1) :finalSet = set()bitList = set2bits(set1, U)for i in range(len(U)) :if(not bitList[i]) :finalSet.add(i)return finalSetdef implication(a,b):return union(compliment(a), b)################################################################################################################# Problems 1-6 ###################################################################################################################################p \/ (q /\ r) = (p \/ q) /\ (p \/ r)def prob1():return union(P, intersection(Q,R)) == intersection(union(P,Q), union(P,R))#p /\ (q \/ r) = (p /\ q) \/ (p /\ r)def prob2():return intersection(P, union(Q,R)) == union(intersection(P,Q), intersection(P,R))#~(p /\ q) = ~p \/ ~qdef prob3():return compliment(intersection(P,R)) == union(compliment(P), compliment(R))#~(p \/ q) = ~p /\ ~qdef prob4():return compliment(union(P,Q)) == intersection(compliment(P), compliment(Q))#(p=>q) = (~q => ~p)def prob5():return implication(P,Q) == implication(compliment(Q), compliment(P))#(p => q) /\ (q => r) => (p => r)def prob6():return implication(intersection(implication(P,Q), implication(Q,R)), implication(P,R))print("Problem 1: ", prob1())print("Problem 2: ", prob2())print("Problem 3: ", prob3())print("Problem 4: ", prob4())print("Problem 5: ", prob5())print("Problem 6: ", prob6())'''Problem 1: TrueProblem 2: TrueProblem 3: TrueProblem 4: TrueProblem 5: TrueProblem 6: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}'''
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]
import random# Define the ranks, suits, and create a deckranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']deck = [(rank, suit) for rank in ranks for suit in suits]# Shuffle the deckrandom.shuffle(deck)# Display the shuffled deckfor card in deck:print(card[0], "of", card[1])
# Listlst = [1, 2, 3, 'Alice', 'Alice']# One-Linerindices = [i for i in range(len(lst)) if lst[i]=='Alice']# Resultprint(indices)# [3, 4]
#84 48 13 20 61 20 33 97 34 45 6 63 71 66 24 57 92 74 6 25 51 86 48 15 64 55 77 30 56 53 37 99 9 59 57 61 30 97 50 63 59 62 39 32 34 4 96 51 8 86 10 62 16 55 81 88 71 25 27 78 79 88 92 50 16 8 67 82 67 37 84 3 33 4 78 98 39 64 98 94 24 82 45 3 53 74 96 9 10 94 13 79 15 27 56 66 32 81 77# xor a list of integers to find the lonely integerres = a[0]for i in range(1,len(a)):res = res ^ a[i]