Ad

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]

Code
Diff
  • 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 = True
    • for n in range(2,possibleprimes):
    • if possibleprimes % n == 0:
    • isprime = False
    • if 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
Code
Diff
  • 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
Code
Diff
  • 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+=j
    • return code
    • print(morse_code('1122'))
    • return "".join([j for i in msg.replace (" ", "*").lower() for j in morse[i]])
Code
Diff
  • 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()
Code
Diff
  • 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
Code
Diff
  • def add(s, o):
        s = map(int, s)
        s = {
            1: (c for c in s if c % 2),
            2: (c for c in s if not c % 2),
        }.get(o, s);
        return sum(s)
    • def add(s, o):
    • s = map(int, s)
    • s = {
    • 1: (c for c in s if c % 2),
    • 2: (c for c in s if c % 2 == 0),
    • 2: (c for c in s if not c % 2),
    • }.get(o, s);
    • return sum(s)
Code
Diff
  • 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) // 2
    • lefthalf = nums[:mid]
    • righthalf = nums[mid:]
    • self.merge_sort(lefthalf)
    • self.merge_sort(righthalf)
    • i = 0
    • j = 0
    • k = 0
    • while i < len(lefthalf) and j < len(righthalf):
    • if lefthalf[i] < righthalf[j]:
    • nums[k] = lefthalf[i]
    • i += 1
    • else:
    • nums[k] = righthalf[j]
    • j += 1
    • k += 1
    • while 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 += 1
    • while 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
Code
Diff
  • 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 = 0
    • for num in string:
    • num = int(num)
    • all_sum += num
    • if num % 2 == 0:
    • even_list.append(num)
    • if num % 2 != 0:
    • odd_list.append(num)
    • if option == 0:
    • return all_sum
    • if 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
Code
Diff
  • #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
Code
Diff
  • 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 = 0
    • y1 = 0
    • for inner_list in range(len(array)):
    • if "y" in array[inner_list]:
    • y = inner_list
    • if "x" in array[inner_list]:
    • y1 = inner_list
    • x = 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)
Code
Diff
  • def hypotenuse(a,b):
        return round((a**2 + b**2)**0.5,4)
    • def hypotenuse(a,b):
    • import math
    • h = round(math.sqrt(a**2 + b**2),4)
    • #print(h)
    • return h
    • return round((a**2 + b**2)**0.5,4)
Code
Diff
  • 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)]
Code
Diff
  • def getLongestWord(words):
        return '' if len(words) == 0 else max(words, key=len)
        
    • def getLongestWord(words):
    • longest = ""
    • for w in words:
    • longest = w if len(w) > len(longest) else longest
    • return longest
    • return '' if len(words) == 0 else max(words, key=len)