第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

創(chuàng)建一個(gè)將數(shù)字轉(zhuǎn)換為字母的函數(shù)

創(chuàng)建一個(gè)將數(shù)字轉(zhuǎn)換為字母的函數(shù)

湖上湖 2023-12-29 14:40:29
我編寫了這個(gè)函數(shù),它應(yīng)該通過(guò)用戶提供的字符串(如 )1-3-5,并輸出相應(yīng)的一系列字母,其中 A 分配給 1,B 分配給 2,C 分配給 3 等。輸出的情況1-3-5是ACE. 對(duì)于2-3-4,它應(yīng)該打印BCD。對(duì)于?-3-4或者--3-4它仍然應(yīng)該打印BCD.這是我到目前為止編寫的代碼:def number_to_letter(encoded):    result = ""    start = 0    for char in range(len(encoded)):        if encoded[char] == '-':            i = encoded.index("-")            sub_str = encoded[start:i]            if not sub_str.isdigit():                result += ""            else:                letter = chr(64 + int(sub_str))                if 0 < int(sub_str) < 27:                    result += letter                else:                    result += ""            start += len(sub_str) + 1    return resultprint(num_to_let('4-3-25'))我的輸出是D,當(dāng)它應(yīng)該是的時(shí)候DCY。我試圖在不使用任何列表或使用該split函數(shù)的情況下執(zhí)行此操作,只需查找-子字符串中的字符并將其前面的數(shù)字轉(zhuǎn)換為字母即可。我能做些什么?
查看完整描述

4 回答

?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

你可以嘗試做這樣的事情:


def number_to_letter(encoded):

    result  = ""

    buffer = ""

    for ch in encoded:

        if ch == '-':

            if buffer and 0 < int(buffer) < 27:

                result += chr(64 + int(buffer))

            buffer = ""

        elif ch.isdigit():

            buffer += ch

    else:

        if buffer and 0 < int(buffer) < 27:

            result += chr(64 + int(buffer))

        return result 


print(number_to_letter('1-3-5'))

輸出:


ACE

解釋:


我們循環(huán)每個(gè)字符并將其添加到某個(gè)緩沖區(qū)中。當(dāng)我們遇到-(分隔符)時(shí),我們嘗試解析緩沖區(qū)并重置它。最后我們?cè)龠M(jìn)行一次相同的解析并返回結(jié)果。


驗(yàn)證的工作方式是,每當(dāng)我們填充緩沖區(qū)時(shí),我們都會(huì)檢查數(shù)字有效性(使用.isdigit()),并且當(dāng)我們解析緩沖區(qū)時(shí),我們會(huì)檢查范圍約束。


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

沒(méi)有清單,好吧。但是聽(tīng)寫呢?


def abc(nums):

    d = {'-':'','1':'A','2':'B','3':'C','4':'D','5':'E','6':'F','7':'G','8':'H','9':'I','0':'J'}

    res = ''

    for n in nums: res += d[n]

    return res

    

print(abc('1-2-3-9-0')) # Output: ABCIJ

這是一個(gè)更正的版本:


def abc(nums):

    d = {'-':'','1':'A','2':'B','3':'C','4':'D','5':'E','6':'F','7':'G','8':'H','9':'I','0':'J'}

    res = ''

    for n in nums:

        if n in d:

            res += d[n]

    return res


print(abc('?-2-3-9-0')) # Output: BCIJ


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

這段代碼的方法是找到第一個(gè)“-”,然后將其存儲(chǔ)在哪里,這樣下次我們就可以在最后一個(gè)“-”之后查找第一個(gè)“-”


當(dāng)我的代碼中的注釋談?wù)撗h(huán)時(shí)意味著要經(jīng)歷一次循環(huán)(循環(huán)時(shí):)


def number_to_letter(encoded):

    letterString = ""

    startSubStr = 0

    endSubStr = 0

    looping = True


    while looping:

        if endSubStr > (len(encoded)-4):# if we're at the last number we don't look for '-'. we go to the end of the str and end the loop

            endSubStr = len(encoded)

            looping = False

        else:

            endSubStr = encoded.index('-', startSubStr) #find the first '-' after the '-' found in the last cycle


        number = int(encoded[startSubStr:endSubStr]) #get the number between the '-' found in the last cycle through this loop and the '-' found in this one

        if number < 27:

            letter = chr(64 + int(number))

            letterString += letter


        startSubStr = endSubStr + 1 #set the start of the substring to the end so the index function doesn't find the '-' found in this cycle again 


    return letterString


print(number_to_letter("23-1-1-2")) #>>> WAAB

結(jié)果:WAAB


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

import string


alphabet = list(string.ascii_lowercase)

combination = "1-2-3"


def seperate(s, sep='-'):

    return [s[:s.index(sep)]] + seperate(s[s.index(sep)+1:]) if sep in s else [s]


combination = seperate(combination)


print("".join([alphabet[int(i)-1] for i in combination]))


查看完整回答
反對(duì) 回復(fù) 2023-12-29
  • 4 回答
  • 0 關(guān)注
  • 243 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)