15 August 2017

List Beautifier

My solution:
def listBeautifier(a):
    res = a[:]
    while res and res[0] != res[-1]:
       b, *res, c = res
    return res

Previous

Mex Function

My solution:
def mexFunction(s, upperBound):
    found = -1
    for i in range(upperBound):
        if not i in s:
            found = i
            break
    else:
        found = upperBound

    return found

Previous Next

Base Conversion

My solution:
def baseConversion(n, x):
    return hex(sum((int(n[i]) if n[i] <= '9' else ord(n[i]) - ord('a') + 10) * x ** (len(n) - 1 - i) for i in range(len(n))))[2:]

Previous Next

Simple Sort

My solution:
def simpleSort(arr):

    n = len(arr)

    for i in range(n):
        j = 0
        stop = n - i
        while j < stop - 1:
            if arr[j] > arr[j + 1]:
                arr[j + 1], arr[j] = arr[j], arr[j + 1]
            j += 1
    return arr

Previous Next

Modulus

My solution:
def modulus(n):
    if type(n) == int:
        return n % 2
    else:
        return -1

Previous Next

Count Bits

My solution:
def countBits(n):
    return n.bit_length()

Previous Next

Language Differences

My answer: "x = 15, y = -4"

Output:
Python 3:
-4
3
1
0
-4

Java:

-4
3
1
0
-3

Previous Next

Special Conditional

My answer: "a == not b"
Two operators ("=="  and "not") cannot be next to each other.

Previous Next

Efficient Comparison

My answer: "Option 1 is the most optimal one."

My solution:
import time
L = 1
x = 2
y = 3
R = 9
t1 = time.perf_counter()
def a():
 if L < x ** y <= R:
  return True

t2 = time.perf_counter()
print(t2 - t1)
t3 = time.perf_counter()
def b():
 if x ** y > L and x ** y <= R:
  return True

t4 = time.perf_counter()
print(t4 - t3)
t5 = time.perf_counter()
def c():
 if x ** y in range(L + 1, R + 1):
  return True

t6 = time.perf_counter()
print(t6 - t5)

Output:
5.131850057605017e-07
1.5395550172814964e-06
1.5395550172815045e-05

Previous Next

Collections Truthness

My answer: [True, False]
Explanation on 4.1 Truth Value Testing.

Next

isCryptSolution

My solution:
bool isCryptSolution(string[] crypt, char[][] solution) 
{
    var words = new List<decimal>();
    foreach (var word in crypt)
    {
        decimal wordsum = 0;
        for (var ci = 0; ci < word.Length; ci++)
            foreach (char[] t in solution)
            {
                if (word[ci] != t[0]) continue;
                if (word[0] == t[0] && t[1] == '0' && word.Length != 1) return false;
                wordsum += Convert.ToInt32(t[1].ToString()) *
                           Convert.ToDecimal(Math.Pow(10, word.Length - 1 - ci));
                break;
            }

        words.Add(wordsum);
    }

    return words[2] == words[0] + words[1];
}

Previous

sudoku2

My solution:
bool sudoku2(char[][] grid) 
{
    var columnList = new List<Dictionary<char, int>>();
    var rowList = new List<Dictionary<char, int>>();
    var nineList = new List<Dictionary<char, int>>();
    foreach (var t in grid)
    {
        var dict = new Dictionary<char, int>();
        for (var j = 0; j < grid[0].Length; j++)
            if (dict.ContainsKey(t[j]))
                dict[t[j]]++;
            else
                dict[t[j]] = 1;

        rowList.Add(dict);
    }

    if (rowList.Any(d => d.Count(pair => pair.Key != '.' && pair.Value > 1) > 0))
        return false;

    for (var i = 0; i < grid.Length; i++)
    {
        var dict = new Dictionary<char, int>();
        for (var j = 0; j < grid[0].Length; j++)
            if (dict.ContainsKey(grid[j][i]))
                dict[grid[j][i]]++;
            else
                dict[grid[j][i]] = 1;

        columnList.Add(dict);
    }

    if (columnList.Any(d => d.Count(pair => pair.Key != '.' && pair.Value > 1) > 0))
        return false;

    for (var i = 0; i < grid.Length / 3; i++)
    {
        for (var j = 0; j < grid[0].Length / 3; j++)
        {
            var dict = new Dictionary<char, int>();
            if (dict.ContainsKey(grid[i * 3][j * 3]))
                dict[grid[i * 3][j * 3]]++;
            else
                dict[grid[i * 3][j * 3]] = 1;

            if (dict.ContainsKey(grid[i * 3][j * 3 + 1]))
                dict[grid[i * 3][j * 3 + 1]]++;
            else
                dict[grid[i * 3][j * 3 + 1]] = 1;

            if (dict.ContainsKey(grid[i * 3][j * 3 + 2]))
                dict[grid[i * 3][j * 3 + 2]]++;
            else
                dict[grid[i * 3][j * 3 + 2]] = 1;

            if (dict.ContainsKey(grid[i * 3 + 1][j * 3]))
                dict[grid[i * 3 + 1][j * 3]]++;
            else
                dict[grid[i * 3 + 1][j * 3]] = 1;

            if (dict.ContainsKey(grid[i * 3 + 1][j * 3 + 1]))
                dict[grid[i * 3 + 1][j * 3 + 1]]++;
            else
                dict[grid[i * 3 + 1][j * 3 + 1]] = 1;

            if (dict.ContainsKey(grid[i * 3 + 1][j * 3 + 2]))
                dict[grid[i * 3 + 1][j * 3 + 2]]++;
            else
                dict[grid[i * 3 + 1][j * 3 + 2]] = 1;

            if (dict.ContainsKey(grid[i * 3 + 2][j * 3]))
                dict[grid[i * 3 + 2][j * 3]]++;
            else
                dict[grid[i * 3 + 2][j * 3]] = 1;

            if (dict.ContainsKey(grid[i * 3 + 2][j * 3 + 1]))
                dict[grid[i * 3 + 2][j * 3 + 1]]++;
            else
                dict[grid[i * 3 + 2][j * 3 + 1]] = 1;

            if (dict.ContainsKey(grid[i * 3 + 2][j * 3 + 2]))
                dict[grid[i * 3 + 2][j * 3 + 2]]++;
            else
                dict[grid[i * 3 + 2][j * 3 + 2]] = 1;

            nineList.Add(dict);
        }
    }

    return !nineList.Any(d => d.Count(pair => pair.Key != '.' && pair.Value > 1) > 0);
}

Previous Next

firstNotRepeatingCharacter

My solution:
char firstNotRepeatingCharacter(string s) 
{
    var d = new Dictionary<char, int>();
    foreach (var c in s)
        if (!d.ContainsKey(c))
            d[c] = 1;
        else
            d[c]++;

    return d.Any(pair => pair.Value == 1) ? d.Where(pair => pair.Value == 1).Select(pair => pair.Key).First() : '_';
}

Previous Next

firstDuplicate

My solution:
int firstDuplicate(int[] a) 
{
    var list = new SortedSet<int>();
    foreach (int d in a)
    {
        if (list.Contains(d))
            return d;
        
        list.Add(d);
    }
    
    return -1;
}

Next

rotateImage

My solution:
int[][] rotateImage(int[][] a) 
{
    var list = new List<List<int>>();
    foreach (var t in a)
    {
        var column = new List<int>();
        for (var j = 0; j < a[0].Length; j++)
            column.Add(t[j]);

        list.Add(column);
    }

    var solution = new int[a.Length][];
    for (var i = 0; i < a.Length; i++)
        solution[i] = new int[a.Length];

    for (var i = 0; i < a.Length; i++)
        for (var j = 0; j < a[0].Length; j++)
            solution[i][j] = list[a.Length - 1 - j][i];

    return solution;
}

Previous Next