Skip to main content

Print "X" pattern

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

More Python Posts

Untitled

0 likes • Apr 21, 2023 • 0 views
Python
print("hellur")

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

delay time lambda

0 likes • Nov 19, 2022 • 0 views
Python
from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second

Binary search algorithm

0 likes • Nov 19, 2022 • 0 views
Python
""" Binary Search Algorithm
----------------------------------------
"""
#iterative implementation of binary search in Python
def binary_search(a_list, item):
"""Performs iterative binary search to find the position of an integer in a given, sorted, list.
a_list -- sorted list of integers
item -- integer you are searching for the position of
"""
first = 0
last = len(a_list) - 1
while first <= last:
i = (first + last) / 2
if a_list[i] == item:
return ' found at position '.format(item=item, i=i)
elif a_list[i] > item:
last = i - 1
elif a_list[i] < item:
first = i + 1
else:
return ' not found in the list'.format(item=item)
#recursive implementation of binary search in Python
def binary_search_recursive(a_list, item):
"""Performs recursive binary search of an integer in a given, sorted, list.
a_list -- sorted list of integers
item -- integer you are searching for the position of
"""
first = 0
last = len(a_list) - 1
if len(a_list) == 0:
return ' was not found in the list'.format(item=item)
else:
i = (first + last) // 2
if item == a_list[i]:
return ' found'.format(item=item)
else:
if a_list[i] < item:
return binary_search_recursive(a_list[i+1:], item)
else:
return binary_search_recursive(a_list[:i], item)

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)

get LDAP user

0 likes • Nov 18, 2022 • 0 views
Python
def get_ldap_user(member_cn, user, passwrd):
'''
Get an LDAP user and return the SAMAccountName
'''
#---- Setting up the Connection
#account used for binding - Avoid putting these in version control
bindDN = str(user) + "@unt.ad.unt.edu"
bindPass = passwrd
#set some tuneables for the LDAP library.
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
#ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, CACERTFILE)
conn = ldap.initialize('ldaps://unt.ad.unt.edu')
conn.protocol_version = 3
conn.set_option(ldap.OPT_REFERRALS, 0)
#authenticate the connection so that you can make additional queries
try:
result = conn.simple_bind_s(bindDN, bindPass)
except ldap.INVALID_CREDENTIALS:
result = "Invalid credentials for %s" % user
sys.exit()
#build query in the form of (uid=user)
ldap_query = '(|(displayName=' + member_cn + ')(cn='+ member_cn + ')(name=' + member_cn + '))'
ldap_info = conn.search_s('DC=unt,DC=ad,DC=unt,DC=edu', ldap.SCOPE_SUBTREE, filterstr=ldap_query)
sAMAccountName = str(ldap_info[0][1]['sAMAccountName']).replace("[b'", "").replace("']","")
return sAMAccountName