D27_ALG 101-Unit 6


Posted by Christy on 2021-05-15

LIOJ 1041:String trim

  • 解題想法:一開始是想碰到字以後就結束,但是字不一定是英文字母,也有可能是標點符號或其他之類的,那這樣就不成立了。另外一個重點是,這樣寫沒辦法去掉後面的空白。
function solve(lines) {
    let s = lines[0]
    console.log(trim(s))
    //BTW, 寫成這樣就好了 console.log(trim(lines[0]))
}

// LIOJ 1041:String trim
function trim(str) {
    let result = ''
    for(let i=0; i<str.length; i++) {
        // if(str[i] >= a && str[i] <= z) {
        //  break
        // } 
        if(str[i] !== ' ') {
            result += str[i]
        }
    }
    return str
}
  • 看了解答發現要分成幾個部份,第一個是去掉前面的空白 + 遇到字以後就留下來;第二個是去掉後面的空白,這時候要用迴圈倒著跑,可是輸入會有問題,因此最後的 result += str[i] 要寫成 result = str[i] + result

  • flag variable,reference: What is a flag variable?

A flag variable, in its simplest form, is a variable you define to have one value until some condition is true, in which case you change the variable's value. It is a variable you can use to control the flow of a function or statement, allowing you to check for certain conditions while your function progresses.

  • 我把 flag variable 當作是一個條件式,先設為 true or false,當某個條件成立時,在這個案例裡面是「當遇到字串時」
var readline = require('readline');

var lines = []
var rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', function (line) {
  lines.push(line)
});

rl.on('close', function() {
  solve(lines)
})

function solve(lines) {
    console.log(trim(lines[0]))
}

// LIOJ 1041:String trim
function trim(str) {
    let result = ''
    //下面這行是一個 flag variable
    let FrontSpaceEnds = false
    for(let i=0; i<str.length; i++) {
        if(str[i] !== ' ' || FrontSpaceEnds) {
            FrontSpaceEnds = true
            result += str[i]
        }
    }
    let result2 = ''
    let BackSpaceEnds = false
    for(let i=result.length - 1; i>=0; i--) {
        if(result[i] !==  ' ' || BackSpaceEnds) {
            BackSpaceEnds = true
            result2 = result[i] + result2
        }
    }
    return result2
}

LIOJ 1042:String toLowerCase

  • Make an imitation of String toLowerCase function
var readline = require('readline');

var lines = []
var rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', function (line) {
  lines.push(line)
});

rl.on('close', function() {
  solve(lines)
})

function solve(lines) {
    console.log(toLowerCase(lines[0]))
}

function toLowerCase(str) {
    let result = ''
    for(let i=0; i<str.length; i++) {
        if(str[i] >= 'A' && str[i] <= 'Z') {
        //OJ出現 runtime error,所以要寫一個
        //let code = str.charCodeAt(i)
        //下面有正解
            result += String.fromCharCode(str.charCodeAt(i) + 32)
        } else {
            result += str[i]
        }
    }
    return result
}
var readline = require('readline');

var lines = []
var rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', function (line) {
  lines.push(line)
});

rl.on('close', function() {
  solve(lines)
})

function solve(lines) {
    console.log(toLowerCase(lines[0]))
}

function toLowerCase(str) {
    let result = ''
    for(let i=0; i<str.length; i++) {
        if(str[i] >= 'A' && str[i] <= 'Z') {
            let code = str.charCodeAt(i)
            result += String.fromCharCode(code + 32)
        } else {
            result += str[i]
        }
    }
    return result
}

LIOJ 1043:String endsWith

  • Make an imitation of String endsWith function
  • 解題想法:從後面開始找,迴圈倒著跑,每一項去比對,比到 target 的長度就結束
  • 這是福爾摩斯的演繹法,先刪去所有不可能,剩下的就是可能,我好久沒看福爾摩斯了...也好久沒吃摩斯漢堡了,懷念~

When you have eliminated the impossible, whatever remains, however improbable, must be the truth.
---Arthur Conan Doyle

var readline = require('readline');

var lines = []
var rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', function (line) {
  lines.push(line)
});

rl.on('close', function() {
  solve(lines)
})

function solve(lines) {
    let str = lines[0]
    let target = lines[1]
    console.log(endsWith(str, target))
}

function endsWith(str, searchString) {
    if(searchString.length > str.length) {
        return false
    }
    let strIndex = str.length - 1
    let searchStrIndex = searchString.length - 1

    while(searchStrIndex >= 0) {
        if(str[strIndex] !== searchString[searchStrIndex]) {
            return false
        }
        strIndex--
        searchStrIndex--
    }
    return true
}

LIOJ 1044:String padEnd

  • Make an imitation of String padEnd function
  • 解題想法:先處理原本字串的長度已經大於等於規定的長度時,就直接回傳原字串;接著就是把新字串加上舊字串,然後把多餘的長度截掉
var readline = require('readline');

var lines = []
var rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', function (line) {
  lines.push(line)
});

rl.on('close', function() {
  solve(lines)
})

function solve(lines) {
    let str = lines[0]
    let targetLength = Number(lines[1])
    let padString = lines[2]
    console.log(padEnd(str, targetLength, padString))
}

function padEnd(str, targetLength, padString) {
    if(str.length >= targetLength) {
        return str
    } 

    let result = str
    while(result.length < targetLength){
        result += padString
    }

    let newResult = ''
    for(let i=0; i<targetLength; i++) {
        newResult += result[i]
    }

    return newResult
}

LIOJ 1045:String slice

  • Make an imitation of String slice function

  • 解題想法:跑個迴圈吧

var readline = require('readline');

var lines = []
var rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', function (line) {
  lines.push(line)
});

rl.on('close', function() {
  solve(lines)
})

function solve(lines) {
    console.log(slice(lines[0], Number(lines[1]), Number(lines[2])))
}

function slice(str, beginIndex, endIndex) {
    let result = ''
    for(let i=beginIndex; i<=endIndex - 1; i++) {
        result += str[i]
    }
    return result
}









Related Posts

該如何入門 CTF 中的 Web 題?

該如何入門 CTF 中的 Web 題?

【隨堂筆記】套件模組與環境設定

【隨堂筆記】套件模組與環境設定

程式導師實驗計畫 Week 15 題目

程式導師實驗計畫 Week 15 題目


Comments