[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');
    }
} 

[MODERATE] Minimum Coins – CodeEval

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

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

 

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

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)
            {
                lines.Add(line);
                line = reader.ReadLine();
            }
        }

        int length = lines.Count;

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

            while (number > 0)
            {
                if(number - 5 >= 0)
                {
                    countOfCoins++;
                    number -= 5;
                }
                else if(number - 3 >= 0)
                {
                    countOfCoins++;
                    number -= 3;
                }
                else if(number - 1 >= 0)
                {
                    countOfCoins++;
                    number -= 1;
                }
                else
                { }
            }

            Console.WriteLine(countOfCoins);
        }
    }
}

[MODERATE] Number of Ones – CodeEval

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

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>();

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

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

        for (int i = 0; i < lines.Count; i++)
        {
            int number = Convert.ToInt32(lines[i]);
            string binary = Convert.ToString(number, 2);
            var countOfOnes = binary.Count(x => x == '1');

            Console.WriteLine(countOfOnes);
        }
    }
}

[EASY] Split The Number – CodeEval

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

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>();

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

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

        for (int i = 0; i < lines.Count; i++)
        {
            string[] splitLine = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            string numberFromLine = splitLine[0];
            string pattern = splitLine[1];
            string firstSidePattern = "", secondSidePattern = "";
            string[] splitPattern = new string[pattern.Length];
            string symbol = "";
            long result = 0;

            if(pattern.Contains("+"))
            {
                splitPattern = pattern.Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries);
                firstSidePattern = splitPattern[0];
                secondSidePattern = splitPattern[1];
                symbol = "+";
            }
            else
            {
                splitPattern = pattern.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                firstSidePattern = splitPattern[0];
                secondSidePattern = splitPattern[1];
                symbol = "-";
            }

            string firstNumberFromPattern = numberFromLine.Substring(0, firstSidePattern.Length);
            string secondNumberFromPattern = numberFromLine.Substring(firstSidePattern.Length, secondSidePattern.Length);

            long firstNumber = Convert.ToInt64(firstNumberFromPattern);
            long secondNumber = Convert.ToInt64(secondNumberFromPattern);

            switch (symbol)
            {
                case "+":
                    result = firstNumber + secondNumber;
                    break;
                case "-":
                    result = firstNumber - secondNumber;
                    break;
            }

            Console.WriteLine(result);
        }
    }
}

[EASY] Lowest Unique Number – CodeEval

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

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

 

using System;
using System.Collections.Generic;
using System.IO;
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)
            {
                lines.Add(line);
                line = reader.ReadLine();
            }
        }

        for (int i = 0; i < lines.Count; i++)
        {
            List<int> beforeSort = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList();
            List<int> numbers = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList();
            int uniqueNumber = int.MinValue, position = 0;

            numbers.Sort();

            for (int j = 0; j < numbers.Count - 1; j++)
            {
                if(j == 0 && numbers[j] != numbers[j + 1])
                {
                    uniqueNumber = numbers[j];
                    break;
                }
                else
                {
                    if (j != 0)
                    {
                        if (numbers[j - 1] != numbers[j] && numbers[j] != numbers[j + 1])
                        {
                            uniqueNumber = numbers[j];
                            break;
                        }
                    }
                }
            }

            for (int k = 0; k < beforeSort.Count; k++)
            {
                if(beforeSort[k] == uniqueNumber)
                {
                    position = k + 1;
                    break;
                }
            }

            Console.WriteLine(position);
        }
    }
}

[EASY] Bit Positions – CodeEval

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

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>();
        char[] separator = new char[] { ',' };

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

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

        for (int i = 0; i < lines.Count; i++)
        {
            string[] numbers = lines[i].Split(separator, StringSplitOptions.RemoveEmptyEntries);

            int number = Convert.ToInt32(numbers[0]);
            int firstPosition = Convert.ToInt32(numbers[1]) - 1;
            int secondPosition = Convert.ToInt32(numbers[2]) - 1;

            string binaryNumber = Convert.ToString(number, 2);
            string reversedBinaryNumber = "";

            for (int j = binaryNumber.Length - 1; j >= 0; j--)
            {
                reversedBinaryNumber += binaryNumber[j];
            }

            if (reversedBinaryNumber[firstPosition] == reversedBinaryNumber[secondPosition])
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }
        }
    }
}

[EASY] Rightmost Char – CodeEval

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

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>();

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

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

        for (int i = 0; i < lines.Count; i++)
        {
            string[] splitLine = lines[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            char character = Convert.ToChar(splitLine[1]);
            string words = splitLine[0];
            bool contains = words.Contains(character.ToString());

            if (contains == true)
            {
                int index = words.LastIndexOf(character);
                Console.WriteLine(index);
            }
            else
            {
                Console.WriteLine("-1");
            }
        }
    }
}

[EASY] Roman Numerals – CodeEval

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

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<int> arabicDigits = new List<int>() 
        {
            1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000
        };
        List<string> romanDigits = new List<string>() 
        {
            "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"
        };

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

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

        for (int i = 0; i < lines.Count; i++)
        {
            int number = Convert.ToInt32(lines[i]);
            string result = "";

            while (number > 0)
            {
                for (int j = arabicDigits.Count - 1; j >= 0; j--)
                {
                    if((number / arabicDigits[j]) >= 1)
                    {
                        number -= arabicDigits[j];
                        result += romanDigits[j];
                        break;
                    }
                }
            }

            Console.WriteLine(result);
        }
    }
}

[EASY] Set Intersection – CodeEval

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

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)
            {
                lines.Add(line);
                line = reader.ReadLine();
            }
        }

        for (int i = 0; i < lines.Count; i++)
        {
            int number = 0, index = 0;
            string[] splitLine = lines[i].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            List<int> firstNumbers = splitLine[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList();
            List<int> secondNumbers = splitLine[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList();
            List<int> result = new List<int>();

            if(firstNumbers.Count > secondNumbers.Count)
            {
                for (int j = 0; j < secondNumbers.Count; j++)
                {
                    if(firstNumbers.Contains(secondNumbers[j]))
                    {
                        result.Add(secondNumbers[j]);
                    }
                }
            }
            else
            {
                for (int k = 0; k < firstNumbers.Count; k++)
                {
                    if(secondNumbers.Contains(firstNumbers[k]))
                    {
                        result.Add(firstNumbers[k]);
                    }
                }
            }

            if (result.Count != 0)
            {
                if(result.Count != 1)
                {
                    for (int l = 0; l < result.Count - 1; l++)
                    {
                        Console.Write(result[l] + ",");
                    }
                    Console.WriteLine(result[result.Count - 1]);
                }
                else
                {
                    Console.WriteLine(result[0]);
                }
            }
            else
            {
                Console.WriteLine();
            }
            
        }
    }
}

[EASY] Swap Elements – CodeEval

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

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>();

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

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

        for (int i = 0; i < lines.Count; i++)
        {
            string[] splitLine = lines[i].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            string[] numbers = splitLine[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            splitLine[1].Replace(" ", "");
            string[] swapElements = splitLine[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            for (int j = 0; j < swapElements.Length; j++)
            {
                string[] indexes = swapElements[j].Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                int firstIndex = Convert.ToInt32(indexes[0]);
                int secondIndex = Convert.ToInt32(indexes[1]);

                string temp = numbers[firstIndex];
                numbers[firstIndex] = numbers[secondIndex];
                numbers[secondIndex] = temp;
            }

            foreach (var item in numbers)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }
    }
}