CommonUtils Flashcards
(5 cards)
Linked List - Node - Python
Template for linked list node class
class LinkedListNode:
# __init__ will be used to make a LinkedListNode type object.
def __init__(self, data, next=None):
self.data = data
self.next = next
linked_list.py
from linked_list_node import LinkedListNode
Template for the linked list
class LinkedList:
# __init__ will be used to make a LinkedList type object.
def __init__(self):
self.head = None
# insert_node_at_head method will insert a LinkedListNode at head # of a linked list. def insert_node_at_head(self, node): if self.head: node.next = self.head self.head = node else: self.head = node # create_linked_list method will create the linked list using the # given integer array with the help of InsertAthead method. def create_linked_list(self, lst): for x in reversed(lst): new_node = LinkedListNode(x) self.insert_node_at_head(new_node) # returns the number of nodes in the linked list def get_length(self, head): temp = head length = 0 while(temp): length+=1 temp = temp.next return length # returns the node at the specified position(index) of the linked list def get_node(self, head, pos): if pos != -1: p = 0 ptr = head while p < pos: ptr = ptr.next p += 1 return ptr # \_\_str\_\_(self) method will display the elements of linked list. def \_\_str\_\_(self): result = "" temp = self.head while temp: result += str(temp.data) temp = temp.next if temp: result += ", " result += "" return result
print_list.py
Template for printing the linked list with forward arrows
def print_list_with_forward_arrow(linked_list_node):
temp = linked_list_node
while temp:
print(temp.data, end=” “) # print node value
temp = temp.next if temp: print("→", end=" ") else: # if this is the last node, print null at the end print("→ null", end=" ")
def print_list_with_forward_arrow_loop(linked_list_node):
temp = linked_list_node
while temp:
print(temp.data, end=” “) # print node value
temp = temp.next if temp: print("→", end=" ")
Define a mapping of DNA characters to numerical values
Define a mapping of DNA characters to numerical values
to_int = {“A”: 0, “C”: 1, “G”: 2, “T”: 3}
Define a mapping of DNA characters to numerical values ( ACGT)
# Convert each character in the input string to its corresponding number
Define a mapping of DNA characters to numerical values
to_int = {“A”: 0, “C”: 1, “G”: 2, “T”: 3}
# Convert each character in the input string to its corresponding number encoded_sequence = [to_int[c] for c in s]