Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

python - ACSL Counting Unique Characters

I am trying to solve an ACSL Junior Division practice problem, here is the prompt.

I am stuck on trying to see all the unique characters that have occupied a single index in a list. Here is my code so far.

idk = ["C", "E", "M", "O", "P", "R", "T"]
idk3 = []
idk2 = "computer"
idk2 = idk2.upper()
idk2 = idk2.strip()
for i in idk2:
    if i in idk:
        index = idk.index(i)
        idk.insert(index, i)
    else:
        idk.insert(0, i)
print(len(idk3))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As it is a challenge i tried, but i still don't get the expected results as in the exercice...It will work sometimes and sometimes not... I guess i'm on the right way...

x=0
while x<1: #Test on 1 loop only
    l1 = ["C", "E", "M", "O", "P", "R", "T", "U"]
    count=1 #because of initial list
    new_input=input("Enter 'N String' ?")#Input format N:int+space+string
    up=new_input.upper()#Chars to uppercase
    splited_list=list(up)#String to list of char
    
    #Index N
    index=int(splited_list[0])#store index N

    #Separated letters
    letters=[c for c in splited_list if c.isalpha()]

    for letter in letters:
        if letter in l1:
            if l1.index(letter)<=index:
                count+=1
            l1.insert(l1.index(letter),letter)
        else:
            l1.append(letter)
            l1=sorted(l1)
            if l1.index(letter)<=index:
                count+=1
    
    print(x,"count: "+str(count))   
    x+=1

Exercice input / output :

2 Computer / 1. 3
2 COMPUTER bat / 2. 5
3 COMPUTER / 3. 2
4 ACSL is fun / 4. 3
9 the xylophone / 5. 4

My input / output :

2 Computer / 1.3
2 COMPUTER bat / 2.5
3 COMPUTER / 3.4
4 ACSL is fun / 4.6
9 the xylophone / 5.11

I don't understand as i think my counts are good.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...