CommonUtils Flashcards

(5 cards)

1
Q

Linked List - Node - Python

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

linked_list.py

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

print_list.py

A

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=" ")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define a mapping of DNA characters to numerical values

A

Define a mapping of DNA characters to numerical values
to_int = {“A”: 0, “C”: 1, “G”: 2, “T”: 3}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define a mapping of DNA characters to numerical values ( ACGT)
# Convert each character in the input string to its corresponding number

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly