• Nov 19, 2022 •CodeCatch
0 likes • 3 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
0 likes • 0 views
def print_x_pattern(size): i,j = 0,size - 1 while j >= 0 and i < size: initial_spaces = ' '*min(i,j) middle_spaces = ' '*(abs(i - j) - 1) final_spaces = ' '*(size - 1 - max(i,j)) if j == i: print(initial_spaces + '*' + final_spaces) else: print(initial_spaces + '*' + middle_spaces + '*' + final_spaces) i += 1 j -= 1 print_x_pattern(7)
from collections import defaultdict def combine_values(*dicts): res = defaultdict(list) for d in dicts: for key in d: res[key].append(d[key]) return dict(res) d1 = {'a': 1, 'b': 'foo', 'c': 400} d2 = {'a': 3, 'b': 200, 'd': 400} combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}
• Feb 26, 2023 •wabdelh
#84 48 13 20 61 20 33 97 34 45 6 63 71 66 24 57 92 74 6 25 51 86 48 15 64 55 77 30 56 53 37 99 9 59 57 61 30 97 50 63 59 62 39 32 34 4 96 51 8 86 10 62 16 55 81 88 71 25 27 78 79 88 92 50 16 8 67 82 67 37 84 3 33 4 78 98 39 64 98 94 24 82 45 3 53 74 96 9 10 94 13 79 15 27 56 66 32 81 77 # xor a list of integers to find the lonely integer res = a[0] for i in range(1,len(a)): res = res ^ a[i]
• May 31, 2023 •CodeCatch
0 likes • 5 views
my_list = [1, 2, 3, 4, 5] removed_element = my_list.pop(2) # Remove and return element at index 2 print(removed_element) # 3 print(my_list) # [1, 2, 4, 5] last_element = my_list.pop() # Remove and return the last element print(last_element) # 5 print(my_list) # [1, 2, 4]
# 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")