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