CodeEval 2 – Mth To The Last Element

Share this

November 10, 2014

CodeEval 2 – Mth To Last Element


About CodeEval post series


CodeEval is a series of posts that 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 to various CodeEval programming challenges. Each solution has already been submitted and accepted as valid on the 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 the 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 that 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 (update: the CodeEval has shut down, so the link was removed).

 


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:        [email protected]
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 myList = line.Split(' ').ToList();
                    int mth = int.Parse(myList[myList.Count - 1]);
                    if (myList.Count > mth)
                    {                        
                        myList.Reverse();
                        Console.WriteLine(myList[mth]);
                    }
                }
            }                
            Console.ReadLine();
        }
    }
}
 
/* NOTE: the solution has already been submitted and accepted as valid! */ 

 


Points 


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

CodeEval 2 – Mth To Last Element Score

Page last modified: March 2, 2020

Christos Samaras

Hi, I am Christos, a Mechanical Engineer by profession (Ph.D.) and a Software Developer by obsession (10+ years of experience)! I founded this site back in 2011 intending to provide solutions to various engineering and programming problems.

Christos E. Samaras

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Add Content Block
>