This is the 29th day of my participation in the August More Text Challenge

1. FIZZBUZZ

Write a program to print the numbers 1 through 100. For multiples of 3, print “Fizz” and for multiples of 5, print “Buzz” and print “FizzBuzz”.

2. Binary search

Binary search (binary search), also called half-interval search, is a search algorithm that looks for a specific element in an ordered array. So binary search is only possible if the array is ordered.

3. Factorials & Recursion

The general way to do factorial

The recursive method implements factorial

4. Find the most common words in array

There are two main steps:

  • Count the number of occurrences of each word
  • Compare the number of words and find the word with the largest number

5. Reverse every other word

All you have to do here is reverse all the words. Implementation steps:

  • Divide sentences into words according to the space in the middle
  • Make new sentences by turning words backwards and putting Spaces between them

The words that specify the singular are reversed.

Remove vowels from the word, using for in loop to avoid five calls to replacingOccurrences.

6. Fibonacci Sequence

The Fibonacci sequence, also known as the Golden section sequence, was introduced by mathematician Leonardoda Fibonacci as an example of rabbit reproduction. It is also known as the Rabbit sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34… That is, any number except the first and second digits is the sum of the first two digits. The Fibonacci sequence can be implemented this way.

Fibonacci sequence is realized by recursive method

7. Higher Order Functions

A function that accepts one or more functions as arguments or as a return value is called a higher-order function. Using higher-order functions simplifies code, makes logic clearer, and is faster than traditional implementations when the data is large.

Map

Loop through the array, take the same action on each element in the array, and return the array.

Filter

Iterate over the number group, filter the elements that do not meet the conditions and return the array of elements that meet the conditions.

Reduce

Reduce combines all the values in the set and returns a new value.

Several abbreviations:

8. Binary tree lookup values

In general, binary trees are more efficient than array traversal lookup, as opposed to finding a value in an array. And if you’re looking for a value that doesn’t exist in the array, the array needs to traverse all elements, whereas a binary tree only needs to look up its height +1 times.

To build a binary tree, you define node

So this is binary tree lookup.

Because we know that the value of the left subtree of the binary tree must be less than the parent, and the value of the right subtree must be greater than the parent, we can optimize the above lookup method.