• Apr 15, 2021 •NoahEaton
0 likes • 0 views
import anytree as at import random as rm # Generate a tree with node_count many nodes. Each has a number key that shows when it was made and a randomly selected color, red or white. def random_tree(node_count): # Generates the list of nodes nodes = [] for i in range(node_count): test = rm.randint(1,2) if test == 1: nodes.append(at.Node(str(i),color="white")) else: nodes.append(at.Node(str(i),color="red")) #Creates the various main branches for i in range(node_count): for j in range(i, len(nodes)): test = rm.randint(1,len(nodes)) if test == 1 and nodes[j].parent == None and (not nodes[i] == nodes[j]): nodes[j].parent = nodes[i] #Collects all the main branches into a single tree with the first node being the root for i in range(1, node_count): if nodes[i].parent == None and (not nodes[i] == nodes[0]): nodes[i].parent = nodes[0] return nodes[0]
• Feb 26, 2023 •wabdelh
#You are given a two-digit integer n. Return the sum of its digits. #Example #For n = 29 the output should be solution (n) = 11 def solution(n): return (n//10 + n%10)
• Aug 1, 2025 •AustinLeath
0 likes • 1 view
from typing import Optional from datetime import datetime def convert_timestamp_string_to_epoch(timestamp: str) -> Optional[int]: epoch_time = None time_obj = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f") epoch_time = int((time_obj - datetime(1970, 1, 1)).total_seconds() * 1000) return epoch_time print(int(convert_timestamp_string_to_epoch("2025-08-01 13:11:47.171"))) #above outputs 1754053907171.0 #how to I remove the .0 ?
• Feb 23, 2025 •hasnaoui1
0 likes • 7 views
print("hello world")
• Nov 19, 2022 •CodeCatch
0 likes • 4 views
""" 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)
• Nov 18, 2022 •AustinLeath
# List lst = [1, 2, 3, 'Alice', 'Alice'] # One-Liner indices = [i for i in range(len(lst)) if lst[i]=='Alice'] # Result print(indices) # [3, 4]