• Sep 14, 2024 •rgannedo-6205
0 likes • 4 views
# Python binary search function def binary_search(arr, target): left = 0 right = len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 # Usage arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] target = 7 result = binary_search(arr, target) if result != -1: print(f"Element is present at index {result}") else: print("Element is not present in array")
• Nov 18, 2022 •AustinLeath
0 likes • 0 views
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
• Jun 1, 2023 •CodeCatch
0 likes • 3 views
def calculate_values(): value1 = 10 value2 = 20 return value1, value2 result1, result2 = calculate_values() print("Result 1:", result1) print("Result 2:", result2)
• May 31, 2023 •CodeCatch
0 likes • 1 view
def generate_pascals_triangle(num_rows): triangle = [] for row in range(num_rows): # Initialize the row with 1 current_row = [1] # Calculate the values for the current row if row > 0: previous_row = triangle[row - 1] for i in range(len(previous_row) - 1): current_row.append(previous_row[i] + previous_row[i + 1]) # Append 1 at the end of the row current_row.append(1) # Add the current row to the triangle triangle.append(current_row) return triangle def display_pascals_triangle(triangle): for row in triangle: for number in row: print(number, end=" ") print() # Prompt the user for the number of rows num_rows = int(input("Enter the number of rows for Pascal's Triangle: ")) # Generate Pascal's Triangle pascals_triangle = generate_pascals_triangle(num_rows) # Display Pascal's Triangle display_pascals_triangle(pascals_triangle)
0 likes • 2 views
# 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)
• Oct 15, 2022 •CodeCatch
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