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

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

如何遍歷字符串 n 次直到它等于原始字符串?

如何遍歷字符串 n 次直到它等于原始字符串?

慕尼黑8549860 2021-09-28 13:50:44
我有幾個(gè)字符串需要循環(huán)并執(zhí)行一些操作,以便在某些迭代后返回原始字符串。例如在 string "1010",在每次迭代中,我需要根據(jù)迭代次數(shù)移動(dòng)前面的字符First iteration (1 iteration):  0101   (moved 1 character from front)Second iteration (2 iteration): 0101   (moved 2 characters from front)Third iteration (3 iteration):  1010   (moved 3 characters from front)所以我找回了原來(lái)的字符串但是對(duì)于像 那樣的字符串"1001",它將需要 7 次迭代First iteration (1 iteration): 0011   (moved 1 character from front)Second iteration (2 iteration): 1100   (moved 2 characters from front)Third iteration (3 iteration): 0110   (moved 3 characters from front)Fourth iteration (4 iteration): 0110   (moved 4 characters from front)Fifth iteration (5 iteration): 1100   (moved 5 characters from front)Sixth iteration (6 iteration): 0011   (moved 6 characters from front)Seventh iteration (7 iteration): 1001   (moved 7 characters from front)下面是我的代碼string_list = ["1010", "1001"]for i in string_list:    print("i",i)    t = 1    print("starting t",t)    new_string = i    for j in i:        num_letter = new_string[:t]        print("num_letter", num_letter)        new_string = new_string[t:] + num_letter        print("intermediate new string",new_string)        if new_string == i:            print("no of iterations until same string occurs", t)            break        else:            t += 1            print("t",t)對(duì)于第一個(gè)字符串,我沒有得到3正確的迭代。但是對(duì)于第二個(gè)字符串,它在第五次迭代時(shí)停止,因?yàn)樽址拈L(zhǎng)度被完全覆蓋。我如何確保它一直循環(huán)遍歷字符串,直到我得到一個(gè)與原始字符串相同的字符串?
查看完整描述

1 回答

  • 1 回答
  • 0 關(guān)注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報(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)