Skip to main content

Selection sort

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

More Python Posts

LeetCode Flood Fill

0 likes • Oct 15, 2022 • 0 views
Python
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
R, C = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r-1, c)
if r+1 < R: dfs(r+1, c)
if c >= 1: dfs(r, c-1)
if c+1 < C: dfs(r, c+1)
dfs(sr, sc)
return image

Remove i'th character

0 likes • Nov 19, 2022 • 0 views
Python
# Python code to demonstrate
# method to remove i'th character
# Naive Method
# Initializing String
test_str = "CodeCatch"
# Printing original string
print ("The original string is : " + test_str)
# Removing char at pos 3
# using loop
new_str = ""
for i in range(len(test_str)):
if i != 2:
new_str = new_str + test_str[i]
# Printing string after removal
print ("The string after removal of i'th character : " + new_str)

UNT CSCE 2100 Question 1

0 likes • Nov 18, 2022 • 1 view
Python
#question1.py
def rose(n) :
if n==0 :
yield []
else :
for k in range(0,n) :
for l in rose(k) :
for r in rose(n-1-k) :
yield [l]+[r]+[r]
def start(n) :
for x in rose(n) :
print(x) #basically I am printing x for each rose(n) file
print("starting program: \n")
start(2) # here is where I call the start function

Lonely Integer

0 likes • Feb 26, 2023 • 0 views
Python
#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]

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)

return byte size

0 likes • Nov 19, 2022 • 1 view
Python
def byte_size(s):
return len(s.encode('utf-8'))
byte_size('😀') # 4
byte_size('Hello World') # 11