• Nov 19, 2022 •CodeCatch
0 likes • 0 views
# Python program to reverse a linked list # Time Complexity : O(n) # Space Complexity : O(n) as 'next' #variable is getting created in each loop. # Node class class Node: # Constructor to initialize the node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to initialize head def __init__(self): self.head = None # Function to reverse the linked list def reverse(self): prev = None current = self.head while(current is not None): next = current.next current.next = prev prev = current current = next self.head = prev # Function to insert a new node at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Utility function to print the linked LinkedList def printList(self): temp = self.head while(temp): print temp.data, temp = temp.next # Driver program to test above functions llist = LinkedList() llist.push(20) llist.push(4) llist.push(15) llist.push(85) print "Given Linked List" llist.printList() llist.reverse() print "\nReversed Linked List" llist.printList()
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")
0 likes • 1 view
import math def factorial(n): print(math.factorial(n)) return (math.factorial(n)) factorial(5) factorial(10) factorial(15)
• Sep 20, 2025 •cntt.dsc-f4b6
1 like • 2 views
print(123)
• Nov 18, 2022 •AustinLeath
# importing the modules import os import shutil # getting the current working directory src_dir = os.getcwd() # printing current directory print(src_dir) # copying the files shutil.copyfile('test.txt', 'test.txt.copy2') #copy src to dst # printing the list of new files print(os.listdir())
1 like • 3 views
def hex_to_rgb(hex): return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4)) hex_to_rgb('FFA501') # (255, 165, 1)