[MODERATE] Remove Characters – CodeEval

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

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>();
        char[] separator = new char[] { ','};
        string[] splitLine = new string[2];
        string word = "", remove = "", result = "";

        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++)
        {
            splitLine = lines[i].Split(separator, StringSplitOptions.RemoveEmptyEntries);
            word = splitLine[0];
            remove = splitLine[1].Remove(0, 1);

            for (int j = 0; j < remove.Length; j++)
            {
                word = word.Replace(remove[j].ToString(), "");
			}
            result = word;
            result.Trim();
            results.Add(result);
        }

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

[MODERATE] First Non-Repeated Character – CodeEval

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

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
{
    public static char firstCharacter = '\0';
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(args[0]);
        List<string> lines = new List<string>();
        List<char> results = new List<char>();

        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++)
        {
            firstCharacter = FirstCharacter(lines[i]);
            results.Add(firstCharacter);
        }

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

    public static char FirstCharacter(string line)
    {
        int length = line.Length;
        int count = 0;

        for (int i = 0; i < length; i++)
        {
            count = line.Count(x => x == line[i]);
            if(count == 1)
            {
                firstCharacter = line[i];
                break;
            }
        }

        return firstCharacter;
    }
}

[EASY] Juggling With Zeros – CodeEval

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

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

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

            for (int j = 0; j < splitLine.Length; j++)
            {
                queue.Enqueue(splitLine[j]);
            }


            while (queue.Count > 0)
            {
                string zeros = queue.Dequeue();
                string add = "";

                if (zeros == "00")
                {
                    add = queue.Dequeue();
                    string addOnes = new string('1', add.Length);
                    binaryNumber += addOnes;
                    
                }
                if (zeros == "0")
                {
                    add = queue.Dequeue();
                    binaryNumber += add;
                }
            }

            long result = Convert.ToInt64(binaryNumber, 2);
            Console.WriteLine(result);
        }
    }
}

[EASY] Shortest Repetition – CodeEval

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

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

 

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
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>();
        string word = null;
        string shortWord = null;
        int countOfShortWord = 1;

        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++)
        {
            word = lines[i];
            shortWord = ShortWord(word);
            countOfShortWord = CountOfShortWord(word, shortWord);

            if (countOfShortWord == 1)
            {
                Console.WriteLine(word.Length);
            }
            else
            {
                Console.WriteLine(shortWord.Length);
            }
        }
    }

    public static string ShortWord(string word)
    {
        string shortWord = "";
        int length = word.Length;

        for (int i = 0; i < length; i++)
        {
            if(!shortWord.Contains(word[i]))
            {
                shortWord += word[i];
            }
            else
            {
                break;
            }
        }

        return shortWord;
    }

    public static int CountOfShortWord(string word, string shortWord)
    {
        int countOfShortWord = Regex.Matches(word, shortWord).Count;

        return countOfShortWord;
    }
}

[MODERATE] Counting Primes – CodeEval

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

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<int> numbers = new List<int>();
        char[] separator = new char[] { ',' };
        int firstNumber = 0, secondNumber = 0, countOfPrimes = 0;

        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++)
        {
            string split = lines[i];
            numbers = split.Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList();

            firstNumber = numbers[0];
            secondNumber = numbers[1];
            countOfPrimes = CountOfPrimes(firstNumber, secondNumber);

            Console.WriteLine(countOfPrimes);
        }
    }

    public static int CountOfPrimes(int startNumber, int endNumber)
    {
        int count = 0;

        for (int i = startNumber; i <= endNumber; i++)
        {
            if(IsPrime(i))
            {
                count++;
            }
        }

        return count;
    }

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

        return true;
    }
}

[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] Decimal To Binary – CodeEval

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

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]);
            string binary = Convert.ToString(number, 2);

            Console.WriteLine(binary);
        }
    }
}

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