Best Python code snippet using Kiwi_python
day8.py
Source:day8.py
1def part1():2 """3 The tree is made up of nodes;4 Specifically, a node consists of:5 A header, which is always exactly two numbers:6 The quantity of child nodes.7 The quantity of metadata entries.8 Zero or more child nodes (as specified in the header).9 One or more metadata entries (as specified in the header).10 What is the sum of all metadata entries?11 """12 with open('input/day8.txt') as input_file:13 tree_as_list = [int(n) for n in input_file.readline().split(' ')]14 def process_node(input_list):15 my_sum = 016 children = input_list.pop(0)17 metadata_entries = input_list.pop(0)18 for i in range(children):19 my_sum += process_node(input_list)20 for i in range(metadata_entries):21 my_sum += input_list.pop(0)22 return my_sum23 print(process_node(tree_as_list))24def part2():25 """26 If a node has no child nodes, its value is the sum of its metadata entries.27 However, if a node does have child nodes, the metadata entries become indexes which refer to those child nodes.28 The value of this node is the sum of the values of the child nodes referenced by the metadata entries.29 If a referenced child node does not exist, that reference is skipped.30 A child node can be referenced multiple time and counts each time it is referenced.31 A metadata entry of 0 does not refer to any child node.32 What is the value of the root node?33 """34 with open('input/day8.txt') as input_file:35 tree_as_list = [int(n) for n in input_file.readline().split(' ')]36 def process_node(input_list):37 my_value = 038 child_count = input_list.pop(0)39 child_values = [0 for _ in range(child_count + 1)]40 metadata_entry_count = input_list.pop(0)41 for i in range(child_count):42 child_values[i+1] = process_node(input_list)43 for i in range(metadata_entry_count):44 if child_count == 0:45 my_value += input_list.pop(0)46 else:47 child_reference = input_list.pop(0)48 if 0 < child_reference <= child_count:49 my_value += child_values[child_reference]50 return my_value...
rescale_tree.py
Source:rescale_tree.py
1#!/usr/bin/env python2.72# This script will rescale a chronogram from MCMCTree so that the scale is in millions of years, rather than hundreds of millions of years3# I have modified this from a script originally written by Dr Simon Ho4# Usage: ./rescale_tree.py tree_name5# e.g.: ./rescale_tree.py chronogram.tre6from sys import argv7from os import system8def multiply_brlens(tree):9 tree_as_list = []10 for thing in tree:11 tree_as_list.append(thing)12 i = 013 new_tree = []14 while i<len(tree_as_list):15 16 if tree_as_list[i] == ':' or tree_as_list[i] == '{' or tree_as_list[i] == ',':17 new_tree.append(tree_as_list[i])18 i+=119 num = []20 if tree_as_list[i].isdigit() or tree_as_list[i] =='.':21 while tree_as_list[i].isdigit() or tree_as_list[i] =='.':22 num.append(tree_as_list[i])23 i+=124 num = float(''.join(num))25 num = str(float(num*100))26 new_tree.append(num)27 else:28 new_tree.append(tree_as_list[i])29 i+=130 else:31 new_tree.append(tree_as_list[i])32 i+=133 new_tree = ''.join(new_tree)34 return new_tree35f = open (argv[1],"r")36tree = f.read()37remove_nexus = '#NEXUS\nBEGIN TREES;\n\n\tUTREE 1 = '38remove_end = '\nEND;'39tree2 = tree.replace(remove_nexus,'')40tree3 = tree2.replace(remove_end,'')41tree4 = tree3.replace(' ','')42rescaled_tree = multiply_brlens(tree4)43final_tree = tree.replace(tree3,rescaled_tree)44new_name = 'scaled_' + argv[1]45outfile = open(new_name, 'w')46outfile.write(final_tree)...
tree.py
Source:tree.py
1from typing import List, Optional2"""3Classic Tree Node, has a value, an optional left TreeNode and an optional right TreeNode 4"""5class TreeNode:6 def __init__(self, val, left=None, right=None):7 self.val = val8 self.left = left9 self.right = right10"""11Given a list of anything, create a Tree. The root will be index 0, and the left child will be index 1, and the right child will be index 2.12For all further children nodes, the children will be 2x + 1 and 2x + 2, where x is the index of the parent node.13"""14class Tree:15 def __init__(self, tree_as_list: List):16 self.tree_as_list = tree_as_list17 self.root = self.generate_tree_node(0)18 def generate_tree_node(self, index: int) -> Optional[TreeNode]:19 if index >= len(self.tree_as_list) or self.tree_as_list[index] is None:20 return None21 else:22 return TreeNode(self.tree_as_list[index], self.generate_tree_node(2 * index + 1),...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!