Skip to content

Latest commit

 

History

History
83 lines (63 loc) · 1.82 KB

HackerRank.md

File metadata and controls

83 lines (63 loc) · 1.82 KB

HackerRank

1. Printing to the conosle

let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!

// Complete the Challenge
func challenge(arr: [[Int]]) -> Int {
    // nothing printed to the console
    print("Hello") 

    // will print to the console using "fileHandle"
    fileHandle.write("HackerRank Challenge\n".data(using: .utf8)!)
    
    return 0
}

2. Case Study - Print the Elements of a Linked List

HackerRank - Print the Elements of a Linked List

JavaScript

// Complete the printLinkedList function below.

/*
 * For your reference:
 *
 * SinglyLinkedListNode {
 *     int data;
 *     SinglyLinkedListNode next;
 * }
 *
 */
function printLinkedList(head) {    
    while(head != null) {
        console.log(head.data);
        head = head.next;  
    }
}

After running the code Congratulations!

Swift

// Complete the printLinkedList function below.

/*
 * For your reference:
 *
 * SinglyLinkedListNode {
 *     data: Int
 *     next: SinglyLinkedListNode?
 * }
 *
 */
func printLinkedList(head: SinglyLinkedListNode?) -> Void {
    var head = head 
    while let currentNode = head {
        print(currentNode.data)
        head = currentNode.next
    } 
}

After running the code Compiler error :(

Swift discussion thread

Some Quick Takeaways

  • JavaScript has autocomplete support, Swift does not.
  • JavaScript runs tests much faster.
  • Swift encounters way more edge case run issues within the HackerRank compiler.