copy

Tuesday, 11 November 2014



CodeEval 2 – Mth To Last Element


About CodeEval post series



CodeEval is a series of posts which are different than the typical engineering/Excel/VBA posts that are being published in this blog. The purpose of this series is to demonstrate possible solutions on various CodeEval programming challenges. Each solution has already been submitted and accepted as valid on CodeEval platform, so if you try to submit the presented solution as it is, you will probably get a “not unique solution” result. The solutions will be presented in C# language, but the logic/algorithm behind them is similar despite the language you might use.



Mth to the last element – challenge description



Write a program which determines the Mth to the last element in a list.

Input sample

The first argument is a path to a file. The file contains the series of space-delimited characters followed by an integer. The integer represents an index in the list (1-based), one per line. Example:

a b c d 4
e f g h 2

Output sample

Print to stdout (usually Windows Console) the Mth element from the end of the list, one per line. If the index is larger than the number of elements in the list, ignore that input. Example:

a
g

The particular challenge has a relatively low success rate (66.5% - 11/11/2014) and its level of difficulty is medium. More info you can find here.



Solution



Despite the medium level of difficulty, I would say that this challenge was probably an easy one! Below you will find a working solution:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

/*
---------------------------------------------------------------------
The code below solves the CodeEval challenge -  Mth to last element.

Written by:    Christos Samaras
Date:          27/06/2014
e-mail:        xristos.samaras@gmail.com
site:          http://www.myengineeringworld.net
---------------------------------------------------------------------
*/

namespace MthToLastElement
{
    class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader reader = File.OpenText(args[0]))            
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (line != null)
                {
                    List<string> myList = line.Split(' ').ToList();
                    int mth = int.Parse(myList[myList.Count - 1]);
                    if (myList.Count > mth)
                    {                        
                        myList.Reverse();
                        Console.WriteLine(myList[mth]);
                    }
                }
            }                
            Console.ReadLine();
        }
    }
}



Points



Here is the proof that the solution works and the points given by the CodeEval platform.

CodeEval 2 – Mth To Last Element - Score

Did you like this post? If yes, then share it with your friends. Thank you!


Categories:


Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: e-mail, Facebook, Twitter, Google+ and Linkedin. More info