Sort by

recency

|

950 Discussions

|

  • + 0 comments

    Can someone help me understand why my program, written in JavaScript programming language, failed some test cases in solving bigSort challenge?

    First of all (or scenario 1), I solved the "bigSort challenge" with application of bubble sort algorithms:

    function bigSort(unsorted) { const size = unsorted.length;

    for (let i = 0; i <= size - 1; i++) {
        let isSwapped = false;
    
        // Compare two values at a time, place
        // the bigger value at the right side of the array
        // and place the lower value at the left side of
        // array.
        for (let j = 0; j <= size - 1; j++) {
            if (parseInt(unsorted[j + 1]) < parseInt(unsorted[j])) {
    
                // Temporarily hold the least value.
                const minValue = unsorted[j + 1];
    
                unsorted[j + 1] = unsorted[j];
                unsorted[j] = minValue;
    
                // Track and update whenever some value are swapped.
                isSwapped = true;
            }
    
            if (!isSwapped) break;
        }
    
        return unsorted;
    }
    

    To my surprise, this fails test: test case 2, test case 7 and some others.

    Secondly (or scenario 2), I modified the code with the language in built "sort()" method. I provided it with the callback compare function as follows:

    functiion bigSort(unsorted) {
        return unsorted.sort((a, b) => a.localeCompare(b, undefined, {numeric: true});
    }
    

    I am surprised when I see test case 2 and 7 marked failed.

    Folks, kindly help me understand why some test cases failed, specifically test case 2 and 7 in the two scenarios.

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/GAvltofMdYc

    vector<string> bigSorting(vector<string> u) {
        sort(u.begin(), u.end(), [](string l, string r){
            if(l.size() == r.size()) return l < r;
            return l.size() < r.size();
        });
        return u;
    }
    
  • + 0 comments

    def bigSorting(unsorted): # Write your code here

    unsorted_1 = []
    unsorted_2 = []
    for i in unsorted:
        if len(i) <= 31:
            unsorted_1.append(i)
        else:
            unsorted_2.append(i)
    
    n = len(unsorted_2)
    for i in range(n-1, 0, -1):
        for j in range(i):
            if len(unsorted_2[j]) > len(unsorted_2[i]) or (len(unsorted_2[j]) == len(unsorted_2[i]) and unsorted_2[j] > unsorted_2[i]):
                unsorted_2[i], unsorted_2[j] = unsorted_2[j], unsorted_2[i]
    
    return list(map(str, sorted(list(map(int, unsorted_1))))) + unsorted_2
    
  • + 0 comments

    MERGE SORT IS still the fastest algorithm since o(n) complexity

    def merge(left, right):
        merged = []
        i = j = 0
        while i < len(left) and j < len(right):
            if compare_numbers(left[i], right[j]) <= 0:
                merged.append(left[i])
                i += 1
            else:
                merged.append(right[j])
                j += 1
        merged.extend(left[i:])
        merged.extend(right[j:])
        return merged
    def compare_numbers(a, b):
        # First, compare by length.
        if len(a) != len(b):
            return len(a) - len(b)
        if a < b:
            return -1
        elif a > b:
            return 1
        else:
            return 0
            
    def merge_sort(arr):
        if len(arr) <= 1:
            return arr
        mid = len(arr) // 2
        left = merge_sort(arr[:mid])
        right = merge_sort(arr[mid:])
        return merge(left, right)
        
    

    this is insertion sort (0)^2 ` def compare(res, current): if len(res) == 0: res = [current] return res for idx, pip in enumerate(res): if pip >= current: rep = idx if rep<0: rep = 0 res.insert(rep, current) return res res.append(current) print("final", res) return res

    def bigSorting(unsorted): # Write your code here res =[] for datas in unsorted: data = int(datas) res = compare(res,data) fix = [] for bub in res: fix.append(str(bub)) print(bub) return fix `

  • + 1 comment

    Python3, tried this way

    from functools import cmp_to_key
    
    def compare_big(num1, num2):
        if len(num1) > len(num2):
            return 1
        elif len(num1) < len(num2):
            return -1
        else:
            for d1, d2 in zip(num1, num2):
                if d1 > d2:
                    return 1
                elif d1 < d2:
                    return -1
            return 0
    
    def bigSorting(unsorted):
        return sorted(unsorted, key=cmp_to_key(compare_big))
        # Write your code herefrom functools import cmp_to_key
    
    def compare_big(num1, num2):
        if len(num1) > len(num2):
            return 1
        elif len(num1) < len(num2):
            return -1
        else:
            for d1, d2 in zip(num1, num2):
                if d1 > d2:
                    return 1
                elif d1 < d2:
                    return -1
            return 0
    
    def bigSorting(unsorted):
        return sorted(unsorted, key=cmp_to_key(compare_big))
        # Write your code here
    

    B

    • + 0 comments

      But you can always do

      return sorted(unsorted, key = lambda x: (len(x), x))