fork download
  1. #PARA GRAFOS
  2.  
  3. def esCamino(GMA,cam):
  4. if len(cam)<2:return True
  5. if not GMA[cam[0]][cam[1]]:return False
  6. return esCamino(GMA,cam[1:])
  7.  
  8. #Matrices
  9.  
  10. def prodpunto(v,w):
  11. if not v:return 0
  12. return v[0]*w[0]+prodpunto(v[1:],w[1:])
  13.  
  14. def esSubCadena(LL,L):
  15. if not L:return True
  16. if not LL:return False
  17. if LL[0]==L[0]:return esSubCadena(LL[1:],L[1:])
  18. return esSubCadena(LL[1:],L)
  19.  
  20. def combN(L, n):
  21. if len(L) == n:
  22. return [L]
  23. if not n:
  24. return [[]]
  25. c = []
  26. for i in combN(L[1:], n - 1): # COMBINACIONES DE UNA LISTA DE TAMAÑO N
  27. c.append([L[0]] + i)
  28. c += combN(L[1:], n)
  29. return c
  30.  
  31. def comb(L):
  32. if len(L) <= 1:return [L]
  33. pers = []
  34. for i in range(len(L)): # COMBINACIONES DE UNA LISTA
  35. p = L[i]
  36. p2 = L[:i] + L[i+1:]
  37. for k in comb(p2):
  38. pers.append([p] + k)
  39. return pers
  40.  
  41.  
  42. def mediana(lista):
  43. lista = quick_sort(lista)
  44. n = len(lista)
  45. if n % 2 == 0: return (lista[n // 2 - 1] + lista[n // 2]) / 2
  46. return lista[n // 2]
  47.  
  48.  
  49. def moda(lista):
  50. if len(lista) == 1:return lista[0]
  51. lista = quick_sort(lista)
  52. res, actual = lista[0], lista[0]
  53. maximo,contador = 1, 1
  54. for i in range(1, len(lista)):
  55. if lista[i] == actual:
  56. contador += 1
  57. else:
  58. if contador > maximo:
  59. res = actual
  60. maximo = contador
  61. actual = lista[i]
  62. contador = 1
  63. return res
  64.  
  65.  
  66. #CODIGOS OG
  67. def es_pot2(n):
  68. return n == (n & (-n))
  69.  
  70.  
  71. # Ejemplos de uso:
  72. print(es_pot2(4)) # True
  73. print(es_pot2(5)) # False
  74. print(es_pot2(16)) # True
  75. print(es_pot2(18)) # False
  76.  
  77.  
  78. def esPrimo(n):
  79. if n==2:return True
  80. if not n%2:return False
  81. i=3
  82. while i*i <= n:
  83. if not n%i: return False
  84. i +=2
  85. return True
  86.  
  87. def RaizEntera(n):
  88. return RE(1,n,n)
  89.  
  90. def RE(inf,sup,n):
  91. if inf+1 == sup:return inf
  92. m= inf+sup //2
  93. if m*m< n:return RE(inf,m,n)
  94. return RE(m,sup,n)
  95.  
  96.  
  97.  
  98.  
  99.  
Success #stdin #stdout 0.05s 9644KB
stdin
Standard input is empty
stdout
True
False
True
False