Loading...
More Python Posts
# Python binary search functiondef binary_search(arr, target):left = 0right = len(arr) - 1while left <= right:mid = (left + right) // 2if arr[mid] == target:return midelif arr[mid] < target:left = mid + 1else:right = mid - 1return -1# Usagearr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]target = 7result = binary_search(arr, target)if result != -1:print(f"Element is present at index {result}")else:print("Element is not present in array")
# importing the modulesimport osimport shutil# getting the current working directorysrc_dir = os.getcwd()# printing current directoryprint(src_dir)# copying the filesshutil.copyfile('test.txt', 'test.txt.copy2') #copy src to dst# printing the list of new filesprint(os.listdir())
filename = "data.txt"with open(filename, "r") as file:file_contents = file.readlines()file_contents = [line.strip() for line in file_contents]print("File contents:")for line in file_contents:print(line)
def generate_floyds_triangle(num_rows):triangle = []number = 1for row in range(num_rows):current_row = []for _ in range(row + 1):current_row.append(number)number += 1triangle.append(current_row)return triangledef display_floyds_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 Floyd's Triangle: "))# Generate Floyd's Trianglefloyds_triangle = generate_floyds_triangle(num_rows)# Display Floyd's Triangledisplay_floyds_triangle(floyds_triangle)
# Python program for Plotting Fibonacci# spiral fractal using Turtleimport turtleimport mathdef fiboPlot(n):a = 0b = 1square_a = asquare_b = b# Setting the colour of the plotting pen to bluex.pencolor("blue")# Drawing the first squarex.forward(b * factor)x.left(90)x.forward(b * factor)x.left(90)x.forward(b * factor)x.left(90)x.forward(b * factor)# Proceeding in the Fibonacci Seriestemp = square_bsquare_b = square_b + square_asquare_a = temp# Drawing the rest of the squaresfor i in range(1, n):x.backward(square_a * factor)x.right(90)x.forward(square_b * factor)x.left(90)x.forward(square_b * factor)x.left(90)x.forward(square_b * factor)# Proceeding in the Fibonacci Seriestemp = square_bsquare_b = square_b + square_asquare_a = temp# Bringing the pen to starting point of the spiral plotx.penup()x.setposition(factor, 0)x.seth(0)x.pendown()# Setting the colour of the plotting pen to redx.pencolor("red")# Fibonacci Spiral Plotx.left(90)for i in range(n):print(b)fdwd = math.pi * b * factor / 2fdwd /= 90for j in range(90):x.forward(fdwd)x.left(1)temp = aa = bb = temp + b# Here 'factor' signifies the multiplicative# factor which expands or shrinks the scale# of the plot by a certain factor.factor = 1# Taking Input for the number of# Iterations our Algorithm will runn = int(input('Enter the number of iterations (must be > 1): '))# Plotting the Fibonacci Spiral Fractal# and printing the corresponding Fibonacci Numberif n > 0:print("Fibonacci series for", n, "elements :")x = turtle.Turtle()x.speed(100)fiboPlot(n)turtle.done()else:print("Number of iterations must be > 0")
""" Rock Paper Scissors----------------------------------------"""import randomimport osimport reos.system('cls' if os.name=='nt' else 'clear')while (1 < 2):print "\n"print "Rock, Paper, Scissors - Shoot!"userChoice = raw_input("Choose your weapon [R]ock], [P]aper, or [S]cissors: ")if not re.match("[SsRrPp]", userChoice):print "Please choose a letter:"print "[R]ock, [S]cissors or [P]aper."continue// Echo the user's choiceprint "You chose: " + userChoicechoices = ['R', 'P', 'S']opponenetChoice = random.choice(choices)print "I chose: " + opponenetChoiceif opponenetChoice == str.upper(userChoice):print "Tie! "#if opponenetChoice == str("R") and str.upper(userChoice) == "P"elif opponenetChoice == 'R' and userChoice.upper() == 'S':print "Scissors beats rock, I win! "continueelif opponenetChoice == 'S' and userChoice.upper() == 'P':print "Scissors beats paper! I win! "continueelif opponenetChoice == 'P' and userChoice.upper() == 'R':print "Paper beat rock, I win! "continueelse:print "You win!"