Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, March 29, 2024

📑 Histograms with Colors by Values

Histograms with Colors by Values




Thursday, January 4, 2024

📑ASCII Art

ASCII Art


Saturday, May 27, 2023

📑 AGG filtering

Anti-Grain Geometry (AGG) & Matplotlib Artists





Sunday, May 21, 2023

📑 Farewell Round A 2023 Task 5 Untie

Farewell Round A 2023. Task 5

Python Only

T=int(input())
for t in range(T):
    C=input()
    if len(set(C))==1:
        count=len(C)//2+len(C)%2
    else:
        while C[0]==C[-1]:
            C=C[1:]+C[0]
        count=C.count('PP')+C.count('SS')+C.count('RR')
    print('Case #{}: {}'.format((t+1),count))
        


📑 Farewell Round A 2023 Task 4 ASCII Art

Farewell Round A 2023. Task 4

Python Only

# Competition Variant 1 (All Points)
letters='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def count26down(number):
    diff1,i,j=number,1,0
    while number>0:
        diff1=number; k1=i
        number-=i*26; i+=1
    while diff1>0:
        diff2=diff1; k2=j
        diff1-=k1; j+=1
    return k2
T=int(input())
for t in range(T):
    N=int(input())
    L=letters[count26down(N)]
    print('Case #{}: {}'.format((t+1),L))
    


Saturday, May 20, 2023

📑 Farewell Round A 2023 Task 3 Rainbow Sort

Farewell Round A 2023. Task 3

Python Only

# Competition Variant 1 (All Points)
T=int(input())
for t in range(T):
    N=int(input())
    cards=input().split()
    cards_set=set()
    result=[]
    for i in range(N):
        if cards[i] not in cards_set:
            cards_set.update([cards[i]])
            result.append(cards[i])
        elif result[-1]!=cards[i]:
            result='IMPOSSIBLE'
            break
 #       print(i,result)
    if result!='IMPOSSIBLE': result=' '.join(result)
    print('Case #{}: {}'.format((t+1),result))
    


Saturday, April 29, 2023

📑 Farewell Round A 2023 Task 2 Illumination Optimization

Farewell Round A 2023. Task 2

Python Only

def update(i,start,end,radius,condition,answer):
    while (i < N):
        if X[i]-start > radius: break
        else: end=X[i]
        i+=1
    if end!=condition: 
        answer+=[end]
        start=end
    else:
        answer=[]
    return i,start,end,answer
T=int(input())
for t in range(T):
    [M,R,N]=list(map(int,input().split()))
    X=list(map(int,input().split()))
    i,start,end,answer=0,0,-1,[]
    i,start,end,answer=update(i,start,end,R,-1,answer)
    while ((i < N) and (answer!=[]) and (end+R < M)):
        i,start,end,answer=update(i,start,end,2*R,start,answer)
    if M-end > R: answer=[]
    if answer!=[]:
        print('Case #{}: {}'.format((t+1),len(answer)))
    else: 
        print('Case #{}: IMPOSSIBLE'.format(t+1))
        


📑 Farewell Round A 2023 Task 1 Colliding Encoding

Farewell Round A 2023. Task 1

Python Only

T=int(input())
letters='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for t in range(T):
    D=input().replace(' ','')
    N=int(input())
    numbers=set()
    words=set()
    for i in range(N):
        w=input()
        words.update([w])
        n=''.join([D[letters.find(s)] for s in w])
        numbers.update([n])
    if len(numbers) < len(words):
        print('Case #{}: YES'.format(t+1))
    else:
        print('Case #{}: NO'.format(t+1))
        


Monday, March 13, 2023

SageMathCell Text Coloring

Text Coloring

Sunday, July 24, 2022

📑 Go, Gopher! Part 2

Codejam Qualification Round 2018. Task 3

Python Only

def go_gopher(a):
    k=a//3+1
    l=k//3
    ra=[3*i+2 for i in list(range(l))]
    if k-1>ra[-1]:
        ra+=[k-1]
    c=1000
    go_ra=[ra[j%len(ra)] for j in range(c)]
    for i in go_ra:
        x,y=[i-1,i,i+1],[1,2,3]
        xy=[' '.join([str(el) for el in [xy1,xy2]]) 
            for xy1 in x for xy2 in y]
        flag=False
        while xy!=[]:
            out_xy='%d 2'%i
            print(out_xy,flush=True)
            in_xy=input()
            if in_xy in xy:
                xy.remove(in_xy)
            elif in_xy=='0 0':
                flag=True
                break
            elif in_xy=='-1 -1':
                exit()                
#            print(xy)
        if flag==True:
            break
T=int(input())
for i in range(T):
    A=int(input())
    go_gopher(A)


📑 Go, Gopher! Part 1

Codejam Qualification Round 2018. Task 3

Process Simulation


📑 Trouble Sort

Codejam Qualification Round 2018. Task 2

Python Only

def trouble_sort(L,N):
    LE,LO=[],[]
    for i in range(N):
        (LO,LE)[i%2==0].append(L[i])
    LE=sorted(LE)
    LO=sorted(LO)
    L=sorted(L)
    for i in range(N):
        if i%2==0:
            el=LE[i//2]
        else:
            el=LO[i//2]
        if el!=L[i]:
            break
    if N-1!=i:
        return str(i)
    else: 
        return 'OK'
T=int(input())
for t in range(T):
    N=int(input())
    L=list(map(int,input().split()))
    ANS=trouble_sort(L,N)
    print('Case #{}: {}'.format(t+1,ANS))


📑 Saving The Universe Again

Codejam Qualification Round 2018. Task 1

Python Only

def damage(SC):
    K,D,N=1,0,len(SC)
    if 'C' not in SC:
        D==N
    for i in range(N):
        if SC[i]=='S':
            D+=K
        else:
            K*=2
    return D
def reduce_damage(SC,D):
    C,N=0,len(SC)      
    if damage(SC) > D:
        while 'CS' in SC:
            idx=SC[::-1].index('SC')
            SC=SC[:N-idx-2]+'SC'+SC[N-idx:]
            C+=1
            if damage(SC) <= D:
                break
    return C,damage(SC)-D
T=int(input())
for t in range(T): 
    [D,SC]=input().split()
    D=int(D)
#    print(D,damage(SC))
#    print(reduce_damage(SC,D))
    C,DIFF=reduce_damage(SC,D)
    if DIFF > 0:
        print('Case #{}: IMPOSSIBLE'.format(t+1))
    else:
        print('Case #{}: {}'.format(t+1,C))


Thursday, April 28, 2022

🏙 Controlled Inflation

Codejam 1B 2022. Thinking about Task 2

🌐   Codejam Task    




Sunday, April 17, 2022

📑 Chain Reactions

Codejam Qualification Round 2022. Task 4

Python Only

class DGraph:
    def __init__(self,n,f,p):
        self.N=n
        self.P=p
        self.F=f
        self.G={k:0 for k in range(n)}
        self.D={k:0 for k in range(n)}
    def add_edges(self):
        for i,el in enumerate(self.P):
            self.G[i]=el-1
            if el!=0:
                self.D[el-1]+=1 
    def max_fun(self):
        indegree=self.D
        inits=[]
        for i in range(self.N):
            if indegree[i]==0:
                inits.append(i)
        fmin=[10**12]*self.N
        fsum=sum([self.F[i] for i in inits])
        while inits:
            i=inits.pop(0)
            node=self.G[i]
            if node!=-1:
                indegree[node]-=1
                fmin[node]=min([fmin[node],self.F[i]])
                if indegree[node]==0:
                    inits.append(node)
                    self.F[node]=max([self.F[node],fmin[node]])
                    fsum+=self.F[node]-fmin[node]
        return fsum
T=int(input())
for t in range(T):
    N=int(input())
    F=list(map(int,input().split()))
    P=list(map(int,input().split()))
    dgraph=DGraph(N,F,P)
    dgraph.add_edges()
    FSUM=dgraph.max_fun()
    print('Case #{}: {}'.format(t+1,FSUM))


Sunday, April 10, 2022

📑 3D Printing 2

Codejam Qualification Round 2022. Task 2 Variant 2

Python Only

def gen_vector(v,s=10**6):
    if sum(v) < s:
        yield 'IMPOSSIBLE'
    else:
        for c in range(v[0],1,-1):
            m=min([v[1],10**6-c])
            y=min([v[2],10**6-c-m])
            k=10**6-c-m-y
            yield '%d %d %d %d'%(c,m,y,k)
T=int(input())
for t in range(T):
    N=[]
    for i in range(3):
        N.append(list(map(int,input().split())))
    v=[min([N[i][j] for i in range(3)]) 
       for j in range(4)]
    print('Case #%d: %s'%(t+1,next(gen_vector(v))))


Saturday, April 9, 2022

📑 Punched Cards 2

Codejam Qualification Round 2022. Task 1 Variant 2

Python Only

def gen_table(r,c):
    for i in range(2*r+1):
        for j in range(2*c+1):
            if (i < 2 and j < 2):
                yield '.'
            else:
                el=((j+1)%2)*((i+1)%2)*'+'+\
                   (j%2)*((i+1)%2)*'-'+\
                   ((j+1)%2)*(i%2)*'|'+\
                   (j%2)*(i%2)*'.'
                yield el
        yield '\n'
T=int(input())
for t in range(T):
    N=input()
    [R,C]=[int(n) for n in N.split()]
    gen=gen_table(R,C)
    print('Case #{}:'.format(t+1))
    print(''.join(gen))


📑 Double or One

Codejam 1A Round 2022. Task 1

Python Only

T=int(input())
for t in range(T):
    S=input()
    NS=''; i=0; temp=''
    while i < len(S)-1:
        if (len(set(S[i:])) == 1) and (len(S[i:]) > 1):
            NS+=S[i:-1]
            break
        else:
            if S[i+1] > S[i]:
                NS+=2*temp+2*S[i]
                temp=''
            elif S[i+1] < S[i]:
                NS+=temp+S[i]
                temp=''
            else:
                temp+=S[i]
        i+=1  
    print('Case #{}: {}'.format(t+1,NS+S[-1]))


def gen_first(S):
    i=0; temp=''
    while i < len(S)-1:
        if (len(set(S[i:])) == 1) and (len(S[i:]) > 1):
            yield S[i:-1]
            break
        else:
            if S[i+1] > S[i]:
                yield 2*temp+2*S[i]
                temp=''
            elif S[i+1] < S[i]:
                yield temp+S[i]
                temp=''
            else:
                temp+=S[i]
        i+=1
    yield S[-1]
T=int(input())
for t in range(T):
    S=input()
    NS=''.join(gen_first(S))
    print('Case #{}: {}'.format(t+1,NS))


Sunday, April 3, 2022

📑 Dice Rows

Codejam Qualification Round 2022. Task 3

Python Only

T=int(input())
for t in range(T):
    N=int(input())
    S=input()
    k=N
    if k>4:
        S=sorted([int(s) for s in S.split()])[::-1]
        k=N=min([N,S[0]])
        for i in range(N):
            if S[i] < N-i:
                k-=1
    print('Case #{}: {}'.format(t+1,k))


📑 3D Printing

Codejam Qualification Round 2022. Task 2

Python Only

T=int(input())
for t in range(T):
    C,M,Y,K=[],[],[],[]
    for i in range(3):
        N=input()
        N=[int(n) for n in N.split()]
        C+=[N[0]]; M+=[N[1]]; Y+=[N[2]]; K+=[N[3]]
    c,m,y,k=min(C),min(M),min(Y),min(K)
    if c+m+y+k<10**6:
        R='IMPOSSIBLE'
    else:
        m=min([m,10**6-c])
        y=min([y,10**6-c-m])
        k=10**6-c-m-y
        R='{} {} {} {}'.format(c,m,y,k)
    print('Case #{}: {}'.format(t+1,R))