/ts-ds-basics

basic data structures in typescript with mocha testing

Primary LanguageTypeScript

Have excercises to suggest? See Contribution guide

Lists

  1. Flatten array of arrays (Code/Test)
flatten([
  [1, 2],
  [3, 4],
  [5, 6]
]); // -> [1,2,3,4,5,6]
  1. Given list of integers return sum of all the list's elements (Code/Test)
sum([1, 2, 3, 4]); // -> 10
  1. Write a function that takes in array of simple values (numbers,strings) and returns original array without duplicated records (Code/Test)
unique([1, 2, 3, 4, 5, 2, 3, 4, 1]); // -> [1,2,3,4,5]
  1. Write a function that returns every n-th element of an array (Code/Test)
getNthElement([1, 2, 3, 4, 5, 6, 7, 8], 4); // -> [4,8]
  1. Write a function that returns first n elements of array
getFirstElements([1, 2, 3, 4, 5], 3); // -> [1,2,3]
  1. Write a function that takes in a list of numbers and returns difference between smallest and largest value (Code/Test)
arrayValueDiff([1, 2, 3, 4]); // -> 3
arrayValueDiff([-1, 2, 3, 5]); // -> 6
arrayValueDiff([3, 3, 3]); // -> 0
  1. Write a function to count the occurrences of a value in an array. (Code/Test)
countValueOccurences([1, 2, 3, 4, 5, 2, 3, 1, 2], 2); // -> 3
  1. Given list of integers return multiplication of all the list's elements (Code/Test)
multiply([1, 2, 3, 4]); // -> 24

Objects

  1. Given a map of employee salaries return sum of all salaries
const salariesExample = {
  Max: 50,
  Anna: 50,
  John: 100
};
sumSalaries(salariesExample); // -> 200
  1. Query string builder (Code/Test)
composeQueryString({ version: 1, user: "Mike", country: "BLR" }); // -> “?version=1&user=Mike&country=BLR”
  1. Write a function that takes in a collection of items, a parameter value and returns a hash map of lists entities groupped by parameter value (Code/Test)
composeGroupsByProperty([
  { age: 20, name: "Peter" },
  { age: 30, name: "Mike" }
]);
// -> {20:[{age:20,name:"Peter"}],30:[{age:30,name:"Mike"}]}
  1. Given a hashmap of values return the key with the highest value (Code/Test)
const map = {
  "Holly Jackson": 250,
  "Jack Southerland": 100,
  "Peter Higgins": 50
};
findHighestHashValue(map); // -> "Holly Jackson"
  1. Given an object an list of strings return a new object with key names present in a list (Code/Test)
const baseObject = { xml: 4, http: 2, rest: 2, js: 10 };
const baseFields = ["xml", "js"];
mapObject(baseObject, baseFields); // -> { xml: 4, js: 10 };
  1. Given a string that represents a chain of object properties return the value from the chain (Code/Test)
const data = {
    version: "0.6",
    account: {
        isProtected: true
    }
};
searchInChain("version",data); // -> 0.6
searchInChain("account",data); // -> {isProtected: true}
searchInChain("account.isProtected",data); // -> true

Strings

  1. Write a function that checks whether or not string is a palindrome (Code/Test)
isPalindrome("xanax"); // -> true
isPalindrome("xanax2"); // -> false
  1. Write a function that takes in a string and returns its abbreviation (Code/Test)
composeAbbreviation("Denial Of Service"); // -> "DOS"
composeAbbreviation("United Nations"); // -> "UN
  1. Write a function that excludes all integers that are <= 1000 from a string of integers. (Code/Test)
greaterThan1000("200 150 1200 400 20 1400"); // -> “1200 1400”.
  1. Write a function that returns first recurring character in a string. (Code/Test)
getFirstRecurringCharacter("ABCDEA"); // -> “A”
  1. Write a function that takes in a string and returns whether the first character of a string is in uppercase (Code/Test)
isFirstCharacterUppercase("ABCDEA"); // -> true
isFirstCharacterUppercase("abcdea"); // -> false
  1. Write a function that takes in a string and a number n of repetitions and returns new string, that is original string repeated n times (Code/Test)
composeCharacterSequence({ sequence: "+", repeatCount: 5 }); // -> "+++++"
  1. Write a function that takes in a string and an array of characters. If a string has a character that’s included in an array then make the character uppercase. (Code/Test)
targetUppercase("123xyzabc", ["x", "c"]); // -> ‘123XyzabC’;
  1. Write a function that takes in a string and returns an object with the following properties of the given string: (Code/Test)
  • length of the string
  • number of unique characters used to compose a string
  • Hash table with the following format -> {character: number of times character occurs in the string};
const string = "abcdeabc";
const heatmap = getHeatmap(string);
/* 
heatmap.length -> 8;
heatmap.uniqueTokensCount -> 5;
heatmap.map -> {
      a: 2,
      b: 2,
      c: 2,
      d: 1,
      e: 1
    };
*/
  1. Write a function that takes a string and converts it to camel case (Code/Test)
toCamelCase("A string of values"); // -> "aStringOfValues";
  1. Given a string return a list of all capitalized words (Code/Test)
getCapitalizedWords("Visit United States of America today!"); // -> ["Visit","United","States","America"]
  1. Given a string that contains snake_case'd words, return a string where those words are surrounded with provided HTML tag. (Code/Test)
surroundSnakeCase(
  "a_snake_case_word followed by normal word and then snake_case_again",
  "h1"
); // -> "<h1>a_snake_case_word</h1> followed by normal word and then <h1>snake_case_again</h1>"
  1. Implement "Fuzzy search" function (Code/Test)
fuzzySearch("car", "cartwheel"); // -> true
fuzzySearch("cwhl", "cartwheel"); // -> true
fuzzySearch("cwheel", "cartwheel"); // -> true
fuzzySearch("cartwheel", "cartwheel"); // -> true
fuzzySearch("cwheeel", "cartwheel"); // -> false
fuzzySearch("lw", "cartwheel"); // -> false
  1. Implement function that checks for parenthesis validity in a string (Code/Test)
getParenthesisValidity("()"); // -> true
getParenthesisValidity("()()"); // -> true
getParenthesisValidity("(()())"); // -> true
getParenthesisValidity(")("); // -> false
getParenthesisValidity("())"); // -> false