Skip to main content

Finding NULL values within set

0 likes • Oct 7, 2022 • 1 view
Python
Loading...

More Python Posts

Sieve of Eratosthenes

0 likes • Nov 19, 2022 • 0 views
Python
# Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
# For example, if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”.
# Python program to print all primes smaller than or equal to
# n using Sieve of Eratosthenes
def SieveOfEratosthenes(n):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
# Print all prime numbers
for p in range(n + 1):
if prime[p]:
print (p)
# driver program
if __name__=='__main__':
n = 30
print("Following are the prime numbers smaller")
print("than or equal to ", n)
print("than or equal to ", n)
SieveOfEratosthenes(n)

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

Distinct Primes Finder > 1000

0 likes • Nov 18, 2022 • 3 views
Python
primes=[]
products=[]
def prime(num):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
return False
else:
primes.append(num)
return True
for n in range(30,1000):
if len(primes) >= 20:
break;
else:
prime(n)
for previous, current in zip(primes[::2], primes[1::2]):
products.append(previous * current)
print (products)

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]),

hex to rgb

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

Return Letter Combinations

0 likes • Nov 18, 2022 • 0 views
Python
# @return a list of strings, [s1, s2]
def letterCombinations(self, digits):
if '' == digits: return []
kvmaps = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
return reduce(lambda acc, digit: [x + y for x in acc for y in kvmaps[digit]], digits, [''])