Skip to main content

Binary search

0 likes • Sep 22, 2023 • 11 views
Python
Loading...

More Python Posts

Convert Decimal to Binary and Hexadecimal

0 likes • May 31, 2023 • 0 views
Python
# Prompt user for a decimal number
decimal = int(input("Enter a decimal number: "))
# Convert decimal to binary
binary = bin(decimal)
# Convert decimal to hexadecimal
hexadecimal = hex(decimal)
# Display the results
print("Binary:", binary)
print("Hexadecimal:", hexadecimal)

Differentiate Between type() and instance()

0 likes • May 31, 2023 • 0 views
Python
class Rectangle:
pass
class Square(Rectangle):
pass
rectangle = Rectangle()
square = Square()
print(isinstance(rectangle, Rectangle)) # True
print(isinstance(square, Rectangle)) # True
print(isinstance(square, Square)) # True
print(isinstance(rectangle, Square)) # False

UNT CSCE 2100 Question 1

0 likes • Nov 18, 2022 • 1 view
Python
#question1.py
def rose(n) :
if n==0 :
yield []
else :
for k in range(0,n) :
for l in rose(k) :
for r in rose(n-1-k) :
yield [l]+[r]+[r]
def start(n) :
for x in rose(n) :
print(x) #basically I am printing x for each rose(n) file
print("starting program: \n")
start(2) # here is where I call the start function

Palindrome checker

0 likes • Nov 19, 2022 • 3 views
Python
# function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")

Color Gradient

0 likes • Mar 10, 2021 • 0 views
Python
color2 = (60, 74, 172)
color1 = (19, 28, 87)
percent = 1.0
for i in range(101):
resultRed = round(color1[0] + percent * (color2[0] - color1[0]))
resultGreen = round(color1[1] + percent * (color2[1] - color1[1]))
resultBlue = round(color1[2] + percent * (color2[2] - color1[2]))
print((resultRed, resultGreen, resultBlue))
percent -= 0.01

Create a Floyd’s Triangle

0 likes • May 31, 2023 • 0 views
Python
def generate_floyds_triangle(num_rows):
triangle = []
number = 1
for row in range(num_rows):
current_row = []
for _ in range(row + 1):
current_row.append(number)
number += 1
triangle.append(current_row)
return triangle
def display_floyds_triangle(triangle):
for row in triangle:
for number in row:
print(number, end=" ")
print()
# Prompt the user for the number of rows
num_rows = int(input("Enter the number of rows for Floyd's Triangle: "))
# Generate Floyd's Triangle
floyds_triangle = generate_floyds_triangle(num_rows)
# Display Floyd's Triangle
display_floyds_triangle(floyds_triangle)