HackerRank Introduction (Part 2)

HackerRank Introduction (Part 2)

Hi this is Jeremy Kruer. Today I am going to continue part 2 of my Hacker Rank Introduction. In case you missed it, check out Part 1 of My Hacker Rank Introduction.

Hello World

In the Hello World Challenge you must write the classic "Hello World" program. There is no input and you must write "Hello World" to the console. This is a pretty easy challenge but the next challenges will build upon it.

Create a new script file and call it HelloWorld.fsx. Type the following code into the script

open System

let main () =  
    printfn "%s" "Hello World"

main()  

This code should be pretty self explanatory if you watched Part 1, if not I would recommend going back to review it now. The biggest difference is that in the call to printfn I use a %s to print a string instead of the %i that I used in Part 1 to print an integer.

Select all of the code by pressing Ctrl + a and then hit Alt + Enter to send the code to the F# Interactive Window. The code should run and print Hello World to the console.

Now copy the code and paste it into the Buffer in the HackerRank Website, choose F# as the programming language and click the "Run Code" button.

You should get a message stating that Test Case 0 passed.

Click the "Submit Code" button and you should get a message stating that all test cases passed and that you solved the challenge.

Hello World N Times

In the Hello World N Times Challenge you take what you learned in the "Hello World" challenge and build upon it. You take an integer, N, as input and you must print "Hello World" N times to the console.

Create a new script file and call it HelloWorldNTimes.fsx. We are going to copy and paste some code from our HelloWorld.fsx and our SolveMeFirstFP.fsx scripts.

open System

let readSting () = Console.ReadLine()  
let readInput convert = readSting () |> convert

let main () =  
    let a = readInput int
    printfn "%s" "Hello World"

main()  

Rename the a variable to n.

At this point, our script is reading an integer as input and storing it in a variable n, but we do not use n. We then print out Hello World one time.

How can we use n to print out "Hello World" that many times?

Loops (avoid them whenever possible)

If you come from an object oriented background like myself, you probably are thinking about using a loop. In C# you might right code like this:

for (int x = 1; x <= n; x++)  
{
    ...
}

While you can use a for loop in F#, it is not the recommended way of doing things. When writing in F# and other functional languages, if you start to write a loop, you should pause and think, is there a more functional way of doing this?

if you start to write a loop, you should pause and think, is there a more functional way of doing this?

List Traversal

A more functional way of accomplishing this is to create a list and then traverse over it. In F# the two most common ways of traversing a list is either with the map function or the iter function. The difference between these two functions is that the map function returns a value for each value in the list and the iter function returns nothing. For this scenario, we will use the iter function because we are just printing out a value to the console and we do not need to return anything.

The syntax for both List.iter and List.map use the lambda syntax which looks like this:

List.iter(fun i -> ...)  

i is the value of the item in the list that is being iterated on. The ... part of the code will be passed i and repeated for every item in the list.

How can you use List.iter to print "Hello World" n Times?

Move the printfn line down one and directly above it create a new list with the bracket syntax [1 .. n]. This creates a list of integers from 1 to n. Now pipe that list into the List.iter function, and print "Hello World".

Your code should look like this:

open System

let readSting () = Console.ReadLine()  
let readInput convert = readSting () |> convert

let main () =  
    let n = readInput int
    [1 .. n]
    |> List.iter(fun _ -> printfn "%s" "Hello World")

main()  
  1. Run the Code in F# Interactive to verify it functions as expected.
  2. Run the Code in Hacker Rank.
  3. Submit this code to Hacker Rank and it should solve the challenge.

You can find a copy of the code in my GitHub repository.

Don't forget to subscribe so that you can get notified each time I post new content. Also, leave a comment below to let me know how I can provide you with more valuable content.

Happy Thanksgiving Everyone!

Get Notified of New Posts

* indicates required

Related Article