• Nov 19, 2022 •CodeCatch
0 likes • 1 view
# Python code to find the URL from an input string # Using the regular expression import re def Find(string): # findall() has been used # with valid conditions for urls in string regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))" url = re.findall(regex,string) return [x[0] for x in url] # Driver Code string = 'My Profile: https://codecatch.net' print("Urls: ", Find(string))
0 likes • 4 views
def clamp_number(num, a, b): return max(min(num, max(a, b)), min(a, b)) clamp_number(2, 3, 5) # 3 clamp_number(1, -1, -5) # -1
""" Number Guessing Game ---------------------------------------- """ import random attempts_list = [] def show_score(): if len(attempts_list) <= 0: print("There is currently no high score, it's yours for the taking!") else: print("The current high score is {} attempts".format(min(attempts_list))) def start_game(): random_number = int(random.randint(1, 10)) print("Hello traveler! Welcome to the game of guesses!") player_name = input("What is your name? ") wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name)) // Where the show_score function USED to be attempts = 0 show_score() while wanna_play.lower() == "yes": try: guess = input("Pick a number between 1 and 10 ") if int(guess) < 1 or int(guess) > 10: raise ValueError("Please guess a number within the given range") if int(guess) == random_number: print("Nice! You got it!") attempts += 1 attempts_list.append(attempts) print("It took you {} attempts".format(attempts)) play_again = input("Would you like to play again? (Enter Yes/No) ") attempts = 0 show_score() random_number = int(random.randint(1, 10)) if play_again.lower() == "no": print("That's cool, have a good one!") break elif int(guess) > random_number: print("It's lower") attempts += 1 elif int(guess) < random_number: print("It's higher") attempts += 1 except ValueError as err: print("Oh no!, that is not a valid value. Try again...") print("({})".format(err)) else: print("That's cool, have a good one!") if __name__ == '__main__': start_game()
• May 31, 2023 •CodeCatch
0 likes • 0 views
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)
• Nov 18, 2022 •AustinLeath
# List lst = [1, 2, 3, 'Alice', 'Alice'] # One-Liner indices = [i for i in range(len(lst)) if lst[i]=='Alice'] # Result print(indices) # [3, 4]
0 likes • 2 views
# Python program for Plotting Fibonacci # spiral fractal using Turtle import turtle import math def fiboPlot(n): a = 0 b = 1 square_a = a square_b = b # Setting the colour of the plotting pen to blue x.pencolor("blue") # Drawing the first square x.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 Series temp = square_b square_b = square_b + square_a square_a = temp # Drawing the rest of the squares for 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 Series temp = square_b square_b = square_b + square_a square_a = temp # Bringing the pen to starting point of the spiral plot x.penup() x.setposition(factor, 0) x.seth(0) x.pendown() # Setting the colour of the plotting pen to red x.pencolor("red") # Fibonacci Spiral Plot x.left(90) for i in range(n): print(b) fdwd = math.pi * b * factor / 2 fdwd /= 90 for j in range(90): x.forward(fdwd) x.left(1) temp = a a = b b = 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 run n = int(input('Enter the number of iterations (must be > 1): ')) # Plotting the Fibonacci Spiral Fractal # and printing the corresponding Fibonacci Number if 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")