Loading...
More Python Posts
prime_lists=[] # a list to store the prime numbersdef prime(n): # define prime numbersif n <= 1:return False# divide n by 2... up to n-1for i in range(2, n):if n % i == 0: # the remainder should'nt be a 0return Falseelse:prime_lists.append(n)return Truefor n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30prime(n)check=0 # a var to limit the output to 10 onlyfor n in prime_lists:for x in prime_lists:val= n *xif (val > 1000 ):check=check +1if (check <10) :print("the num is:", val , "=",n , "* ", x )break
#Loop back to this point once code finishesloop = 1while (loop < 10):#All the questions that the program asks the usernoun = input("Choose a noun: ")p_noun = input("Choose a plural noun: ")noun2 = input("Choose a noun: ")place = input("Name a place: ")adjective = input("Choose an adjective (Describing word): ")noun3 = input("Choose a noun: ")#Displays the story based on the users inputprint ("------------------------------------------")print ("Be kind to your",noun,"- footed", p_noun)print ("For a duck may be somebody's", noun2,",")print ("Be kind to your",p_noun,"in",place)print ("Where the weather is always",adjective,".")print ()print ("You may think that is this the",noun3,",")print ("Well it is.")print ("------------------------------------------")#Loop back to "loop = 1"loop = loop + 1
from collections import defaultdictdef collect_dictionary(obj):inv_obj = defaultdict(list)for key, value in obj.items():inv_obj[value].append(key)return dict(inv_obj)ages = {'Peter': 10,'Isabel': 10,'Anna': 9,}collect_dictionary(ages) # { 10: ['Peter', 'Isabel'], 9: ['Anna'] }
def generate_pascals_triangle(num_rows):triangle = []for row in range(num_rows):# Initialize the row with 1current_row = [1]# Calculate the values for the current rowif row > 0:previous_row = triangle[row - 1]for i in range(len(previous_row) - 1):current_row.append(previous_row[i] + previous_row[i + 1])# Append 1 at the end of the rowcurrent_row.append(1)# Add the current row to the triangletriangle.append(current_row)return triangledef display_pascals_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 Pascal's Triangle: "))# Generate Pascal's Trianglepascals_triangle = generate_pascals_triangle(num_rows)# Display Pascal's Triangledisplay_pascals_triangle(pascals_triangle)
import randomclass Node:def __init__(self, c):self.left = Noneself.right = Noneself.color = cdef SetColor(self,c) :self.color = cdef PrintNode(self) :print(self.color)def insert(s, root, i, n):if i < n:temp = Node(s[i])root = temproot.left = insert(s, root.left,2 * i + 1, n)root.right = insert(s, root.right,2 * i + 2, n)return rootdef MakeTree(s) :list = insert(s,None,0,len(s))return listdef MakeSet() :s = []count = random.randint(7,12)for _ in range(count) :color = random.randint(0,1) == 0 and "Red" or "White"s.append(color)return sdef ChangeColor(root) :if (root != None) :if (root.color == "White") :root.SetColor("Red")ChangeColor(root.left)ChangeColor(root.right)def PrintList(root) :if root.left != None :PrintList(root.left)else :root.PrintNode()if root.right != None :PrintList(root.right)else :root.PrintNode()t1 = MakeTree(MakeSet())print("Original Colors For Tree 1:\n")PrintList(t1)ChangeColor(t1)print("New Colors For Tree 1:\n")PrintList(t1)t2 = MakeTree(MakeSet())print("Original Colors For Tree 2:\n")PrintList(t2)ChangeColor(t2)print("New Colors For Tree 2:\n")PrintList(t2)t3 = MakeTree(MakeSet())print("Original Colors For Tree 3:\n")PrintList(t3)ChangeColor(t3)print("New Colors For Tree 3:\n")PrintList(t3)
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()