okay, but why if if when you can if else if ?

Code
Diff
  • import java.util.*;
    interface HighLow {
      static int[] printLargestAndSmallest(int[] nums) {
            int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
            for(int i=0;i<nums.length; i++){
                if(max<nums[i])max=nums[i]; else  //  <----
                if(min>nums[i])min=nums[i];
            }
            return nums == null || nums.length==0 ? null : new int[]{min, max};
        }
    }
  • 11
    import java.util.*;
    
    22
    interface HighLow {
    
    33
      static int[] printLargestAndSmallest(int[] nums) {
    
    44
            int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
    
    55
            for(int i=0;i<nums.length; i++){
    
    6
                if(max<nums[i])max=nums[i];
    
    6+
                if(max<nums[i])max=nums[i]; else  //  <----
    
    77
                if(min>nums[i])min=nums[i];
    
    88
            }
    
    99
            return nums == null || nums.length==0 ? null : new int[]{min, max};
    
    1010
        }
    
    1111
    }
    
Code
Diff
  • decr = lambda a: sum(map(lambda h: int(h, 16), a.split(':')))
  • 1
    decr = lambda a: sum(int(h, 16) for h in a.split(':'))
    
    1+
    decr = lambda a: sum(map(lambda h: int(h, 16), a.split(':')))
    

0

Code
Diff
  • def rubegoldberg()
      0 ** 0
    end
  • 11
    def rubegoldberg()
    
    2
        return rand(1..10**rand(1..10)) ** 0
    
    2+
      0 ** 0
    
    33
    end
    
Code
Diff
  • interface HighLow {
      static int[] printLargestAndSmallest(int[] nums) {
        int[] hiLo = new int[] {nums[0], nums[0]};
        for(int n : nums) {
          hiLo[0] = hiLo[0] <= n ? hiLo[0] : n;
          hiLo[1] = hiLo[1] >= n ? hiLo[1] : n;
        }
        return hiLo;
      }
    }
  • 11
    interface HighLow {
    
    22
      static int[] printLargestAndSmallest(int[] nums) {
    
    3
        int min = nums[0], max = nums[0];
    
    4
        for (int n : nums) {
    
    5
          min = min <= n ? min : n;
    
    6
          max = max >= n ? max : n;
    
    3+
        int[] hiLo = new int[] {nums[0], nums[0]};
    
    4+
        for(int n : nums) {
    
    5+
          hiLo[0] = hiLo[0] <= n ? hiLo[0] : n;
    
    6+
          hiLo[1] = hiLo[1] >= n ? hiLo[1] : n;
    
    77
        }
    
    8
        return new int[] {min, max};
    
    8+
        return hiLo;
    
    99
      }
    
    1010
    }
    
Code
Diff
  • abbrev = lambda s: s if (n := len(s) - 2) < 2 else f'{s[0]}{n}{s[-1]}'
  • 1
    def abbrev(s):
    
    2
        length = len(s)
    
    3
        if length < 4:
    
    4
            return s
    
    5
        return s[0] + str(length - 2) + s[-1]
    
    1+
    abbrev = lambda s: s if (n := len(s) - 2) < 2 else f'{s[0]}{n}{s[-1]}'
    
Strings
Arrays
Code
Diff
  • #special_fission=lambda s:list(filter(lambda x:x.isalpha(),list(s)))  #
    
    #special_fission=lambda s:list(filter(lambda c:c.isalpha(),s))        #  11179.08ms
    
    #special_fission=lambda s:[*filter(lambda c:c.isalpha(),s)]           #  10973.41ms
    
    #from string import ascii_letters
    #special_fission=lambda s:[*filter(lambda c:c in ascii_letters,s)]    #   9228.35ms
    
    special_fission=lambda s: [*filter(lambda c: \
        c in "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", s)]  #   8759.84ms
  • 1
    special_fission=lambda s:list(filter(lambda x:x.isalpha(),list(s)))
    
    1+
    #special_fission=lambda s:list(filter(lambda x:x.isalpha(),list(s)))  #
    
    2+
    3+
    #special_fission=lambda s:list(filter(lambda c:c.isalpha(),s))        #  11179.08ms
    
    4+
    5+
    #special_fission=lambda s:[*filter(lambda c:c.isalpha(),s)]           #  10973.41ms
    
    6+
    7+
    #from string import ascii_letters
    
    8+
    #special_fission=lambda s:[*filter(lambda c:c in ascii_letters,s)]    #   9228.35ms
    
    9+
    10+
    special_fission=lambda s: [*filter(lambda c: \
    
    11+
        c in "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", s)]  #   8759.84ms
    

Take a number and repeatedly add its digits to form a new number until the new number is less than 10.

EX. 1245358,
output=1+2+4+5+3+5+8=28, 28=2+8=10, 10=1+0=1
so, output=1

Code
Diff
  • def digital_root(n):
        "NOPE"
  • 1
    n=int(input())
    
    2
                                                                                       
    
    3
    4
    5
    for i in range(n):
    
    6
        add=add+n[i]
    
    7
        if add>9:
    
    8
            while add<10:
    
    9
                for j in range(add):
    
    10
                    add+=add[j]
    
    11
                    print(add)
    
    12
        else:
    
    13
            print(add)
    
    14
        
    
    15
        
    
    1+
    def digital_root(n):
    
    2+
        "NOPE"
    
Code
Diff
  • is_prime=lambda n:n in(2,3)or n%2*n%3*n>1!=0
  • 1
    def is_prime(n):
    
    2
        return bool(n>1 and n%2 and n%3 or n in (2,3))
    
    1+
    is_prime=lambda n:n in(2,3)or n%2*n%3*n>1!=0
    

Write a function that receives three random integers and assigns their sorted values to an array pointer. Integers should be sorted without using any predefined or external functions.

Novelty is considered at a high premium here.

Code
Diff
  • void sort_three(int a, int b, int c, int *sorted)
    {
    
        sorted[0] = a < b ? (a < c ? a : c) : (b < c ? b : c);
        sorted[1] = sorted[0] == a ? (b < c ? b : c) : (b < c ? (a < c ? a : c) : (a < b ? a : b));
        sorted[2] = sorted[0] == a ? (sorted[1] == b ? c : b) : (sorted[0] == b ? (sorted[1] == a ? c : a) : (sorted[1] == a ? b : a));
      
    }
    
    // just insta-barfed it & passed lol
  • 11
    void sort_three(int a, int b, int c, int *sorted)
    
    22
    {
    
    33
    4
        if(a > b) { int t=a; a=b; b=t; }
    
    5
        if(b > c) { int t=b; b=c; c=t; }
    
    6
        if(a > b) { int t=a; a=b; b=t; }
    
    7
    8
        sorted[0] = a;
    
    9
        sorted[1] = b;
    
    10
        sorted[2] = c;
    
    11
    4+
        sorted[0] = a < b ? (a < c ? a : c) : (b < c ? b : c);
    
    5+
        sorted[1] = sorted[0] == a ? (b < c ? b : c) : (b < c ? (a < c ? a : c) : (a < b ? a : b));
    
    6+
        sorted[2] = sorted[0] == a ? (sorted[1] == b ? c : b) : (sorted[0] == b ? (sorted[1] == a ? c : a) : (sorted[1] == a ? b : a));
    
    7+
      
    
    1212
    }
    
    9+
    10+
    // just insta-barfed it & passed lol
    
Algorithms
Data Structures

You are working to an accounting company and your boss ask to make a program to get the most worked years.

Him already have the data in the database and will response with a matrix data.

For example: [[2001,2005],[1920,1960],[2001,1999],...] what you
will receive from the database.

The program only should return only an integer value corresponding to the most worked year.

Code
Diff
  • function mostWorkedYears(matrix) {
      let most = 0, count = 0, years = {};
      for(let array of matrix) {
        for(let year of array) {
          let num = years[year];
          if(num === undefined) {
            num = 1
            years[year] = num;
          }
          else {
            years[year]++;
          }
          if(num > count) {
             count = num;
             most = year;
          }
        }
      }
      return most;
    }
  • 1
    const mostWorkedYears = (matrix) =>
    
    2
      matrix
    
    3
        .map((item) => item[0])
    
    4
        .reduce(
    
    5
          (accum, curr, index, array) =>
    
    6
            curr === array[index + 1] ? accum + curr : curr,
    
    7
          0
    
    8
        );
    
    1+
    function mostWorkedYears(matrix) {
    
    2+
      let most = 0, count = 0, years = {};
    
    3+
      for(let array of matrix) {
    
    4+
        for(let year of array) {
    
    5+
          let num = years[year];
    
    6+
          if(num === undefined) {
    
    7+
            num = 1
    
    8+
            years[year] = num;
    
    9+
          }
    
    10+
          else {
    
    11+
            years[year]++;
    
    12+
          }
    
    13+
          if(num > count) {
    
    14+
             count = num;
    
    15+
             most = year;
    
    16+
          }
    
    17+
        }
    
    18+
      }
    
    19+
      return most;
    
    20+
    }
    
Code
Diff
  • remoue=lambda ಠ‿ಠ:__import__('re').sub(r'[A-Z 0-9]','',ಠ‿ಠ) # I just realized why this is no good :(
    
    remove=lambda s:''.join(__import__('re').findall(r'[a-z]',s)) # can the regex also do the ''.join() ?
  • 1
    remove=lambda s:''.join(filter(str.islower,s))
    
    1+
    remoue=lambda ಠ‿ಠ:__import__('re').sub(r'[A-Z 0-9]','',ಠ‿ಠ) # I just realized why this is no good :(
    
    2+
    3+
    remove=lambda s:''.join(__import__('re').findall(r'[a-z]',s)) # can the regex also do the ''.join() ?
    

create a program that takes an input and converts that input and removes all the spaces, numbers, and capital letters and then it counts how many charecters are left.

you will be given a string called s.

Code
Diff
  • CAPITAL_LETTERS = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')
    NUMBERS = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
    SPACE = (' ')
    
    def remove(ಠ‿ಠ):
        for category in (CAPITAL_LETTERS, NUMBERS, SPACE):
            for item in category:
                while item in ಠ‿ಠ:
                    ಠ‿ಠ = remove(ಠ‿ಠ.replace(item, ''))
        return ಠ‿ಠ
  • 1
    def remove(s):
    
    1+
    CAPITAL_LETTERS = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    
    2+
                       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')
    
    3+
    NUMBERS = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
    
    4+
    SPACE = (' ')
    
    5+
    6+
    def remove(ಠ‿ಠ):
    
    7+
        for category in (CAPITAL_LETTERS, NUMBERS, SPACE):
    
    8+
            for item in category:
    
    9+
                while item in ಠ‿ಠ:
    
    10+
                    ಠ‿ಠ = remove(ಠ‿ಠ.replace(item, ''))
    
    11+
        return ಠ‿ಠ
    

yeah, this too

Code
Diff
  • split = lambda s: [*s]
  • 1
    split = lambda a: list(a)
    
    1+
    split = lambda s: [*s]
    

duplicating a built-in; no-where to go. end of kumite? you would think . . .

Code
Diff
  • split = list
  • 1
    def split(word):
    
    2
        # if word is "hello" returns ["h", "e", "l", "l", "o"]
    
    3
        return [x for x in word]
    
    1+
    split = list
    
Code
Diff
  • split = lambda a: list(a)
  • 1
    def split(word):
    
    2
        # if word is "hello" returns ["h", "e", "l", "l", "o"]
    
    3
        return [x for x in word]
    
    1+
    split = lambda a: list(a)
    
Loading more items...