# Ngày 25/06 ## Bài tập 1 ### Example 1: Input: nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]] Output: [3,2,7,1] ### Example 2: Input: nums = [1,2], operations = [[1,3],[2,1],[3,2]] Output: [2,1] ## Bài 2 Input: tickets = [2,3,2], k = 2 Output: 6 Explanation: - In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1]. - In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0]. The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds. Input: tickets = [5,1,1,1], k = 0 Output: 8 Explanation: - In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0]. - In the next 4 passes, only the person in position 0 is buying tickets. The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds. > [name=mrnamcpvn] --- ## Bài 3 > [name=Nhun Nhun] Input: [1, 2, 3, 4, 5] Output: 9 Input: [4, 3, 2] Output: 4 Input: [] Output: -1 ## Bài 4 Input: s = "the sky is blue" Output: "blue is sky the" Input: s = " hello world " Output: "world hello" Input: s = "a good example" Output: "example good a" ## Bài 5 Input: nums = [10,2] Output: "210" Input: nums = [3,30,34,5,9] Output: "9534330" ## Bài 6 > [name=vuong.nguyen] > token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJOT1RFIjoiQ8OgaSB0aMawIHZp4buHbiAnbW9yc2UtY29kZS1qcycgxJHhu4MgZ2nhuqNpIG3huq10IHRoxrAgW0tFWV0sIHNhdSDEkcOzIGTDuW5nIFtLRVldIMSR4buDIGdp4bqjaSBbTUVTU0FHRV0iLCJLRVkiOiIqLS4tKi4qKioqfC0uKioqKi4qLS4tKi4qKioqfC0qLioqLiouKi4tKnwtKi4qLS4qLS0uLS18LSouKi0uLSotLXwtKiouLSoqLiotLi0qKi18LSouLS0qLi0tLS4tLioqKnwtKi4tLSouKioqKi4qLi0uKioqfCoqKi0tLi0tLS0tfCotKi4tLS0uLS0tLioqLioqLSoiLCJNRVNTQUdFIjoiJzMyLjUwLjQ0LjQ0LjM4LjQ4IDQ5LjUwLjMwLjMwLjQzLjM1IDUxLjUwLjM4IDUxLjM0LjM0LjQ3IiwibmJmIjoxNjU2MDU4ODkyLCJleHAiOjE2NTYxNDUyOTIsImlhdCI6MTY1NjA1ODg5Mn0.W5ILmwmV66uOuieMo_J-phMtcI9hJ1iFZBr6q1dxo2Lxb1waTc-zn5TUtdzhnDS3Al4tffb02ag0BhELYlEvpg" - Mở trang https://jwt.io/ - Copy token và paste vào thì sẽ trả về kết quả sau ```typescript= { "NOTE": "Cài thư viện 'morse-code-js' để giải mật thư [KEY], sau đó dùng [KEY] để giải [MESSAGE]", "KEY": "*-.-*.****|-.****.*-.-*.****|-*.**.*.*.-*|-*.*-.*--.--|-*.*-.-*--|-**.-**.*-.-**-|-*.--*.---.-.***|-*.--*.****.*.-.***|***--.-----|*-*.---.---.**.**-*", "MESSAGE": "'32.50.44.44.38.48 49.50.30.30.43.35 51.50.38 51.34.34.47", "nbf": 1656058892, "exp": 1656145292, "iat": 1656058892 } ``` - Mở https://stackblitz.com/, chọn dự án Typescript và cài package 'morse-code-js'; - Nội dung file index.ts ```typescript= import * as morseCode from 'morse-code-js'; // SOLVE [KEY] const key = morseCode.stringSentence( '*-.-*.****|-.****.*-.-*.****|-*.**.*.*.-*|-*.*-.*--.--|-*.*-.-*--|-**.-**.*-.-**-|-*.--*.---.-.***|-*.--*.****.*.-.***|***--.-----|*-*.---.---.**.**-*' ); console.log(key); // ANH THANH NIEEN NAWM NAY DDAX NGOTS NGHETS 30 ROOIF => A = 30 // SOLVE [MESSAGE] const alpla: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const nums: number[] = [ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, ]; const decode = (msg: string): string => { let result: string[] = []; let words: string[] = msg.split(' '); words.forEach((word) => { const characters: string[] = word.split('.'); characters.forEach((char) => { const index: number = nums.indexOf(+char); result.push(alpla[index]); }); result.push(' '); }); return result.join(''); }; console.log(decode('32.50.44.44.38.48 49.50.30.30.43.35 51.50.38 51.34.34.47')); // CUOOIS TUAANF VUI VEER ``` ## Bài 7 # Bài 6: Giải mật thư Người này là ai? ### Gợi ý 1 --- > 72677384857267 > `Key: Xe Êm biển số 67` ``` Giải thích: X Ê => C = 67 ``` ```typescript= function NumberToString(num: number) { let arr = []; let originArr = String(num).split(''); for (let i = 0; i = originArr.length / 2; i++) { arr.push(Number.parseInt(originArr.splice(originArr.length - 2, 2).join(''))); } return arr.map(x=> String.fromCharCode(x)).join(''); } Đ.Án: CHUTICH ``` ### Gợi ý 2 --- > 11980950 > ``` Ta cùng chia đội Tám người cùng chơi Chẵn chẵn lẻ lẻ Lấy hai hai bốn ``` ```typescript= function ChiaDoi(num: string){ let arr = [...num.split('').reverse().filter((x,i)=> i % 2 == 0), ...num.split('').reverse().filter((x,i)=> i % 2 != 0)] return [arr.splice(0,2).join(''), arr.splice(0,2).join(''), arr.join('')]; } // Đ.Án : 19 05 1890 ``` ### Gợi ý 3 --- > Mật Thư: GUHINYGNUESNCN ``` Key: Rắn Ăn Đuôi ``` ```typescript= function RanAnDuoi(str: string) { // Lấy giá trị ở đầu 1, 3 5 ,7 ,9 ,11, 13 let firstArr = str.split('').splice(0, str.length / 2); // Lấy giá trị cuối, đảo ngược lại => 2,4,6,8... let afterArr = str.split('').splice(str.length / 2, str.length).reverse(); // Sắp xếp let arrs: string[] = []; firstArr.forEach((x, i) => { arrs.push(firstArr[i], afterArr[i]); }) // Đảo ngược, join return arrs.reverse().join(''); } Đ.Án: NGUYENSINHCUNG ``` --- # Bài 8 *Input: x = 1 => Output: [[1]] Input: x = 2 => Output: [[1,2],[2,4]] Input: x = 3 => Output: [[1, 2, 3], [2, 4, 6], [3, 6, 9]]* --- ```typescript= function multiplicationTable (size: number): number[][] { let arrRs = []; for (let i = 1; i <= size; i++) { let arr = []; for (let j = 1; j <= size; j++) { arr.push(j * i); } arrRs.push(arr); } return arrRs; } ``` ```typescript= function multiplicationTable (size: number): number[][] { return Array.from({length: size}, (v,i) => Array.from({length: size}, (k,j) => (i+1)*(j+1)) ) } ``` ## Bài 9 > [name=Tánh] - input: "a!@Bc&D eF$" - output:"F!@eD&c Ba$" - hoán đổi vị trí ký tự đối xứng nhau nhưng không làm thay đổi vị trí ký tự đặc biệt. ```typescript= function swapCharacter(s: string){ let temp = []; for(let i=0; i<s.length; i++){ if(isCheckAnphabet(s[i])){ temp.push(s[i]); } } let tempReverse = temp.reverse(); let s1 = s.split(''); let j=0; for(let i = 0; i < s1.length; i++){ if(isCheckAnphabet(s1[i])){ s1[i] = tempReverse[j]; j++; }else continue; } console.log(s) ; console.log(s1.join('')); } function isCheckAnphabet(character: string): boolean{ return ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')); } ``` swapCharacter("a!@Bc&D eF$"); ## Giải bài 1 ```typescript= function arrayChange(nums: number[], operations: number[][]): number[] { operations.forEach(item => { nums.splice(nums.findIndex(x => x == item[0]), 1, item[1]); }); return nums; } console.log(arrayChange([1,2,4,6], [[1,3],[4,7],[6,1]])); console.log(arrayChange([1,2], [[1,3],[2,1],[3,2]])); ``` --- ## Giải bài 2 ```typescript= function timeRequiredToBuy(tickets: number[], k: number): number { let countTime = 0; while(tickets[k] !== 0){ for(let i = 0; i < tickets.length; i++){ if(tickets[k] == 0){ return countTime; } if(tickets[i] !== 0){ tickets[i] = tickets[i] - 1; countTime++; } } } return countTime; }; ``` console.log(timeRequiredToBuy([2,3,2],2)) console.log(timeRequiredToBuy([2,3,2],2)) --- ## Giải bài 3 ```typescript= function sumNumbersLargerAverage(a: number[]){ var tb = a.reduce((a,b) => a+b, 0) / a.length; console.log("tb", tb) var res = a.filter(v => v > tb); console.log("res", res) return res.length ? res.reduce((a,b) => a+b, 0) : -1; } ``` --- ## Giải bài 4 ```typescript= function reverseWords(s: string): string { return (s.split(/(\s+)/).filter(v => { return v.trim().length > 0; } )).reverse().join(' '); }; ```