On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:

  • "L": turn 90 degrees to the left;
  • "R": turn 90 degrees to the right.

The robot performs the instructions given in order, and…

var prystring = function(char, num){
let string = '\n'
for (let i = 1 ; i <= num; i++){
for(let j = num; j > i; j--){
string += " "
}
for(let k = 0; k < i * 2 -1; k++){
string += `${char}`
}
for(let j = num; j > i; j--){
string += " "
}
string += "\n"
}
console.log(string)
}

prystring('*',5)
'
*
***
*****
*******
*********
'
var strings = function(char, num){
let string = '\n'
for (let i = 0 ; i < num; i++){
for(let j = 0; j < i; j++){
string += " "
}
for(let k = 0; k < 2 * (num-i) -1; k++){
string += `${char}`
}
string += "\n"
}
console.log(string)
}

strings('*',5)
'
*********
*******
*****
***
*
'

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

Example 1:

Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].

Example 2:

Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]

Given an array of unsorted numbers, find all unique triplets in it that add up to zero.

Example 1:

Input: [-3, 0, 1, 2, -1, 1, -2]
Output: [-3, 1, 2], [-2, 0, 2], [-2, 1, 1], [-1, 0, 1]
Explanation: There are four unique triplets whose sum is equal to zero…

function solution(S, K) {

let split = S.split(“ “)

console.log(split)

let lengths = split.map(word => word.length)

console.log(lengths)

let arr = S.split(“ “).map(word => word.length)

console.log(“arr”, arr)

let map = new Map()

let smsCount = 1

if ( Math.max(…arr) > K ) {

return -1

//if biggest item in array >…

/*
Simple String Sort Function
Given two arguments, a string of characters (input) and a sort string (sortStr),
implement a simple sort function that returns the characters to sort in the order
specified by the sort string. If any characters do not appear in the sort string,
they go at the end, in any order.
@Example
sort(‘banana’, ‘nab’) -> ‘nnaaab’
sort(‘house’, ‘soup’) -> ‘souhe’ or ‘soueh’
*/

function sort(input, sortStr) {
let sorted =[]
let remainder = input
for(let i =0; i<=sortStr.length -1; i++){
for(let j =0; j<=input.length; j++){
if (sortStr[i] == input[j]){
sorted.push(sortStr[i])
console.log(sorted)
}
}
}
remainder = remainder.split(“”)
let end =remainder.filter( x => !sorted.includes(x))
sorted.push(end.join(“”))
return sorted.join(“”)
}

sort(‘house’, ‘soup’)
sort(‘banana’, ‘nab’)

Write a function, linkedListValues, that takes in the head of a linked list as an argument. The function should return an array containing all values of the nodes in the linked list.

test_00:

const a = new Node("a");
const b = new Node("b");
const c = new Node("c");
const d = new Node("d")…

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Example 2:

Input: nums = [1]
Output: 1

Example 3:

Input: nums = [5,4,-1,7,8]
Output: 23

Solution:

var maxSubArray = function(nums) {
let sum = 0;
let maxSum = -Infinity;

if(nums.length === 0) return 0;
if(nums.length === 1) return nums[0]

for(let i = 0;i<nums.length;i++){
sum+=nums[i];
maxSum = Math.max(maxSum,sum);
if(sum < 0) sum = 0;
}
return maxSum;
};

Write a function, intersection, that takes in two arrays, a,b, as arguments. The function should return a new array containing elements that are in both of the two arrays.

You may assume that each input array does not contain duplicate elements.

const intersection = (a, b) => {
// todo
let set = new Set()
let arr = []
for( let i = 0; i < a.length; i++){
set.add(a[i])
}
for(let j = 0; j < b.length; j ++){
if (set.has(b[j])){
arr.push(b[j])
}
}
return arr
}

SunJet Liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store