## sum of powers

0 likes • Nov 19, 2022 • 0 views

Python

Loading...

## More Python Posts

# Python program for implementation of Bubble Sortdef bubbleSort(arr):n = len(arr)# Traverse through all array elementsfor i in range(n-1):# range(n) also work but outer loop will repeat one time more than needed.# Last i elements are already in placefor j in range(0, n-i-1):# traverse the array from 0 to n-i-1# Swap if the element found is greater# than the next elementif arr[j] > arr[j+1] :arr[j], arr[j+1] = arr[j+1], arr[j]# Driver code to test abovearr = [64, 34, 25, 12, 22, 11, 90]bubbleSort(arr)print ("Sorted array is:")for i in range(len(arr)):print ("%d" %arr[i]),

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 mysql.connectormydb = mysql.connector.connect(host="localhost",user="yourusername",password="yourpassword")mycursor = mydb.cursor()mycursor.execute("CREATE DATABASE mydatabase")

# Prompt user for a decimal numberdecimal = int(input("Enter a decimal number: "))# Convert decimal to binarybinary = bin(decimal)# Convert decimal to hexadecimalhexadecimal = hex(decimal)# Display the resultsprint("Binary:", binary)print("Hexadecimal:", hexadecimal)

# Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.# For example, if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”.# Python program to print all primes smaller than or equal to# n using Sieve of Eratosthenesdef SieveOfEratosthenes(n):# Create a boolean array "prime[0..n]" and initialize# all entries it as true. A value in prime[i] will# finally be false if i is Not a prime, else true.prime = [True for i in range(n + 1)]p = 2while (p * p <= n):# If prime[p] is not changed, then it is a primeif (prime[p] == True):# Update all multiples of pfor i in range(p * 2, n + 1, p):prime[i] = Falsep += 1prime[0]= Falseprime[1]= False# Print all prime numbersfor p in range(n + 1):if prime[p]:print (p)# driver programif __name__=='__main__':n = 30print("Following are the prime numbers smaller")print("than or equal to ", n)print("than or equal to ", n)SieveOfEratosthenes(n)

import copybegining = [False,False,False,False,False,None,True,True,True,True,True]#False = black True = whiteits = [0]def swap(layout, step):layoutCopy = copy.deepcopy(layout)layoutCopy[(step[0]+step[1])], layoutCopy[step[1]] = layoutCopy[step[1]], layoutCopy[(step[0]+step[1])]return layoutCopydef isSolved(layout):for i in range(len(layout)):if(layout[i] == False):return (i >= (len(layout)/2))def recurse(layout, its, steps = []):if isSolved(layout):its[0] += 1print(layout,list(x[0] for x in steps))returnstep = Nonefor i in range(len(layout)):if(layout[i] == None):if(i >= 1): #If the empty space could have something to the leftif(layout[i - 1] == False):step = [-1,i]recurse(swap(layout,step), its, (steps+[step]))if(i > 1): #If the empty space could have something 2 to the leftif(layout[i - 2] == False):step = [-2,i]recurse(swap(layout,step), its, (steps+[step]))if(i < (len(layout)-1)): #If the empty space could have something to the rightif(layout[i + 1] == True):step = [1,i]recurse(swap(layout,step), its, (steps+[step]))if(i < (len(layout)-2)): #If the empty space could have something to the rightif(layout[i + 2] == True):step = [2,i]recurse(swap(layout,step), its, (steps+[step]))its[0] += 1#return Nonerecurse(begining,its,[])print(its[0])