Skip to main content

Connect to MYSQL and create a database

0 likes • Nov 19, 2022 • 0 views
Python
Loading...

More Python Posts

Fibonacci Series

0 likes • Nov 18, 2022 • 5 views
Python
#Python 3: Fibonacci series up to n
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
fib(1000)

Caesar Encryption

0 likes • Mar 10, 2021 • 0 views
Python
import string
def caesar(text, shift, alphabets):
def shift_alphabet(alphabet):
return alphabet[shift:] + alphabet[:shift]
shifted_alphabets = tuple(map(shift_alphabet, alphabets))
final_alphabet = "".join(alphabets)
final_shifted_alphabet = "".join(shifted_alphabets)
table = str.maketrans(final_alphabet, final_shifted_alphabet)
return text.translate(table)
plain_text = "Hey Skrome, welcome to CodeCatch"
print(caesar(plain_text, 8, [string.ascii_lowercase, string.ascii_uppercase, string.punctuation]))

Topological sort

0 likes • Nov 19, 2022 • 0 views
Python
#Python program to print topological sorting of a DAG
from collections import defaultdict
#Class to represent a graph
class Graph:
def __init__(self,vertices):
self.graph = defaultdict(list) #dictionary containing adjacency List
self.V = vertices #No. of vertices
# function to add an edge to graph
def addEdge(self,u,v):
self.graph[u].append(v)
# A recursive function used by topologicalSort
def topologicalSortUtil(self,v,visited,stack):
# Mark the current node as visited.
visited[v] = True
# Recur for all the vertices adjacent to this vertex
for i in self.graph[v]:
if visited[i] == False:
self.topologicalSortUtil(i,visited,stack)
# Push current vertex to stack which stores result
stack.insert(0,v)
# The function to do Topological Sort. It uses recursive
# topologicalSortUtil()
def topologicalSort(self):
# Mark all the vertices as not visited
visited = [False]*self.V
stack =[]
# Call the recursive helper function to store Topological
# Sort starting from all vertices one by one
for i in range(self.V):
if visited[i] == False:
self.topologicalSortUtil(i,visited,stack)
# Print contents of stack
print(stack)
g= Graph(6)
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
print("Following is a Topological Sort of the given graph")
g.topologicalSort()

Sort a List of Strings

0 likes • Oct 15, 2022 • 2 views
Python
my_list = ["blue", "red", "green"]
#1- Using sort or srted directly or with specifc keys
my_list.sort() #sorts alphabetically or in an ascending order for numeric data
my_list = sorted(my_list, key=len) #sorts the list based on the length of the strings from shortest to longest.
# You can use reverse=True to flip the order
#2- Using locale and functools
import locale
from functools import cmp_to_key
my_list = sorted(my_list, key=cmp_to_key(locale.strcoll))

Reverse a linked list

0 likes • Nov 19, 2022 • 0 views
Python
# 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()

sum of powers

0 likes • Nov 19, 2022 • 0 views
Python
def sum_of_powers(end, power = 2, start = 1):
return sum([(i) ** power for i in range(start, end + 1)])
sum_of_powers(10) # 385
sum_of_powers(10, 3) # 3025
sum_of_powers(10, 3, 5) # 2925