Skip to main content

return maximum

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)

Remove numbers from files

0 likes • Mar 26, 2023 • 0 views
Python
import os
# Get the current directory
current_dir = os.getcwd()
# Loop through each file in the current directory
for filename in os.listdir(current_dir):
# Check if the file name starts with a number followed by a period and a space
if filename[0].isdigit() and filename[1] == '.' and filename[2] == ' ':
# Remove the number, period, and space from the file name
new_filename = filename[3:]
# Rename the file
os.rename(os.path.join(current_dir, filename), os.path.join(current_dir, new_filename))

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()

Bubble sort

0 likes • Nov 19, 2022 • 0 views
Python
# Python program for implementation of Bubble Sort
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n-1):
# range(n) also work but outer loop will repeat one time more than needed.
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
# Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i]),

Calculate the Area of a Triangle

0 likes • May 31, 2023 • 0 views
Python
# Prompt user for base and height
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Calculate the area
area = (base * height) / 2
# Display the result
print("The area of the triangle is:", area)

Palindrome checker

0 likes • Nov 19, 2022 • 3 views
Python
# function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")