[Python] CodeSignal 문제 풀이 (31~33)

2020. 3. 12. 21:17Python/CodeSignal Algorithm

31. You have deposited a specific amount of money into your bank account. Each year your balance increases at the same growth rate. With the assumption that you don't make any additional deposits, find out how long it would take for your balance to pass a specific threshold.

 

[Example]

For deposit = 100, rate = 20, and threshold = 170, the output should be
depositProfit(deposit, rate, threshold) = 3.

Each year the amount of money in your account increases by 20%. So throughout the years, your balance would be:

  • year 0: 100;
  • year 1: 120;
  • year 2: 144;
  • year 3: 172.8.

Thus, it will take 3 years for your balance to pass the threshold, so the answer is 3.

 

[Solution]

#<My Code>
def depositProfit(deposit, rate, threshold):
    depo= deposit
    cnt = 0
    
    while True:
        depo *= (1 + rate / 100)
        cnt += 1
        if threshold <= depo:
            return cnt

#<Best Code>
def depositProfit(deposit, rate, threshold):
    return math.ceil(math.log(threshold/deposit, 1+rate/100))

 

32. You have deposited a specific amount of money into your bank account. Each year your balance increases at the same growth rate. With the assumption that you don't make any additional deposits, find out how long it would take for your balance to pass a specific threshold.

 

[Example]

For deposit = 100, rate = 20, and threshold = 170, the output should be
depositProfit(deposit, rate, threshold) = 3.

Each year the amount of money in your account increases by 20%. So throughout the years, your balance would be:

 

[Solution]

#<My Code>
def depositProfit(deposit, rate, threshold):
    depo= deposit
    cnt = 0
    
    while True:
        depo *= (1 + rate / 100)
        cnt += 1
        if threshold <= depo:
            return cnt
            
#<Best Code>
def depositProfit(deposit, rate, threshold):
    return math.ceil(math.log(threshold/deposit, 1+rate/100))

 

32. iven a sorted array of integers a, your task is to determine which element of a is closest to all other values of a. In other words, find the element x in a, which minimizes the following sum:

abs(a[0] - x) + abs(a[1] - x) + ... + abs(a[a.length - 1] - x)

(where abs denotes the absolute value)

If there are several possible answers, output the smallest one.

 

[Example]

  • For a = [2, 4, 7], the output should be absoluteValuesSumMinimization(a) = 4.

    • for x = 2, the value will be abs(2 - 2) + abs(4 - 2) + abs(7 - 2) = 7.
    • for x = 4, the value will be abs(2 - 4) + abs(4 - 4) + abs(7 - 4) = 5.
    • for x = 7, the value will be abs(2 - 7) + abs(4 - 7) + abs(7 - 7) = 8.

    The lowest possible value is when x = 4, so the answer is 4.

  • For a = [2, 3], the output should be absoluteValuesSumMinimization(a) = 2.

    • for x = 2, the value will be abs(2 - 2) + abs(3 - 2) = 1.
    • for x = 3, the value will be abs(2 - 3) + abs(3 - 3) = 1.

    Because there is a tie, the smallest x between x = 2 and x = 3 is the answer.

 

[Solution]

#<My Code>
def absoluteValuesSumMinimization(a):
    minAbs, l = 0, 0

    for i in range(len(a)):
        sumAbs = 0
        for j in range(len(a)):
            if(i == 0):
                sumAbs += abs(a[i] - a[j])
                minAbs = sumAbs
                l = i
            else:
                sumAbs += abs(a[i] - a[j])
        if minAbs > sumAbs:
            minAbs = sumAbs
            l = i
    return a[l]
                
                
#<Best Code>
def absoluteValuesSumMinimization(A):
    return A[(len(A)-1)//2]

 

33. Given an array of equal-length strings, you'd like to know if it's possible to rearrange the order of the elements in such a way that each consecutive pair of strings differ by exactly one character. Return true if it's possible, and false if not.

 

[Example]

  • For inputArray = ["aba", "bbb", "bab"], the output should be
    stringsRearrangement(inputArray) = false.

    There are 6 possible arrangements for these strings:

    • ["aba", "bbb", "bab"]
    • ["aba", "bab", "bbb"]
    • ["bbb", "aba", "bab"]
    • ["bbb", "bab", "aba"]
    • ["bab", "bbb", "aba"]
    • ["bab", "aba", "bbb"]

    None of these satisfy the condition of consecutive strings differing by 1 character, so the answer is false.

  • For inputArray = ["ab", "bb", "aa"], the output should be
    stringsRearrangement(inputArray) = true.

    It's possible to arrange these strings in a way that each consecutive pair of strings differ by 1 character (eg: "aa", "ab", "bb" or "bb", "ab", "aa"), so return true.

 

[Solution]

def stringsRearrangement(inputArray):