Ad
#include <ctype.h>
#include <string.h>

void drop_caps(char *dest, const char *src) {
  unsigned in_word = 0;
	for (unsigned i = 0; i < strlen(src) + 1000; i++, src++, dest++) {
		if (*src == ' ' || (!in_word && strcspn(src, " ") <= 2)) in_word = 0, *dest = *src;
		else if (in_word) *dest = tolower(*src);
		else in_word = 1, *dest = toupper(*src);
	}
	*dest = '\0';
}
Fundamentals
Code
Diff
  • numberprint=lambda x:int("".join(map(str,[*range(1,x),*range(x,0,-1)])))
    • numberprint=lambda x:int(''.join(map(str,list(range(1,x+1))+list(range(x-1,0,-1)))))
    • numberprint=lambda x:int("".join(map(str,[*range(1,x),*range(x,0,-1)])))
Code
Diff
  • def regex(s):
        import re
        return re.sub(r"[0-9]", "", s)
    
    def regex_plus(s):
        import re
        return re.sub(r"[0-9]+", "", s)
    
    def isnumeric(s):
        from itertools import filterfalse
        return "".join(filterfalse(str.isnumeric, s))
    
    def filter_digits(x):
        from itertools import filterfalse
        return "".join(filterfalse("0123456789".__contains__, x))
    
    def translate(s):
        return s.translate(str.maketrans("", "", "0123456789"))
    • from itertools import filterfalse
    • digits = "0123456789".__contains__
    • def regex(s):
    • import re
    • return re.sub(r"[0-9]", "", s)
    • remove_numbers=lambda x:''.join(filterfalse(digits, x))
    • def regex_plus(s):
    • import re
    • return re.sub(r"[0-9]+", "", s)
    • def isnumeric(s):
    • from itertools import filterfalse
    • return "".join(filterfalse(str.isnumeric, s))
    • def filter_digits(x):
    • from itertools import filterfalse
    • return "".join(filterfalse("0123456789".__contains__, x))
    • def translate(s):
    • return s.translate(str.maketrans("", "", "0123456789"))
Code
Diff
  • """
    import cmath as c;f=lambda a,b=None:c.pi*a if b==None else getattr(c,"a"*(b<0)+["sin","cos","tan"][abs(b)%4-1]+"h"*(abs(b)>4))(a)
    import cmath as c;f=lambda a,b=None:getattr(c,"a"*(b<0)+["sin","cos","tan"][abs(b)%4-1]+"h"*(abs(b)>4))(a)if b!=None else c.pi*a
    import cmath as c;f=lambda a,b="":getattr(c,"a"*(b<0)+["sin","cos","tan"][abs(b)%4-1]+"h"*(abs(b)>4))(a)if b!=""else c.pi*a
    import cmath as c;f=lambda a,b="":getattr(c,"a"[b:]+["sin","cos","tan"][abs(b)%4-1]+"h"*(abs(b)>4))(a)if b!=""else c.pi*a
    import cmath as c;f=lambda a,b="":getattr(c,"a"[b:]+["sin","cos","tan"][abs(b)%4-1]+"h"[-4<b<4:])(a)if b!=""else c.pi*a
    """
    
    from cmath import*;f=lambda a,b=0:globals()["a"[b:]+"sctioansn"[abs(b)%4-1::3]+"h"[-4<b<4:]](a)if b else pi*a
    • """
    • import cmath as c;f=lambda a,b=None:c.pi*a if b==None else getattr(c,"a"*(b<0)+["sin","cos","tan"][abs(b)%4-1]+"h"*(abs(b)>4))(a)
    • import cmath as c;f=lambda a,b=None:getattr(c,"a"*(b<0)+["sin","cos","tan"][abs(b)%4-1]+"h"*(abs(b)>4))(a)if b!=None else c.pi*a
    • import cmath as c;f=lambda a,b="":getattr(c,"a"*(b<0)+["sin","cos","tan"][abs(b)%4-1]+"h"*(abs(b)>4))(a)if b!=""else c.pi*a
    • import cmath as c;f=lambda a,b="":getattr(c,"a"[b:]+["sin","cos","tan"][abs(b)%4-1]+"h"*(abs(b)>4))(a)if b!=""else c.pi*a
    • """
    • import cmath as c;f=lambda a,b="":getattr(c,"a"[b:]+["sin","cos","tan"][abs(b)%4-1]+"h"[-4<b<4:])(a)if b!=""else c.pi*a
    • """
    • from cmath import*;f=lambda a,b=0:globals()["a"[b:]+"sctioansn"[abs(b)%4-1::3]+"h"[-4<b<4:]](a)if b else pi*a
Dictionary
Data Structures
Code
Diff
  • def nested_dic(*args):
        a = list(args)
        r = a.pop()
        for x in reversed(a):
            r = [{y: z} for y, z in zip(x, r)]
        return r
    • def nested_dic(*args, i0 = None, i1 = None):
    • if i0 != None:
    • if i0 < len(args) - 1:
    • return {args[i0][i1]: nested_dic(*args, i0 = i0 + 1, i1 = i1)}
    • else:
    • return args[len(args) - 1][i1]
    • return [{args[0][i1]: nested_dic(*args, i0 = 1, i1 = i1)} for i1 in range(len(args[0]))]
    • def nested_dic(*args):
    • a = list(args)
    • r = a.pop()
    • for x in reversed(a):
    • r = [{y: z} for y, z in zip(x, r)]
    • return r
Algorithms
Logic
Lists
Data Structures
Code
Diff
  • from math import ceil
    
    def variable_chunks(a):
        n = 0
        return [a[n:(n := n + i)] for i in range(1, ceil(((1 + 8 * len(a))**0.5 + 1) / 2))]
    • def variable_chunks(given):
    • i, chunks = 0, []
    • while given := given[i:]:
    • chunks.append(given[:(i := i + 1)])
    • return chunks
    • from math import ceil
    • def variable_chunks(a):
    • n = 0
    • return [a[n:(n := n + i)] for i in range(1, ceil(((1 + 8 * len(a))**0.5 + 1) / 2))]
Code
Diff
  • t = str.maketrans("ACGTacgt", "TGCATGCA")
    
    reverse_compliment = lambda dna: "".join(chr(t[ord(x)]) for x in reversed(dna))
    • reverse_compliment = lambda dna: dna[::-1].translate(str.maketrans("ACGTacgt", "TGCATGCA"))
    • t = str.maketrans("ACGTacgt", "TGCATGCA")
    • reverse_compliment = lambda dna: "".join(chr(t[ord(x)]) for x in reversed(dna))
Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Data Types
Code
Diff
  • def get_primes(n):
        a = [1] * n
        a[0] = a[1] = 0
        for i in range(2, n):
            if a[i]:
                x, y = divmod(n - i**2, i)
                a[i**2:n:i] = [0] * (x + bool(y))
        return [i for i, x in enumerate(a) if x]
    • from math import sqrt
    • def is_prime(num):
    • for newnum in range(2, int(sqrt(num)) + 1):
    • if num % newnum == 0:
    • return False
    • return True
    • def get_primes(num):
    • return [n for n in range(2, num + 1) if is_prime(n)]
    • def get_primes(n):
    • a = [1] * n
    • a[0] = a[1] = 0
    • for i in range(2, n):
    • if a[i]:
    • x, y = divmod(n - i**2, i)
    • a[i**2:n:i] = [0] * (x + bool(y))
    • return [i for i, x in enumerate(a) if x]
Performance
Code
Diff
  • divisors=lambda n,r=[]:r.clear()or[d-n//d and r.append(n//d)or d for d in range(1,int(n**.5+1))if n%d<1]+r[::-1]
    • divisors=lambda n,r=[]:r.clear()or[d for d in range(1,int(n**.5+1))if n%d<1 and(d-n//d and r.append(n//d)or 1)]+r[::-1]
    • divisors=lambda n,r=[]:r.clear()or[d-n//d and r.append(n//d)or d for d in range(1,int(n**.5+1))if n%d<1]+r[::-1]
-- code which does not return anything
Code
Diff
  • int nbDaysInMonth(int n, bool m) {
      return n == 2 ? 28 + m : 30 + (n + (n > 7) & 1);
    }
    • int nbDaysInMonth(int month, bool leap) {
    • int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    • return days[month - 1] + (month == 2 && leap);
    • int nbDaysInMonth(int n, bool m) {
    • return n == 2 ? 28 + m : 30 + (n + (n > 7) & 1);
    • }
Algorithms
Logic
Arrays
Data Types
Mathematics
Numbers
Statistics
Data
Code
Diff
  • function mad(a) {
      if (!a.length) return null;
      let n = a.reduce((x, y) => x + y, 0) / a.length;
      return a.reduce((x, y) => x + Math.abs(y - n), 0) / a.length;
    }
    • function mad(array) {
    • if(array.length === 0){return null}
    • if(array.length === 1){return 0}
    • let average = (array.reduce((acc, cV) => acc + cV))/array.length
    • let array2 = array.map((item,index,arr)=> Math.abs(item-average))
    • return (array2.reduce((acc, cV) => acc + cV))/array2.length;
    • function mad(a) {
    • if (!a.length) return null;
    • let n = a.reduce((x, y) => x + y, 0) / a.length;
    • return a.reduce((x, y) => x + Math.abs(y - n), 0) / a.length;
    • }
Code
Diff
  • #include <functional>
    #include <string>
    
    std::string calculator(int op, int x, int y) {
      static std::array<std::function<int(int, int)>, 5> ops{
        std::plus<int>(),
        std::minus<int>(),
        std::multiplies<int>(),
        std::divides<int>(),
        std::modulus<int>(),
      };
      if (op < 1 || op > 5 || (!y && (op == 4 || op == 5))) return "Invalid Input!";
      return std::to_string(ops[op-1](x, y));
    }
    • #include <iostream>
    • using namespace std;
    • #include <functional>
    • #include <string>
    • string die() {
    • return "Invalid Input!";
    • }
    • string Calculator(int choice, int x, int y) {
    • string answer;
    • if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5){
    • switch (choice) {
    • case 1:
    • answer = std::to_string(x + y);
    • break;
    • case 2:
    • answer = std::to_string(x - y);
    • break;
    • case 3:
    • answer = std::to_string(x * y);
    • break;
    • case 4:
    • answer = std::to_string(x / y);
    • break;
    • case 5:
    • answer = std::to_string(x % y);
    • break;
    • }
    • }
    • else { return die(); }
    • return answer;
    • std::string calculator(int op, int x, int y) {
    • static std::array<std::function<int(int, int)>, 5> ops{
    • std::plus<int>(),
    • std::minus<int>(),
    • std::multiplies<int>(),
    • std::divides<int>(),
    • std::modulus<int>(),
    • };
    • if (op < 1 || op > 5 || (!y && (op == 4 || op == 5))) return "Invalid Input!";
    • return std::to_string(ops[op-1](x, y));
    • }
Code
Diff
  • def convert(s):
        return "".join(x.lower() if i & 1 else x.upper() for i, x in enumerate(filter(str.isalpha, s)))
    • import string
    • def HeLlOwOrLddddd(strng):
    • strng = [x for x in strng if x in string.ascii_letters]
    • return "".join([strng[x].upper() if x % 2 == 0 else strng[x].lower() for x in range(len(strng))])
    • def convert(s):
    • return "".join(x.lower() if i & 1 else x.upper() for i, x in enumerate(filter(str.isalpha, s)))
Code
Diff
  • public class HeLlOwOrLddddd {
      public static String convert(String s) {
        StringBuilder sb = new StringBuilder();
        boolean f = false;
        for (char x : s.toCharArray())
          if (Character.isLetter(x))
            sb.append((f = !f) ? Character.toUpperCase(x) : Character.toLowerCase(x));
        return sb.toString();
      }
    }
    • public class HeLlOwOrLddddd {
    • public static String convert(String input) {
    • String salida = "";
    • boolean mayus = true;
    • for (int i=0;i<input.length();i++){
    • if (Character.isLetter(input.charAt(i))){
    • if (mayus){
    • salida+=Character.toUpperCase(input.charAt(i));
    • mayus=false;
    • }else{
    • salida+=Character.toLowerCase(input.charAt(i));
    • mayus=true;
    • }
    • }
    • }
    • return salida;
    • public static String convert(String s) {
    • StringBuilder sb = new StringBuilder();
    • boolean f = false;
    • for (char x : s.toCharArray())
    • if (Character.isLetter(x))
    • sb.append((f = !f) ? Character.toUpperCase(x) : Character.toLowerCase(x));
    • return sb.toString();
    • }
    • }
Loading more items...