成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

用于解答算法題目的Python3代碼框架

Dr_Noooo / 2919人閱讀

摘要:代碼于是我就利用的代碼片段功能編寫了一個(gè)用于處理這些輸入輸出的代碼框架,并加入了測(cè)試功能寫函數(shù)前先寫測(cè)試時(shí)正確的事情。

前言

最近在實(shí)習(xí),任務(wù)并不是很重,就利用閑暇時(shí)間使用Python3在PAT網(wǎng)站上刷題,并致力于使用Python3的特性和函數(shù)式編程的理念,其中大部分題目都有著類似的輸入輸出格式,例如一行讀入若干個(gè)數(shù)字,字符串,每行輸出多少個(gè)字符串等等,所以產(chǎn)生了很多重復(fù)的代碼。

Python代碼

于是我就利用VS Code的代碼片段功能編寫了一個(gè)用于處理這些輸入輸出的代碼框架,并加入了測(cè)試功能(寫函數(shù)前先寫測(cè)試時(shí)正確的事情)。代碼如下

"""Simple Console Program With Data Input And Output."""
import sys
import io


def read_int():
    """Read a seris of numbers."""
    return list(map(int, sys.stdin.readline().split()))


def test_read_int():
    """Test the read_int function"""
    test_file = io.StringIO("1 2 3
")
    sys.stdin = test_file
    assert read_int() == [1, 2, 3], "read_int error"


def read_float():
    """Read a seris of float numbers."""
    return list(map(float, sys.stdin.readline().split()))


def test_read_float():
    """Test the read_float function"""
    test_file = io.StringIO("1 2 3
")
    sys.stdin = test_file
    assert read_float() == [1.0, 2.0, 3.0], "read_float error"


def read_word():
    """Read a seris of string."""
    return list(map(str, sys.stdin.readline().split()))


def test_read_word():
    """Test the read_word function"""
    test_file = io.StringIO("1 2 3
")
    sys.stdin = test_file
    assert read_word() == ["1", "2", "3"], "read_word error"


def combine_with(seq, sep=" ", num=None):
    """Combine list enum with a character and return the string object"""
    res = sep.join(list(map(str, seq)))
    if num is not None:
        res = str(seq[0])
        for element in range(1, len(seq)):
            res += sep + 
                str(seq[element]) if element % num != 0 else "
" + 
                str(seq[element])
    return res


def test_combile_with():
    """Test the combile_with function."""
    assert combine_with([1, 2, 3, 4, 5], "*", 2) == """1*2
3*4
5""", "combine_with error."


def main():
    """The main function."""
    pass


if __name__ == "__main__":
    sys.exit(int(main() or 0))
VS Code代碼片段

添加到VS Code的默認(rèn)代碼片段的操作大致如下:

文件->首選項(xiàng)->用戶代碼片段,選擇Python

編輯"python.json"文件如以下內(nèi)容

{
/*
     // Place your snippets for Python here. Each snippet is defined under a snippet name and has a prefix, body and 
     // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
     // $1, $2 for tab stops, ${id} and ${id:label} and ${1:label} for variables. Variables with the same id are connected.
     // Example:
     "Print to console": {
        "prefix": "log",
        "body": [
            "console.log("$1");",
            "$2"
        ],
        "description": "Log output to console"
    }
*/
 "Simple Console Program With Data Input And Output": {
        "prefix": "simple",
        "body": [""""Simple Console Program With Data Input And Output."""
import sys

def read_int():
    """Read a seris of numbers."""
    return list(map(int, sys.stdin.readline().split()))


def read_float():
    """Read a seris of float numbers."""
    return list(map(float, sys.stdin.readline().split()))


def read_word():
    """Read a seris of string."""
    return list(map(str, sys.stdin.readline().split()))


def combine_with(seq, sep=" ", num=None):
    """Combine list enum with a character and return the string object"""
    res = sep.join(list(map(str, seq)))
    if num is not None:
        res = str(seq[0])
        for element in range(1, len(seq)):
            res += sep + str(seq[element]) if element % num != 0 else "
" + str(seq[element])
    return res


def main():
    """The main function."""
    pass


if __name__ == "__main__":
    sys.exit(int(main() or 0))
"
        ],
        "description": "Simple Console Program With Data Input And Output"
    }
}```
 然后再編寫Python代碼的時(shí)候,鍵入"simple"就可以自動(dòng)輸入以上模板。

![](https://static.oschina.net/uploads/img/201608/04182804_9GZn.png "在這里輸入圖片標(biāo)題")
# 總結(jié)

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/45510.html

相關(guān)文章

  • 【數(shù)據(jù)結(jié)構(gòu)_浙江大學(xué)MOOC】第二講 線性結(jié)構(gòu)

    摘要:應(yīng)直接使用原序列中的結(jié)點(diǎn),返回歸并后的帶頭結(jié)點(diǎn)的鏈表頭指針。要求分別計(jì)算兩個(gè)多項(xiàng)式的乘積與和,輸出第一項(xiàng)為乘積的系數(shù)和指數(shù),第二行為和的系數(shù)和指數(shù)。選定了表示方法后,考慮數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)。選擇鏈表在設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu)的時(shí)候有系數(shù)指數(shù)和指針結(jié)構(gòu)指針。 函數(shù)題給出編譯器為 C(gcc) 的解答,編程題給出編譯器 C++(g++) 或 Python(python3) 的解答。 函數(shù)題 兩個(gè)有序鏈表序...

    luxixing 評(píng)論0 收藏0
  • JS算法題之leetcode(1~10)

    摘要:先去空白,去掉空白之后取第一個(gè)字符,判斷正負(fù)符號(hào),若是英文直接返回,若數(shù)字則不取?;匚臄?shù)題目描述判斷一個(gè)整數(shù)是否是回文數(shù)?;匚臄?shù)是指正序從左向右和倒序從右向左讀都是一樣的整數(shù)。 JS算法題之leetcode(1~10) 前言 一直以來,前端開發(fā)的知識(shí)儲(chǔ)備在數(shù)據(jù)結(jié)構(gòu)以及算法層面是有所暫缺的,可能歸根于我們的前端開發(fā)的業(yè)務(wù)性質(zhì),但是我認(rèn)為任何的編程崗位都離不開數(shù)據(jù)結(jié)構(gòu)以及算法。因此,我作為...

    SoapEye 評(píng)論0 收藏0
  • 從簡(jiǎn)歷被拒到收割今日頭條 offer,我用一年時(shí)間破繭成蝶!

    摘要:正如我標(biāo)題所說,簡(jiǎn)歷被拒??戳宋液?jiǎn)歷之后說頭條競(jìng)爭(zhēng)激烈,我背景不夠,點(diǎn)到為止。。三準(zhǔn)備面試其實(shí)從三月份投遞簡(jiǎn)歷開始準(zhǔn)備面試到四月份收,也不過個(gè)月的時(shí)間,但這都是建立在我過去一年的積累啊。 本文是 無精瘋 同學(xué)投稿的面試經(jīng)歷 關(guān)注微信公眾號(hào):進(jìn)擊的java程序員K,即可獲取最新BAT面試資料一份 在此感謝 無精瘋 同學(xué)的分享 目錄: 印象中的頭條 面試背景 準(zhǔn)備面試 ...

    tracymac7 評(píng)論0 收藏0
  • 從簡(jiǎn)歷被拒到收割今日頭條 offer,我用一年時(shí)間破繭成蝶!

    摘要:正如我標(biāo)題所說,簡(jiǎn)歷被拒??戳宋液?jiǎn)歷之后說頭條競(jìng)爭(zhēng)激烈,我背景不夠,點(diǎn)到為止。。三準(zhǔn)備面試其實(shí)從三月份投遞簡(jiǎn)歷開始準(zhǔn)備面試到四月份收,也不過個(gè)月的時(shí)間,但這都是建立在我過去一年的積累啊。 本文是 無精瘋 同學(xué)投稿的面試經(jīng)歷 關(guān)注微信公眾號(hào):進(jìn)擊的java程序員K,即可獲取最新BAT面試資料一份 在此感謝 無精瘋 同學(xué)的分享目錄:印象中的頭條面試背景準(zhǔn)備面試頭條一面(Java+項(xiàng)目)頭條...

    wdzgege 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

Dr_Noooo

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<