|
12 | 12 | 10. [Maps and Sets](#maps-and-sets)
|
13 | 13 | 11. [Iterables and iterators](#iterables-and-iterators)
|
14 | 14 | 12. [Generators](#generators)
|
| 15 | +13. [Coding Problems](#coding-problems) |
15 | 16 |
|
16 | 17 | ## Variable scope
|
17 | 18 |
|
@@ -1269,3 +1270,247 @@ genObj1.return('Result')
|
1269 | 1270 | Exiting
|
1270 | 1271 | { value: 'Result', done: true }
|
1271 | 1272 | ```
|
| 1273 | +## Coding Problems |
| 1274 | +### Check prime number |
| 1275 | +A prime number is only divisible by itself and 1. |
| 1276 | +``` |
| 1277 | +function isPrime(num) { |
| 1278 | + for(var i = 2; i < num; i++) |
| 1279 | + if(num % i === 0) return false; |
| 1280 | + return num !== 1 && num !== 0; |
| 1281 | +} |
| 1282 | +
|
| 1283 | +isPrime(137); // true |
| 1284 | +``` |
| 1285 | +### Check prime number another way |
| 1286 | +You can decrease complexity of the function if you run the loop until square root of number: |
| 1287 | +``` |
| 1288 | +function isPrime (num) { |
| 1289 | + for(let i = 2, s = Math.sqrt(num); i <= s; i++) { |
| 1290 | + if(num % i === 0) return false; |
| 1291 | + } |
| 1292 | + return num !== 1 && num !== 0; |
| 1293 | +} |
| 1294 | +``` |
| 1295 | +### Find all the prime factors of the given number |
| 1296 | +``` |
| 1297 | +function findPrimeFactors (num) { |
| 1298 | +
|
| 1299 | + var primeFactors = []; |
| 1300 | + while (num % 2 === 0) { |
| 1301 | + primeFactors.push(2); |
| 1302 | + num = num / 2; |
| 1303 | + } |
| 1304 | + |
| 1305 | + var sqrtNum = Math.sqrt(num); |
| 1306 | + for (var i = 3; i <= sqrtNum; i++) { |
| 1307 | + while (num % i === 0) { |
| 1308 | + primeFactors.push(i); |
| 1309 | + num = num / i; |
| 1310 | + } |
| 1311 | + } |
| 1312 | +
|
| 1313 | + if (num > 2) { |
| 1314 | + primeFactors.push(num); |
| 1315 | + } |
| 1316 | + return primeFactors; |
| 1317 | +} |
| 1318 | +
|
| 1319 | +console.log(findPrimeFactors(10)); // [2, 5] |
| 1320 | +console.log(findPrimeFactors(11)); // [11] |
| 1321 | +``` |
| 1322 | +### Find the nth Fibonacci number |
| 1323 | +``` |
| 1324 | +function fibonacci(n){ |
| 1325 | + if(n<=1) |
| 1326 | + return n; |
| 1327 | + else |
| 1328 | + return fibonacci(n-1) + fibonacci (n-2); |
| 1329 | +} |
| 1330 | +
|
| 1331 | +fibonacci(12); // 144 |
| 1332 | +``` |
| 1333 | +### Find Greatest Common Divisor |
| 1334 | +Using Euclidean algorithm: |
| 1335 | +``` |
| 1336 | +function greatestCommonDivisor(a, b){ |
| 1337 | + if(b == 0) { |
| 1338 | + return a; |
| 1339 | + } |
| 1340 | + else { |
| 1341 | + return greatestCommonDivisor(b, a%b); |
| 1342 | + } |
| 1343 | +} |
| 1344 | +
|
| 1345 | +greatestCommonDivisor(14, 21); // 7 |
| 1346 | +``` |
| 1347 | +### Remove duplicate element from an array |
| 1348 | +Using `Array.filter` method, check each element's index is equal to the `indexOf` value of the array. |
| 1349 | +``` |
| 1350 | +function removeDuplicate(arr) { |
| 1351 | + return arr.filter((element, index, array) => array.indexOf(element) === index); |
| 1352 | +} |
| 1353 | +``` |
| 1354 | +Or in ES6 using `set`: |
| 1355 | +``` |
| 1356 | +function removeDuplicate(arr) { |
| 1357 | + return Array.from(new Set(arr)) |
| 1358 | +} |
| 1359 | +``` |
| 1360 | +### Merge two sorted arrays |
| 1361 | +Given two arrays of same length, merge them into sing array in ascending order. |
| 1362 | +``` |
| 1363 | +function mergeSortedArray(arr1, arr2) { |
| 1364 | + return [...arr1, ...arr2].sort((a, b) => a - b) |
| 1365 | +} |
| 1366 | +
|
| 1367 | +mergeSortedArray([2,5,6,9], [1,2,3,29]); |
| 1368 | +// [1, 2, 2, 3, 5, 6, 9, 29] |
| 1369 | +``` |
| 1370 | +### Swap two variables without temp |
| 1371 | +``` |
| 1372 | +function swapNumb(a, b){ |
| 1373 | + b = b - a; |
| 1374 | + a = a + b; |
| 1375 | + b = a - b; |
| 1376 | +} |
| 1377 | +``` |
| 1378 | +Using ES6: |
| 1379 | +``` |
| 1380 | +function swapNumb(a, b) { |
| 1381 | + console.log('before swap: ' + a + ' ' + b); |
| 1382 | + [a, b] = [b, a]; |
| 1383 | + console.log('after swap: ' + a + ' ' + b); |
| 1384 | +} |
| 1385 | +``` |
| 1386 | +### String reverse |
| 1387 | +Given a string, print the reverse of the string (ex: javascript becomes tpircsavaj). |
| 1388 | + |
| 1389 | +Without native methods: |
| 1390 | +``` |
| 1391 | +function reverse(str){ |
| 1392 | + var rtnStr = []; |
| 1393 | + if(!str || typeof str != 'string' || str.length < 2 ) return str; |
| 1394 | + |
| 1395 | + for(var i = str.length-1; i>=0;i--){ |
| 1396 | + rtnStr.push(str[i]); |
| 1397 | + } |
| 1398 | + return rtnStr.join(''); |
| 1399 | +} |
| 1400 | +``` |
| 1401 | +Using recursion: |
| 1402 | +``` |
| 1403 | +function reverse (str) { |
| 1404 | + if (str === "") { |
| 1405 | + return ""; |
| 1406 | + } else { |
| 1407 | + return reverse(str.substr(1)) + str.charAt(0); |
| 1408 | + } |
| 1409 | +} |
| 1410 | +``` |
| 1411 | +Using native methods: |
| 1412 | +``` |
| 1413 | +function reverse(str){ |
| 1414 | + if(!str || str.length <2) return str; |
| 1415 | + |
| 1416 | + return str.split('').reverse().join(''); |
| 1417 | +} |
| 1418 | +``` |
| 1419 | +### Reverse in Place |
| 1420 | +Given a string, reverse the letters of word but preserve the word order (ex: "I am the good boy" becomes "I ma eht doog yob") |
| 1421 | +``` |
| 1422 | +function reverseInPlace(str){ |
| 1423 | + return str.split(' ').reverse().join(' ').split('').reverse().join(''); |
| 1424 | +} |
| 1425 | +``` |
| 1426 | +### Print the first non repeating character in a string |
| 1427 | +Given a string, print the first non-repeating letter in the string. |
| 1428 | +``` |
| 1429 | +function firstNonRepeatedCharacter(string) { |
| 1430 | + for (var i = 0; i < string.length; i++) { |
| 1431 | + var c = string.charAt(i); |
| 1432 | + if (string.indexOf(c) == i && string.indexOf(c, i + 1) == -1) { |
| 1433 | + return c; |
| 1434 | + } |
| 1435 | + } |
| 1436 | + return null; |
| 1437 | +} |
| 1438 | +``` |
| 1439 | +The `indexOf` method takes one additional optional parameter `start` at which index start the search. So check if the character is found after the current index. |
| 1440 | +### Check if a given string is palindrom |
| 1441 | +A string is palindrom if the reverse order is same as original. |
| 1442 | +``` |
| 1443 | +function checkPalindrom(str) { |
| 1444 | + return str == str.split('').reverse().join(''); |
| 1445 | +} |
| 1446 | +``` |
| 1447 | +### Generate random number between two given |
| 1448 | +``` |
| 1449 | +function generateRandom(start, end) { |
| 1450 | + return Math.floor(Math.random() * end) + start; |
| 1451 | +} |
| 1452 | +``` |
| 1453 | +### Find the missing number |
| 1454 | +Given a unsorted array of numbers 1 to 100 excluding one number, find the missing number. |
| 1455 | + |
| 1456 | +The sum of a linear series of n numbers is equal to n*(n+1)/2. |
| 1457 | + |
| 1458 | +``` |
| 1459 | +function missingNumber(arr){ |
| 1460 | + var n = arr.length+1, |
| 1461 | + sum = 0, |
| 1462 | + expectedSum = n* (n+1)/2; |
| 1463 | + |
| 1464 | + sum = arr.reduce((total, num) => total + num); |
| 1465 | + return expectedSum - sum; |
| 1466 | +} |
| 1467 | +
|
| 1468 | +missingNumber([5, 2, 6, 1, 3]); |
| 1469 | +// 4 |
| 1470 | +``` |
| 1471 | +### Find the largest and smallest number in an array |
| 1472 | +Given an unsorted array, find the largest and smallest number. |
| 1473 | +``` |
| 1474 | +function findLargestAndSmallest(arr) { |
| 1475 | + arr = arr.sort((a, b) => a - b); |
| 1476 | + return [arr[0], arr[arr.length - 1]]; |
| 1477 | +} |
| 1478 | +
|
| 1479 | +findLargestAndSmallest([1, 100, 2, -1, 2, 0]) |
| 1480 | +// [-1, 100] |
| 1481 | +``` |
| 1482 | +Using native mehtods and `apply`: |
| 1483 | +``` |
| 1484 | +function findLargestAndSmallest(arr) { |
| 1485 | + let smallest = Math.min.apply(null, arr); |
| 1486 | + let largest = Math.max.apply(null, arr); |
| 1487 | + return [smallest, largest]; |
| 1488 | +} |
| 1489 | +``` |
| 1490 | +### Permutations of a string |
| 1491 | +Get all permutations of a string |
| 1492 | +``` |
| 1493 | +function permut(string) { |
| 1494 | + if (string.length < 2) return string; // This is our break condition |
| 1495 | +
|
| 1496 | + var permutations = []; // This array will hold our permutations |
| 1497 | +
|
| 1498 | + for (var i=0; i<string.length; i++) { |
| 1499 | + var char = string[i]; |
| 1500 | +
|
| 1501 | + // Cause we don't want any duplicates: |
| 1502 | + if (string.indexOf(char) != i) // if char was used already |
| 1503 | + continue; // skip it this time |
| 1504 | +
|
| 1505 | + var remainingString = string.slice(0,i) + string.slice(i+1,string.length); |
| 1506 | +
|
| 1507 | + for (var subPermutation of permut(remainingString)) |
| 1508 | + permutations.push(char + subPermutation) |
| 1509 | +
|
| 1510 | + } |
| 1511 | + return permutations; |
| 1512 | +} |
| 1513 | +
|
| 1514 | +let permutations = permut('xyz'); |
| 1515 | +// ["xyz", "xzy", "yxz", "yzx", "zxy", "zyx"] |
| 1516 | +``` |
0 commit comments