daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import *
  2.  
  3. def appartenance_it(l,n):
  4.     for i in range (len(l)):
  5.         if l[i]==n:
  6.             return True
  7.     return False
  8.  
  9. def appartenance_rec(l,n):
  10.     m=l[:]
  11.     if len(m)==1 and m[0] != n:
  12.         return False
  13.     if m[0]!=n:
  14.         del m[0]
  15.         return appartenance_rec(m,n)
  16.    
  17.     if m[0]== n :
  18.         return True
  19.  
  20. def rech_dichotomie_rec(l,n):
  21.     m=len(l)//2
  22.     p=l[:]
  23.     if len(p)==1 and p[0]!= n:
  24.         return False
  25.     elif p[m]==n:
  26.         return True
  27.     elif p[m]>n:
  28.         return rech_dichotomie_rec(p[0:m],n)
  29.     elif p[m]<n:
  30.         return rech_dichotomie_rec(p[m:len(p)],n)
  31.        
  32.  
  33. def rech_dichotomie_iter(l,n):
  34.    
  35.     p=l[:]
  36.    
  37.     while len(p)>=1:
  38.        
  39.         m=len(p)//2
  40.        
  41.         if p[m]==n:
  42.             return True
  43.         if len(p)==1 and p[0]!=n:
  44.             return False
  45.        
  46.         elif p[m]>n:
  47.             p=p[0:m]
  48.         elif p[m]<n:
  49.             p=p[m:len(p)]
  50.  
  51.  
  52. def retirer_doublon_iter(l):
  53.     test=True
  54.    
  55.     for i in range(len(l)):
  56.         t=l[i]
  57.         p=l[:]
  58.         del p[i]
  59.         while test==True:
  60.            
  61.             if appartenance_it(p,t)==True:
  62.                 l=0
  63.        
  64.     return False
  65.  
  66.  
  67.  
  68.    
  69. ###########################################################################################
  70.  
  71. l=[0]*30
  72. for i in range(30):
  73.     l[i]=randrange(50)
  74. a=randrange(30)
  75.  
  76. print(appartenance_rec(l,l[a]))
  77. print(appartenance_it(l,l[a]))
  78. p=l[:]
  79. p.sort()
  80. print(rech_dichotomie_rec(p,p[a]))
  81. print(rech_dichotomie_iter(p,p[a]))
  82.  
  83. #Test censĂ© ĂȘtre positif
  84.  
  85. print(appartenance_rec(l,51))
  86. print(appartenance_it(l,51))
  87. print(rech_dichotomie_rec(p,51))
  88. print(rech_dichotomie_iter(p,51))
  89.  
  90. #False
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top