How Many Numbers Are Smaller Than the Current Number
In this lab, you will be given an array of integers nums
. For each nums[i]
, you need to find out how many numbers in the array are smaller than it. That is, for each nums[i]
you have to count the number of valid j's
such that j != i
and nums[j] < nums[i]
. Your task is to implement a function smallerNumbersThanCurrent
that takes in an array of integers and returns the result in another array.
Make sure to use ESM import/export in your solution.
Example
Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
Constraints
2 <= nums.length <= 500
0 <= nums[i] <= 100
Challenges
- Export your function
smallerNumbersThanCurrent
. - Test your function with the given examples and expect the correct output in each test case.
Evaluation script
1import fs from 'fs' 2import { smallerNumbersThanCurrent } from '/home/damner/code/index.js' 3 4const testlog = [] 5 6// this first block matches with - Challenge 1 7try { 8 if(smallerNumbersThanCurrent !== undefined) { 9 testlog.push({ status: 'pass' }) 10 } else { 11 throw new Error('Function is not exported properly') 12 } 13} catch(error) { 14 testlog.push({ 15 status: 'error', 16 error: error.message || 'Challenge 1 failed' 17 }) 18} 19 20// this second block matches with - Challenge 2 21try { 22 const result1 = smallerNumbersThanCurrent([8,1,2,2,3]) 23 const expected1 = [4,0,1,1,3] 24 const result2 = smallerNumbersThanCurrent([6,5,4,8]) 25 const expected2 = [2,1,0,3] 26 const result3 = smallerNumbersThanCurrent([7,7,7,7]) 27 const expected3 = [0,0,0,0] 28 29 if(JSON.stringify(result1) === JSON.stringify(expected1) && 30 JSON.stringify(result2) === JSON.stringify(expected2) && 31 JSON.stringify(result3) === JSON.stringify(expected3)) { 32 testlog.push({ status: 'pass' }) 33 } else { 34 throw new Error('Function does not return expected results') 35 } 36} catch(error) { 37 testlog.push({ 38 status: 'error', 39 error: error.message || 'Challenge 2 failed' 40 }) 41} 42 43fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog)) 44fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')))
Setting up test environment script
1#!/bin/bash 2set -e 1 3 4mkdir -p /home/damner/code/.labtests 5 6cat > /home/damner/code/.labtests/package.json << EOF 7{ 8 "type": "module" 9} 10EOF 11 12cd /home/damner/code/.labtests 13mv $TEST_FILE_NAME ./nodecheck.test.js 14 15# import puppeteer doesn't work without it 16npm link puppeteer 17 18node nodecheck.test.js 2>&1 | tee evaluationscript.log
Initial file system for user
/** * @param {number[]} nums * @return {number[]} */ export var smallerNumbersThanCurrent = function(nums) { };
{ "name": "codedamn-lab", "type": "module" }
tabs: ['index.js']
terminals: ['yarn install']