Showing posts with label Through the Fog. Show all posts
Showing posts with label Through the Fog. Show all posts

01 April 2017

stringsRearrangement

My solution:
bool stringsRearrangement(string[] inputArray) 
{
    IEnumerable<IEnumerable<int>> permutations =
    GetPermutations(Enumerable.Range(1, inputArray.Length), inputArray.Length);
    Dictionary<int, bool> dictionary = new Dictionary<int, bool>();
    int key = 0;
    foreach (IEnumerable<int> intArray in permutations)
    {
        key++;
        List<int> intList = intArray.ToList();
        for (int i = 0; i < intList.Count - 1; i++)
        {
            int diffcount = 0;
            for (int j = 0; j < inputArray[intList[i] - 1].Length; j++)
                if (inputArray[intList[i] - 1][j] != inputArray[intList[i + 1] - 1][j])
                    diffcount++;

            if (diffcount != 1)
            {
                dictionary.Add(key, false);
                break;
            }
        }

        if (!dictionary.ContainsKey(key))
            dictionary.Add(key, true);
    }

    return dictionary.Any(x => x.Value);
}

IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
{
    if (length == 1) return list.Select(t => (IEnumerable<T>)new T[] { t });

    return GetPermutations(list, length - 1)
        .SelectMany(t => list.Where(e => !t.Contains(e)),
            (t1, t2) => t1.Concat(new T[] { t2 }));
}
Terms of use: Method by pengyang, used under CC BY-SA 3.0. 

Previous Next

absoluteValuesSumMinimization

My solution:
int absoluteValuesSumMinimization(int[] A) 
{
    int smallest = int.MaxValue;
    int x = 0;
    foreach (int i in A)
    {
        if (A.Sum(t => Math.Abs(t - i)) >= smallest) continue;
        smallest = A.Sum(t => Math.Abs(t - i));
        x = i;
    }

    return x;
}

Previous Next

depositProfit

My solution:
int depositProfit(int deposit, int rate, int threshold) 
{
    return Convert.ToInt32(Math.Ceiling(Math.Log(Convert.ToDouble(threshold) / Convert.ToDouble(deposit), 1 + Convert.ToDouble(rate) / 100)));
}

Previous Next

Circle of Numbers

My solution:
int circleOfNumbers(int n, int firstNumber) 
{
    return (firstNumber + n / 2) % n;
}

Previous Next