[EASY] Delta Time – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/166/ And here is my solution. I get 100% with this code.

 

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

class DeltaTime
{
    public static string line = null;

    static void Main(string[] args)
    {
        List<string> readFile = ReadFileLines(args[0]);
        List<TimeSpan> deltaTime = FindDeltaTime(readFile);
        PrintDeltaTime(deltaTime);
    }

    public static List<string> ReadFileLines(string fileName)
    {
        List<string> readFileLines = new List<string>();
        StreamReader reader = new StreamReader(fileName);

        using (reader)
        {
            line = reader.ReadLine();

            while (line != null)
            {
                readFileLines.Add(line);
                line = reader.ReadLine();
            }
        }

        return readFileLines;
    }

    public static List<TimeSpan> FindDeltaTime(List<string> fileLines)
    {
        List<TimeSpan> findDeltaTime = new List<TimeSpan>();
        int length = fileLines.Count;
        string[] times = new string[2];
        char[] separator = new char[] { ' ' };
        DateTime firstTime = new DateTime();
        DateTime secondTime = new DateTime();
        TimeSpan diff = new TimeSpan();

        for (int i = 0; i < length; i++)
        {
            line = fileLines[i];
            times = line.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToArray();

            firstTime = DateTime.Parse(times[0]);
            secondTime = DateTime.Parse(times[1]);

            diff = secondTime.Subtract(firstTime);

            if(diff.Hours < 0 || diff.Minutes < 0 || diff.Seconds < 0)
            {
                diff = diff.Negate();
            }
            
            findDeltaTime.Add(diff);
        }

        return findDeltaTime;
    }

    public static void PrintDeltaTime(List<TimeSpan> times)
    {
        foreach (var time in times)
        {
            Console.WriteLine(time);
        }
    }
}

[EASY] The Major Element – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/132/ And here is my solution. I get 100% with this code.

 

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

class TheMajorElement
{
    public static string line = null;

    static void Main(string[] args)
    {
        List<string> readFile = ReadFileLines(args[0]);

        string result = FindMajorElement(readFile);

        Console.WriteLine(result);
    }

    public static List<string> ReadFileLines(string fileName)
    {
        List<string> readFileLines = new List<string>();
        StreamReader reader = new StreamReader(fileName);

        using (reader)
        {
            line = reader.ReadLine();

            while (line != null)
            {
                readFileLines.Add(line);
                line = reader.ReadLine();
            }
        }

        return readFileLines;
    }

    public static string FindMajorElement(List<string> elements)
    {
        List<int> numbers = new List<int>();
        string checkResult = null;
        string result = null;
        int length = elements.Count;
        int numbersCount = 0;
        int count = 0;
        int number = 0;
        int limit = 0;
        char[] separator = new char[] { ' ', ',' };

        for (int i = 0; i < length; i++)
        {
            line = elements[i];
            numbers = line.Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList();
            numbersCount = numbers.Count;
            limit = numbersCount / 2;

            for (int j = 0; j < numbersCount; j++)
            {
                number = numbers[j];
                count = numbers.Count(x => x == number);

                if(count > limit)
                {
                    checkResult += number + "\n";
                    break;
                }
            }
            
            if (checkResult == null)
            {
                result += "None\n";
            }
            else
            {
                result += checkResult;
            }

            checkResult = null;
        }

        return result.TrimEnd('\n');
    }
} 

[EASY] JSON menu IDs – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/102/ And here is my solution. I get 100% with this code.

 

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

class JSONMenuIDs
{
    public static string line = null;

    static void Main(string[] args)
    {
        List<string> readFileLines = ReadFile(args[0]);
        List<int> sumOfIDs = SumOfIDs(readFileLines);
        PrintSum(sumOfIDs);
    }

    public static List<string> ReadFile(string fileName)
    {
        List<string> readFile = new List<string>();
        StreamReader reader = new StreamReader(fileName);

        using (reader)
        {
            line = reader.ReadLine();

            while (line != null)
            {
                readFile.Add(line);
                line = reader.ReadLine();
            }
        }

        return readFile;
    }

    public static List<int> SumOfIDs(List<string> jsonFile)
    {
        List<int> sumOfIDs = new List<int>();
        int length = jsonFile.Count;
        int indexStartLabel = 0;
        int indexEndLabel = 0;
        int sum = 0;
        string label = "\"Label ";
        string endLabel = "\"";
        string fullLabel = null;
        
        for (int i = 0; i < length; i++)
        {
            line = jsonFile[i];
            indexStartLabel = line.IndexOf(label);

            while (indexStartLabel != -1)
            {
                indexEndLabel = line.IndexOf(endLabel, indexStartLabel + 1);
                fullLabel = line.Substring(indexStartLabel, indexEndLabel - indexStartLabel);
                sum += Convert.ToInt32(Regex.Match(fullLabel, @"-?\d+").Value);
                indexStartLabel = line.IndexOf(label, indexEndLabel + 1);
            }
            
            sumOfIDs.Add(sum);
            sum = 0;
        }


        return sumOfIDs;
    }

    public static void PrintSum(List<int> sumOfIDs)
    {
        foreach (var sum in sumOfIDs)
        {
            Console.WriteLine(sum);
        }
    }
}

[EASY] Sum of Digits – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/21/ And here is my solution. I get 100% with this code.

 

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

class SumОfDigits
{
    public static string line = null;
    public static int length = 0;

    static void Main(string[] args)
    {
        List<string> numbers = NumbersFromFile(args[0]);
        
        List<int> sumOfDigits = SumOfDigits(numbers);

        Print(sumOfDigits);
    }

    public static List<string> NumbersFromFile(string fileName)
    {
        List<string> numbersFromFile = new List<string>();
        StreamReader reader = new StreamReader(fileName);

        using (reader)
        {
            line = reader.ReadLine();

            while (line != null)
            {
                numbersFromFile.Add(line);
                line = reader.ReadLine();
            }
        }

        return numbersFromFile;
    }

    public static List<int> SumOfDigits(List<string> numbersFromFile)
    {
        List<int> digits = new List<int>();
        List<int> sumOfDigits = new List<int>();
        length = numbersFromFile.Count;
        

        for (int i = 0; i < length; i++)
        {
            line = numbersFromFile[i];
            digits = line.Where(x => Char.IsDigit(x) == true).Select(x => Convert.ToInt32(x.ToString())).ToList();
            sumOfDigits.Add(digits.Sum());
        }


        return sumOfDigits;
    }

    public static void Print(List<int> sumOfDigits)
    {
        foreach (var sum in sumOfDigits)
        {
            Console.WriteLine(sum);
        }
    }
}

[EASY] Read More – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/167/
And here is my solution. I get 92% with this code.

 

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

class ReadMore
{
    public static string line = null;
    static void Main(string[] args)
    {
        List<string> readFile = ReadFile(args[0]);
        
        List<string> formatText = FormatText(readFile);
        
        PrintFormatedText(formatText);
    }

    public static List<string> ReadFile(string fileName)
    {
        List<string> readFileLines = new List<string>();
        StreamReader reader = new StreamReader(fileName);
        line = null;

        using (reader)
        {
            line = reader.ReadLine();

            while (line != null)
            {
                readFileLines.Add(line);
                line = reader.ReadLine();
            }
        }

        return readFileLines;
    }

    public static List<string> FormatText(List<string> fileText)
    {
        List<string> formatTextLines = new List<string>();
        int length = fileText.Count;
        int lineLength = 0;
        string formatLine = null;

        for (int i = 0; i < length; i++)
        {
            line = fileText[i];
            lineLength = line.Length;

            if(lineLength < 55)
            {
                formatTextLines.Add(line);
            }
            else
            {
                String[] words = line.Split(new char[] { ' ', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
                formatLine = "";
                string tmp = null;
                int count = 0;
                int exit = 0;

                while (exit != -1)
                {
                    tmp = formatLine + " " + words[count];
                    count++;
                    if(tmp.Length <= 40)
                    {
                        formatLine = tmp;
                        
                    }
                    else
                    {
                        exit = -1;
                    }
                }

                formatLine = formatLine.TrimStart(new char[] { ' ' });
                formatLine += "... <Read More>";
                formatTextLines.Add(formatLine);
            }
        }

        return formatTextLines;
    }

    public static void PrintFormatedText(List<string> formatedText)
    {
        foreach (var line in formatedText)
        {
            Console.WriteLine(line);
        }
    }
}

[EASY] Mixed Content – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/115/

And here is my solution. I get 100% with this code.

 

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

class Program
{

    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(args[0]);
        List<string> lines = new List<string>();
        List<string> words = new List<string>();
        List<string> digits = new List<string>();
        List<string> results = new List<string>();
        char[] separator = new char[] { ',' };
        string line = "", result = "", removeDigitsFromLine = "", removeTextFromLine = "";
        Regex removeDigits = new Regex(@"[\d-]");
        Regex removeText = new Regex("[^.0-9]");

        using (reader)
        {
            line = reader.ReadLine();

            while (line != null)
            {
                lines.Add(line);
                line = reader.ReadLine();
            }
        }

        int length = lines.Count;

        for (int i = 0; i < length; i++)
        {
            line = lines[i];

            if (CheckWords(line))
            {
                removeDigitsFromLine = Regex.Replace(line, @"[\d-]", String.Empty);
                words = removeDigitsFromLine.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            else
            {
                words = new List<string>();
            }
            if (CheckDigits(line))
            {
                removeTextFromLine = removeText.Replace(line, ",");
                digits = removeTextFromLine.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            else
            {
                digits = new List<string>();
            }

            result = Separate(words, digits);
            results.Add(result);
        }

        foreach (var item in results)
        {
            Console.WriteLine(item);
        }
    }

    public static string Separate(List<string> words, List<string> digits)
    {
        string result = "", resultWords = "", resultDigits = "";

        int wordsLength = words.Count;
        int digitsLength = digits.Count;

        if (wordsLength != 0)
        {
            for (int i = 0; i < wordsLength - 1; i++)
            {
                resultWords += words[i] + ",";
            }
            resultWords += words[wordsLength - 1];
        }
        if (digitsLength != 0)
        {
            for (int i = 0; i < digitsLength - 1; i++)
            {
                resultDigits += digits[i] + ",";
            }
            resultDigits += digits[digitsLength - 1];
        }

        if(resultWords != "" && resultDigits != "")
        {
            result = resultWords + "|" + resultDigits;
        }
        else if(resultWords == "")
        {
            result = resultDigits;
        }
        else if(resultDigits == "")
        {
            result = resultWords;
        }
        else
        {
            result = "";
        }
        return result;
    }

    public static bool CheckDigits(string line)
    {
        int length = line.Length;

        for (int i = 0; i < length; i++)
        {
            if(Char.IsDigit(line[i]))
            {
                return true;
            }
        }

        return false;
    }

    public static bool CheckWords(string line)
    {
        int length = line.Length;

        for (int i = 0; i < length; i++)
        {
            if(Char.IsLetter(line[i]))
            {
                return true;
            }
        }

        return false;
    }
}

[EASY] Morse Code – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/116/

And here is my solution. I get 100% with this code.

 

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

class Program
{
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(args[0]);
        List<string> lines = new List<string>();
        List<string> splitLine = new List<string>();
        List<string> results = new List<string>();
        string result = "", line = "";
        char[] separator = new char[] { ' ' };

        using (reader)
        {
            line = reader.ReadLine();

            while (line != null)
            {
                lines.Add(line);
                line = reader.ReadLine();
            }
        }

        int length = lines.Count;

        for (int i = 0; i < length; i++)
        {
            line = lines[i].Replace("  ", " SPACE ");
            splitLine = line.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();
            result = MorseToString(splitLine);
            results.Add(result);
        }

        foreach (var item in results)
        {
            Console.WriteLine(item);
        }
    }

    public static string MorseToString(List<string> splitLine)
    {
        string result = "", symbol = "", morseSymbol = "";
        int index = 0, length = splitLine.Count;

        while (length-- > 0)
        {
            morseSymbol = splitLine[index];

            switch (morseSymbol)
            {
                case ".-":
                    symbol = "A";
                    break;
                case "-...":
                    symbol = "B";
                    break;
                case "-.-.":
                    symbol = "C";
                    break;
                case "-..":
                    symbol = "D";
                    break;
                case ".":
                    symbol = "E";
                    break;
                case "..-.":
                    symbol = "F";
                    break;
                case "--.":
                    symbol = "G";
                    break;
                case "....":
                    symbol = "H";
                    break;
                case "..":
                    symbol = "I";
                    break;
                case ".---":
                    symbol = "J";
                    break;
                case "-.-":
                    symbol = "K";
                    break;
                case ".-..":
                    symbol = "L";
                    break;
                case "--":
                    symbol = "M";
                    break;
                case "-.":
                    symbol = "N";
                    break;
                case "---":
                    symbol = "O";
                    break;
                case ".--.":
                    symbol = "P";
                    break;
                case "--.-":
                    symbol = "Q";
                    break;
                case ".-.":
                    symbol = "R";
                    break;
                case "...":
                    symbol = "S";
                    break;
                case "-":
                    symbol = "T";
                    break;
                case "..-":
                    symbol = "U";
                    break;
                case "...-":
                    symbol = "V";
                    break;
                case ".--":
                    symbol = "W";
                    break;
                case "-..-":
                    symbol = "X";
                    break;
                case "-.--":
                    symbol = "Y";
                    break;
                case "--..":
                    symbol = "Z";
                    break;
                case ".-.-.-":
                    symbol = ".";
                    break;
                case "--..--":
                    symbol = ",";
                    break;
                case "..--..":
                    symbol = "?";
                    break;
                case "-..-.":
                    symbol = "/";
                    break;
                case ".--.-.":
                    symbol = "@";
                    break;
                case ".----":
                    symbol = "1";
                    break;
                case "..---":
                    symbol = "2";
                    break;
                case "...--":
                    symbol = "3";
                    break;
                case "....-":
                    symbol = "4";
                    break;
                case ".....":
                    symbol = "5";
                    break;
                case "-....":
                    symbol = "6";
                    break;
                case "--...":
                    symbol = "7";
                    break;
                case "---..":
                    symbol = "8";
                    break;
                case "----.":
                    symbol = "9";
                    break;
                case "-----":
                    symbol = "0";
                    break;
                case "SPACE":
                    symbol = " ";
                    break;
            }

            result += symbol;

            index++;
        }

        return result;
    }
}

[MODERATE] Prime Numbers – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/46/

And here is my solution. I get 100% with this code.

 

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

class Program
{
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(args[0]);
        List<string> lines = new List<string>();
        StringBuilder results = new StringBuilder();
        string primeNumbers = "";
        string line = null;
        uint number = 0;

        using (reader)
        {
            line = reader.ReadLine();

            while (line != null)
            {
                lines.Add(line);
                line = reader.ReadLine();
            }
        }

        int length = lines.Count;

        for (int i = 0; i < length; i++)
        {
            number = Convert.ToUInt32(lines[i]);

            primeNumbers = PrimeNumbers(number);
            primeNumbers = primeNumbers.Remove(primeNumbers.Length - 1);
            if (i == length - 1)
            {
                results.Append(primeNumbers);
            }
            else
            {
                results.Append(primeNumbers).AppendLine();
            }
        }

        Console.WriteLine(results.ToString());

    }

    public static string PrimeNumbers(uint number)
    {
        StringBuilder primeNumbers = new StringBuilder();

        for (uint i = 2; i < number; i++)
        {
            if (IsPrime(i))
            {
                primeNumbers.AppendFormat("{0},", i);
            }
        }

        return primeNumbers.ToString();
    }

    public static bool IsPrime(uint number)
    {
        for (uint i = 2; i < (number / 2 + 1); i++)
        {
            if((number % i) == 0)
            {
                return false;
            }
        }

        return true;
    }
}

[MODERATE] Longest Lines – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/2/

And here is my solution. I get 100% with this code.

 

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

class Program
{
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(args[0]);
        List<string> lines = new List<string>();
        

        using (reader)
        {
            string line = reader.ReadLine();

            while (line != null)
            {
                if (!(String.IsNullOrEmpty(line) || String.IsNullOrWhiteSpace(line)))
                {
                    line.Trim(new char[] { '\r', ' ', '\t' });
                    lines.Add(line);
                }
                line = reader.ReadLine();
            }
        }

        int length = lines.Count;
        int numberOfResults = Convert.ToInt32(lines[0]);

        IEnumerable<string> results =
            from result in lines
            orderby result.Length descending
            select result;

        for (int i = 0; i < numberOfResults; i++)
        {
            Console.WriteLine(results.ElementAt(i));
        }
    }
}

[EASY] Roller Coaster – CodeEval

Here is the problem: https://www.codeeval.com/open_challenges/156/

And here is my solution. I get 100% with this code.

 

using System;
using System.IO;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(args[0]);
        List<string> lines = new List<string>();
        List<string> results = new List<string>();

        using (reader)
        {
            string line = reader.ReadLine();

            while (line != null)
            {
                lines.Add(line);
                line = reader.ReadLine();
            }
        }

        int length = lines.Count;

        for (int i = 0; i < length; i++)
        {
            results.Add(ConvertLine(lines[i]));
        }

        foreach (var result in results)
        {
            Console.WriteLine(result);
        }
    }

    public static string ConvertLine(string line)
    {
        int length = line.Length;
        int index = 0;
        string result = "";

        for (int i = 0; i < length; i++)
        {
            if(Char.IsLetter(line[i]))
            {
                if(index % 2 == 0)
                {
                    result += line[i].ToString().ToUpper();
                }
                else
                {
                    result += line[i].ToString().ToLower();
                }
                index++;
            }
            else
            {
                result += line[i];
            }
        }

        return result;
    }
}