motifs2.py

Created by schraf

Created on October 15, 2021

1016 Bytes

Utilisation d’une fonction de hachage simple : somme des couleurs


from kandinsky import *
from random import *

ACTG = [0,(33,253,1),(246,90,100),(254,180,50)] # L'ADN est fait de 4 composants (ACTG) donc 4 couleurs
T=5 # taille de la sequence cherchee

def rect(x,y,l,h,c):
  for i in range(l*h):
    set_pixel(x+i%l,y+i//l,c)

def brin():
  for i in range(80*50):
    rect(4*(i%80),20+4*(i//80),4,4,choice(ACTG))

def aff(m):
  draw_string(str(m),0,0)
  rect(4*(m%80),16+4*(m//80),4*T,4,(255,255,255))
  rect(4*(m%80),24+4*(m//80),4*T,4,(255,255,255))

def pixel(i):
  return get_pixel(4*(i%80),20+4*(i//80))


def hachage(seq):
  m=0
  empreinte = sum(seq)
  actuel = sum([pixel(j) for j in range(T)])
  while m<=80*50-T:
    if actuel == empreinte:
      if all(pixel(m+j)==seq[j] for j in range(T)):
        aff(m)
        return True
    actuel = actuel - pixel(m) + pixel(m+T)  
    m+=1
  draw_string("ABS",0,0)  
  return False
  
def go():
  seq=[choice(ACTG) for i in range(T)]
  for i,c in enumerate(seq):
    rect(160+8*i,5,8,8,c)
  brin()
  hachage(seq)