Ad
Code
Diff
  • from operator import mul as multiple
    • def multiple(a,b):
    • return a*b
    • from operator import mul as multiple
Code
Diff
  • from operator import add
    • def add(a,b):
    • return a+b
    • from operator import add
Code
Diff
  • def getLongestWord(words):
        return max(words, key=len, default="")
    • def getLongestWord(words):
    • longest = ""
    • for w in words:
    • longest = w if len(w) > len(longest) else longest
    • return longest
    • return max(words, key=len, default="")
Code
Diff
  • def square(n):
        return ['*' * n] * n
    • def square(n):
    • return ['*' * n for x in range(0,n)]
    • return ['*' * n] * n
Code
Diff
  • package main
    
    import "fmt"
    
    
    
    func bubble_sort(dataset []int, amout_of_integers int){
      for i := 0; i <= amout_of_integers; i++ {
        for j := amout_of_integers; j >= i + 1; j-- {
          if dataset[j] < dataset[j-1] {
            dataset[j], dataset[j-1] = dataset[j-1], dataset[j] 
          }
        }
      }
    }
    
    
    func main(){
    
      dataset := []int{5, 2, 4, 6, 1, 3};
    
      fmt.Println(dataset)
    
      bubble_sort(dataset, 5);
    
      fmt.Println(dataset)
    }
    
    • package main
    • import "fmt"
    • func swap(dataset []int, a, b int) {
    • var x int = dataset[a]
    • dataset[a] = dataset[b]
    • dataset[b] = x
    • }
    • func bubble_sort(dataset []int, amout_of_integers int){
    • for i := 0; i <= amout_of_integers; i++ {
    • for j := amout_of_integers; j >= i + 1; j-- {
    • if dataset[j] < dataset[j-1] {
    • swap(dataset, j, j - 1)
    • dataset[j], dataset[j-1] = dataset[j-1], dataset[j]
    • }
    • }
    • }
    • }
    • func main(){
    • dataset := []int{5, 2, 4, 6, 1, 3};
    • fmt.Println(dataset)
    • bubble_sort(dataset, 5);
    • fmt.Println(dataset)
    • }
Code
Diff
  • from math import sqrt
    
    def distance2D(pA, pB):
        if pA == pB: return 0
        (xA, yA), (xB, yB) = pA, pB
        return sqrt((xA - xB)**2 + (yA - yB)**2)
        
    def distance3D(pA, pB):
        if pA == pB: return 0
        (xA, yA, zA), (xB, yB, zB) = pA, pB
        return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)
    • from math import sqrt
    • def distance2D(pA, pB):
    • if pA == pB: return 0
    • xA, yA = tuple(pA); xB, yB = tuple(pB)
    • (xA, yA), (xB, yB) = pA, pB
    • return sqrt((xA - xB)**2 + (yA - yB)**2)
    • def distance3D(pA, pB):
    • if pA == pB: return 0
    • xA, yA, zA = tuple(pA); xB, yB, zB = tuple(pB)
    • (xA, yA, zA), (xB, yB, zB) = pA, pB
    • return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)
Strings
Data Types
Code
Diff
  • from operator import itemgetter
    
    last_char = itemgetter(-1)
    • last_char = lambda s: s[-1]
    • from operator import itemgetter
    • last_char = itemgetter(-1)
Code
Diff
  • from functools import lru_cache
    
    @lru_cache()
    def fib(n):
        if n < 2: return 1
        return fib(n-1) + fib(n-2)
        
    • from functools import lru_cache
    • @lru_cache()
    • def fib(n):
    • memo = {0:1, 1:1}
    • def recursiveFib(n):
    • if n not in memo:
    • memo[n] = recursiveFib(n-1)+recursiveFib(n-2)
    • return memo[n]
    • return recursiveFib(n)
    • if n < 2: return 1
    • return fib(n-1) + fib(n-2)