The algorithm works considering that every prime number is found with the formula 6k-1 and / or 6k + 1.
But not every number obtained with the formula is a prime number. To validate, 6k-1 and / or 6k + 1 are evaluated with the previous prime numbers.
Considering as predefined prime numbers to [2,3]
def primemaker(n): Prime,p1,p2,k =[2,3],0,0,1 if n <= 0: return [] elif n <= 2: return Prime[:n] while p1<=n or p2<=n: p1, p2 = 6*k - 1, 6*k + 1 if isprime(p1,Prime) and p1<=n : Prime.append(p1) if isprime(p2,Prime) and p2<=n : Prime.append(p2) k = k + 1 return Prime def isprime(number,Prime): for i in range(0,len(Prime),1) : if not(number % Prime[i]): return False return True
def primemaker(x):primes = []if x < 2 : return []else:for possibleprimes in range(2,(x+1)):isprime = Truefor n in range(2,possibleprimes):if possibleprimes % n == 0:isprime = Falseif isprime:primes.append(possibleprimes)return primes- def primemaker(n):
- Prime,p1,p2,k =[2,3],0,0,1
- if n <= 0: return []
- elif n <= 2: return Prime[:n]
- while p1<=n or p2<=n:
- p1, p2 = 6*k - 1, 6*k + 1
- if isprime(p1,Prime) and p1<=n : Prime.append(p1)
- if isprime(p2,Prime) and p2<=n : Prime.append(p2)
- k = k + 1
- return Prime
- def isprime(number,Prime):
- for i in range(0,len(Prime),1) :
- if not(number % Prime[i]):
- return False
- return True
# TODO: Replace examples and use TDD development by writing your own tests # These are some of the methods available: # test.expect(boolean, [optional] message) # test.assert_equals(actual, expected, [optional] message) # test.assert_not_equals(actual, expected, [optional] message) # You can use Test.describe and Test.it to write BDD style test groupings test.assert_equals(primemaker(100),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]) test.assert_equals(primemaker(20),[2,3,5,7,11,13,17,19]) test.assert_equals(primemaker(50),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]) test.assert_equals(primemaker(0),[]) test.assert_equals(primemaker(1),[2]) test.assert_equals(primemaker(-1),[]) test.assert_equals(primemaker(500),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499])
- # TODO: Replace examples and use TDD development by writing your own tests
- # These are some of the methods available:
- # test.expect(boolean, [optional] message)
- # test.assert_equals(actual, expected, [optional] message)
- # test.assert_not_equals(actual, expected, [optional] message)
- # You can use Test.describe and Test.it to write BDD style test groupings
- test.assert_equals(primemaker(100),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97])
- test.assert_equals(primemaker(20),[2,3,5,7,11,13,17,19])
- test.assert_equals(primemaker(50),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47])
- test.assert_equals(primemaker(0),[])
- test.assert_equals(primemaker(1),[2])
- test.assert_equals(primemaker(-1),[])
- test.assert_equals(primemaker(500),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499])
import re def AmIYelling(input_sentence): return True if(len(re.sub('[^a-z]','',input_sentence))==0) else False
- import re
- def AmIYelling(input_sentence):
return len([l for l in input_sentence if ((l.isalpha() and l.isupper())or not l.isalpha())]) == len(input_sentence)- return True if(len(re.sub('[^a-z]','',input_sentence))==0) else False
dot,dash='.','-' morse = {'a':[dot,dash],'b':[dash,dot,dot,dot],'c':[dash,dot,dash,dot],'d':[dash,dot,dot],'e':[dot],'f':[dot,dot,dash,dot],'g':[dash,dash,dot],'h':[dot,dot,dot,dot],"" 'i':[dot,dot],'j':[dot,dash,dash,dash],'k':[dash,dot,dash],'l':[dot,dash,dot,dot],'m':[dash,dash],'n':[dash,dot],'o':[dash,dash,dash],'p':[dot,dash,dash],'q':[dash,dash,dot,dash],"" 'r':[dot,dash,dot],'s':[dot,dot,dot],'t':[dash],'u':[dot,dot,dash],'v':[dot,dot,dot,dash],'w':[dot,dash,dash],'x':[dash,dot,dot,dash],'y':[dash,dot,dash,dash],'z':[dash,dot,dot],"" '0':[dash,dash,dash,dash,dash],'1':[dot,dash,dash,dash,dash],'2':[dot,dot,dash,dash,dash],'3':[dot,dot,dot,dash,dash],'4':[dot,dot,dot,dot,dash],'5':[dot,dot,dot,dot,dot],"" '6':[dash,dot,dot,dot,dot],'7':[dash,dash,dot,dot,dot],'8':[dash,dash,dash,dot,dot],'9':[dash,dash,dash,dash,dot],'*':' '} def morse_code(msg): return "".join([j for i in msg.replace (" ", "*").lower() for j in morse[i]])
dot='.'dash='-'- dot,dash='.','-'
- morse = {'a':[dot,dash],'b':[dash,dot,dot,dot],'c':[dash,dot,dash,dot],'d':[dash,dot,dot],'e':[dot],'f':[dot,dot,dash,dot],'g':[dash,dash,dot],'h':[dot,dot,dot,dot],""
- 'i':[dot,dot],'j':[dot,dash,dash,dash],'k':[dash,dot,dash],'l':[dot,dash,dot,dot],'m':[dash,dash],'n':[dash,dot],'o':[dash,dash,dash],'p':[dot,dash,dash],'q':[dash,dash,dot,dash],""
- 'r':[dot,dash,dot],'s':[dot,dot,dot],'t':[dash],'u':[dot,dot,dash],'v':[dot,dot,dot,dash],'w':[dot,dash,dash],'x':[dash,dot,dot,dash],'y':[dash,dot,dash,dash],'z':[dash,dot,dot],""
- '0':[dash,dash,dash,dash,dash],'1':[dot,dash,dash,dash,dash],'2':[dot,dot,dash,dash,dash],'3':[dot,dot,dot,dash,dash],'4':[dot,dot,dot,dot,dash],'5':[dot,dot,dot,dot,dot],""
'6':[dash,dot,dot,dot,dot],'7':[dash,dash,dot,dot,dot],'8':[dash,dash,dash,dot,dot],'9':[dash,dash,dash,dash,dot]}- '6':[dash,dot,dot,dot,dot],'7':[dash,dash,dot,dot,dot],'8':[dash,dash,dash,dot,dot],'9':[dash,dash,dash,dash,dot],'*':' '}
- def morse_code(msg):
code=''for i in msg:if i==' ':code+=' 'else:for j in morse[i.lower()]:code+=jreturn codeprint(morse_code('1122'))- return "".join([j for i in msg.replace (" ", "*").lower() for j in morse[i]])
def AmIYelling(input_sentence): return input_sentence.isupper() or input_sentence.isdigit()
- def AmIYelling(input_sentence):
return len([l for l in input_sentence if ((l.isalpha() and l.isupper())or not l.isalpha())]) == len(input_sentence)- return input_sentence.isupper() or input_sentence.isdigit()
def divisors(n): a,x = [],0 for i in range(1,int(n**0.5) + 1): if not(n%i): a.insert(x,i) x+=1 a.insert(x,n//i) return a
from math import sqrt- def divisors(n):
fa={i for i in range(1,int(sqrt(n)) + 1) if not(n%i)}ct={n//i for i in fa}return sorted(fa|ct)- a,x = [],0
- for i in range(1,int(n**0.5) + 1):
- if not(n%i):
- a.insert(x,i)
- x+=1
- a.insert(x,n//i)
- return a
class Sort: def merge_sort(self, nums): if nums == None: return None elif len(nums) <= 1: return nums vLeft = self.merge_sort(nums[:len(nums) // 2]) vRight= self.merge_sort(nums[len(nums) // 2:]) lLeft,lRight = len(vLeft),len(vRight) i,j,k = 0,0,0 while i < lLeft and j < lRight: if vLeft[i] < vRight[j]: nums[k] = vLeft[i] i += 1 else: nums[k] = vRight[j] j += 1 k += 1 while i < lLeft: nums[k] = vLeft[i] i += 1 k += 1 while j < lRight: nums[k] = vRight[j] j += 1 k += 1 return nums
- class Sort:
- def merge_sort(self, nums):
- if nums == None: return None
if len(nums) > 1:mid = len(nums) // 2lefthalf = nums[:mid]righthalf = nums[mid:]self.merge_sort(lefthalf)self.merge_sort(righthalf)i = 0j = 0k = 0while i < len(lefthalf) and j < len(righthalf):if lefthalf[i] < righthalf[j]:nums[k] = lefthalf[i]i += 1else:nums[k] = righthalf[j]j += 1k += 1while i < len(lefthalf):nums[k] = lefthalf[i]- elif len(nums) <= 1: return nums
- vLeft = self.merge_sort(nums[:len(nums) // 2])
- vRight= self.merge_sort(nums[len(nums) // 2:])
- lLeft,lRight = len(vLeft),len(vRight)
- i,j,k = 0,0,0
- while i < lLeft and j < lRight:
- if vLeft[i] < vRight[j]:
- nums[k] = vLeft[i]
- i += 1
k += 1while j < len(righthalf):nums[k] = righthalf[j]- else:
- nums[k] = vRight[j]
- j += 1
k += 1- k += 1
- while i < lLeft:
- nums[k] = vLeft[i]
- i += 1
- k += 1
- while j < lRight:
- nums[k] = vRight[j]
- j += 1
- k += 1
- return nums
def add(s, o): if o == 0: return sum(int(c) for c in s) elif o == 1: return sum(int(c) for c in s if int(c)%2) elif o == 2: return sum(int(c) for c in s if not(int(c)%2)) else: return 0
def add(string, option):even_list = []odd_list = []all_sum = 0for num in string:num = int(num)all_sum += numif num % 2 == 0:even_list.append(num)if num % 2 != 0:odd_list.append(num)if option == 0:return all_sumif option == 1:return sum(odd_list)if option == 2:return sum(even_list)- def add(s, o):
- if o == 0: return sum(int(c) for c in s)
- elif o == 1: return sum(int(c) for c in s if int(c)%2)
- elif o == 2: return sum(int(c) for c in s if not(int(c)%2))
- else: return 0
test.assert_equals(add("1234567" , 0),28) test.assert_equals(add("1234567" , 1),16) test.assert_equals(add("1234567" , 2),12) test.assert_equals(add("0" , 2),0) test.assert_equals(add("0" , 1),0) test.assert_equals(add("0" , 0),0) test.assert_equals(add("0" , 3),0) test.assert_equals(add("23232323" , 2),8) test.assert_equals(add("23232323" , 1),12)
- test.assert_equals(add("1234567" , 0),28)
- test.assert_equals(add("1234567" , 1),16)
- test.assert_equals(add("1234567" , 2),12)
- test.assert_equals(add("0" , 2),0)
- test.assert_equals(add("0" , 1),0)
- test.assert_equals(add("0" , 0),0)
- test.assert_equals(add("0" , 3),0)
- test.assert_equals(add("23232323" , 2),8)
- test.assert_equals(add("23232323" , 1),12)
#ORDEN O(1) SIN FOR, #Usado en Katas Anteriores por otro colaborador solo #lo aplique para Python. def euler(n): return getMult(3,n-1) + getMult(5,n-1) - getMult(15,n-1) def getMult(multiple, n): return getArith(multiple, n - n%multiple, int(n/multiple)) def getArith(first,last,count): return count * (first + last)/2
def euler(num):return sum(range(3,num,3)) + sum(range(5,num,5)) - sum(range(15,num,15))- #ORDEN O(1) SIN FOR, #Usado en Katas Anteriores por otro colaborador solo
- #lo aplique para Python.
- def euler(n): return getMult(3,n-1) + getMult(5,n-1) - getMult(15,n-1)
- def getMult(multiple, n): return getArith(multiple, n - n%multiple, int(n/multiple))
- def getArith(first,last,count): return count * (first + last)/2
def distance(array): for i in range(len(array)): if "y" in array[i] : y = abs(array[i].index("y") - i) if "x" in array[i] : x = abs(array[i].index("x") - i) return (x+y)
- def distance(array):
y = 0y1 = 0for inner_list in range(len(array)):if "y" in array[inner_list]:y = inner_listif "x" in array[inner_list]:y1 = inner_listx = array[y].index("y")x1 = array[y1].index("x")dist = abs(x -x1) + abs(y-y1)return dist- for i in range(len(array)):
- if "y" in array[i] : y = abs(array[i].index("y") - i)
- if "x" in array[i] : x = abs(array[i].index("x") - i)
- return (x+y)
def fizz_buzz(n): return ['FizzBuzz' if (i % 15 == 0) else 'Fizz' if(i % 3 == 0) else 'Buzz' if(i % 5 == 0) else i for i in range(1,n)]
- def fizz_buzz(n):
for i in range(1, n):yield "Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0) or i- return ['FizzBuzz' if (i % 15 == 0) else 'Fizz' if(i % 3 == 0) else 'Buzz' if(i % 5 == 0) else i for i in range(1,n)]