Skip to content

Some HackerRank Solutions

Balanced Brackets

https://www.hackerrank.com/challenges/balanced-brackets/problem

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'isBalanced' function below.
#
# The function is expected to return a STRING.
# The function accepts STRING s as parameter.
#

def isBalanced(s):
    # Write your code here
    br = []
    isbal = 'YES'
    for c in s:
        if c in ['{', '(', '[']:
            br.append(c)
        elif c in ['}', ')', ']']:
            if len(br) == 0:
                isbal = 'NO'
                break
            elif ((c == '}') & (br[-1] != '{') | 
                (c == ')') & (br[-1] != '(') | 
                (c == ']') & (br[-1] != '[')):
                isbal = 'NO'
                break
            else:
                br.pop(-1)
    if len(br) > 0:
        isbal = 'NO'
    return isbal                

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())

    for t_itr in range(t):
        s = input()

        result = isBalanced(s)

        fptr.write(result + '\n')

    fptr.close()

Queue using Two Stacks

https://www.hackerrank.com/challenges/queue-using-two-stacks/problem

# Enter your code here. Read input from STDIN. Print output to STDOUT

if __name__ == '__main__':
    nq = int(input())
    q = []
    for _ in range(nq):
        a = input()
        if a[0] == '1':
            b = int(a[2:])
            q = q + [b]
        elif a[0] == '2':
            q = q[1:]
        elif a[0] == '3':
            print(q[0])

Merge two sorted linked lists

https://www.hackerrank.com/challenges/merge-two-sorted-linked-lists/problem

#!/bin/python3

import math
import os
import random
import re
import sys

class SinglyLinkedListNode:
    def __init__(self, node_data):
        self.data = node_data
        self.next = None

class SinglyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def insert_node(self, node_data):
        node = SinglyLinkedListNode(node_data)

        if not self.head:
            self.head = node
        else:
            self.tail.next = node


        self.tail = node

def print_singly_linked_list(node, sep, fptr):
    while node:
        fptr.write(str(node.data))

        node = node.next

        if node:
            fptr.write(sep)

# Complete the mergeLists function below.

#
# For your reference:
#
# SinglyLinkedListNode:
#     int data
#     SinglyLinkedListNode next
#
#
def mergeLists(head1, head2):
    h1, h2 = [], []
    while(head1):
        h1.append(head1.data)
        head1 = head1.next
    while(head2):
        h2.append(head2.data)
        head2 = head2.next
    s = sorted(h1+h2)
    ret = SinglyLinkedList();
    for v in s:
        ret.insert_node(v)
    return ret.head


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    tests = int(input())

    for tests_itr in range(tests):
        llist1_count = int(input())

        llist1 = SinglyLinkedList()

        for _ in range(llist1_count):
            llist1_item = int(input())
            llist1.insert_node(llist1_item)
            
        llist2_count = int(input())

        llist2 = SinglyLinkedList()

        for _ in range(llist2_count):
            llist2_item = int(input())
            llist2.insert_node(llist2_item)

        llist3 = mergeLists(llist1.head, llist2.head)

        print_singly_linked_list(llist3, ' ', fptr)
        fptr.write('\n')

    fptr.close()
Published inProgramming

Be First to Comment

Leave a Reply