Ad
Code
Diff
  • dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a[0]!={'R':'P','P':'S','S':'R'}[b[0]])+1} wins`
    • dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a.slice(0,1)!={'R':'P','P':'S','S':'R'}[b.slice(0,1)])+1} wins`
    • dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a[0]!={'R':'P','P':'S','S':'R'}[b[0]])+1} wins`
Code
Diff
  • firstNonRepeatingCharacter=s=>(j=s.split``,r=j.reduce((o,c)=>(o[c]=(o[c]??0)+1,o),{}),j.find(c=>r[c]==1)??null)
    • const firstNonRepeatingCharacter = (str) => {
    • let counts = {};
    • for(const char of str) {
    • counts[char] = 1 + (counts[char] ? counts[char] : 0);
    • }
    • for(const char in counts) {
    • if(counts[char] == 1) {
    • return char;
    • }
    • }
    • return null;
    • }
    • firstNonRepeatingCharacter=s=>(j=s.split``,r=j.reduce((o,c)=>(o[c]=(o[c]??0)+1,o),{}),j.find(c=>r[c]==1)??null)
Strings
Code
Diff
  • const isUnique = string => new Set(string).size === string.length
    
    • function isUnique(string) {
    • const newString = new Set(string)
    • return newString.size === string.length
    • }
    • const isUnique = string => new Set(string).size === string.length
Code
Diff
  • const nums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    const digitToText = nums.at.bind(nums) ?? 'idk'
    • const nums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    • function digitToText(digit) {
    • return nums[digit]
    • }
    • const digitToText = nums.at.bind(nums) ?? 'idk'
Code
Diff
  • from functools import reduce; from operator import add; tit = lambda *x : reduce(add, x)
    • from functools import reduce; tit = lambda *x : reduce(int.__add__, x)
    • from functools import reduce; from operator import add; tit = lambda *x : reduce(add, x)
Code
Diff
  • def tit(*abs):
        return sum(abs)
    • def tit(a, b):
    • return sum(tit)
    • def tit(*abs):
    • return sum(abs)
Code
Diff
  • #include <vector>    
    using namespace std; 
    
    int sum(vector<int>& v) {
      int sum = 0;
      for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
        sum += *it;
      }
      return sum;
    }
    • #include <iostream>
    • #include <vector>
    • using namespace std;
    • int sum(vector<int>& arr)
    • {
    • int i = 0, sum = 0, n = arr.size() - 1;
    • while(i <= n)
    • {
    • sum += arr[i++];
    • int sum(vector<int>& v) {
    • int sum = 0;
    • for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
    • sum += *it;
    • }
    • return sum;
Code
Diff
  • module ExampleSolution
      let add = (+)
    • module ExampleSolution
    • let add a b = (+) a b
    • let add = (+)
Code
Diff
  • const isReadyToSale = (age, weight) => 
      (Number.isFinite(age) && Number.isFinite(weight)) &&
      (age > 30 && age < 100) && (weight > 1.5 && weight < 10)
    
    • function isReadyToSale(age , weight){
    • // Validasi input required
    • if(!age || !weight)
    • return false
    • // Validasi tipe data input harus number
    • if(!isNumber(age) || !isNumber(weight))
    • return false
    • // Validasi range umur
    • if(age < 0 || age > 100)
    • return false
    • // Validasi range berat
    • if(weight < 0 || weight > 10)
    • return false
    • // Validasi siap jual atau sebaliknya
    • if(age > 30 && weight > 1.5)
    • return true
    • else
    • return false
    • }
    • function isNumber(param){
    • return typeof param == `number`;
    • }
    • const isReadyToSale = (age, weight) =>
    • (Number.isFinite(age) && Number.isFinite(weight)) &&
    • (age > 30 && age < 100) && (weight > 1.5 && weight < 10)
Code
Diff
  • const grades = [[90, 'A'], [80, 'B'], [70, 'C'], [60, 'D']]
    const getGrade = (...ss) => {
      const score = ss.reduce((a,b) => a+b) / ss.length
      return grades.find(([s, g]) => score >= s)?.at(1) || 'F'
    }
    • function getGrade (s1, s2, s3) {
    • let avg = (s1 + s2 + s3) / 3;
    • return avg>= 90
    • ? "A"
    • : avg>=80
    • ? "B"
    • : avg>=70
    • ? "C"
    • : avg>=60
    • ? "D"
    • : "F"
    • const grades = [[90, 'A'], [80, 'B'], [70, 'C'], [60, 'D']]
    • const getGrade = (...ss) => {
    • const score = ss.reduce((a,b) => a+b) / ss.length
    • return grades.find(([s, g]) => score >= s)?.at(1) || 'F'
    • }
Code
Diff
  • module Solution where
    
    make_mod_repr d repr n | n `mod` d == 0 = Just repr
                           | otherwise      = Nothing
                           
    mod_3_repr = make_mod_repr 3 "fizz"
    mod_5_repr = make_mod_repr 5 "buzz"
    
    fizzbuzz :: [String] -> Int -> Int -> [String]
    fizzbuzz _ i n = map fizzbuzz' [i .. n]
      where
        fizzbuzz' x | repr == Nothing = show x
                    | otherwise       = (\(Just x) -> x) repr
            where 
                repr = foldl (\acc f -> acc <> f x) Nothing [mod_3_repr, mod_5_repr]
    
    -- we don't need the empty accumulator
    -- could be fizzbuzz :: Int -> Int -> [String]
    • module Solution where
    • make_mod_repr d repr n | n `mod` d == 0 = Just repr
    • | otherwise = Nothing
    • mod_3_repr = make_mod_repr 3 "fizz"
    • mod_5_repr = make_mod_repr 5 "buzz"
    • fizzbuzz :: [String] -> Int -> Int -> [String]
    • fizzbuzz _ i n = map fizzbuzz' [i .. n]
    • where fizzbuzz' n
    • | n `mod` 15 == 0 = "fizzbuzz"
    • | n `mod` 3 == 0 = "fizz"
    • | n `mod` 5 == 0 = "buzz"
    • | otherwise = show n
    • where
    • fizzbuzz' x | repr == Nothing = show x
    • | otherwise = (\(Just x) -> x) repr
    • where
    • repr = foldl (\acc f -> acc <> f x) Nothing [mod_3_repr, mod_5_repr]
    • -- we don't need the empty accumulator
    • -- could be fizzbuzz :: Int -> Int -> [String]
Code
Diff
  • import re
    
    is_palindrome = lambda s: (lambda r: r == r[::-1])(re.sub(r'[^a-zA-Z0-9]', '', s.lower()))
    • import re
    • is_palindrome = lambda s: re.sub(r'[^a-zA-Z0-9]', '', s.lower()) == re.sub(r'[^a-zA-Z0-9]', '', s.lower())[::-1]
    • is_palindrome = lambda s: (lambda r: r == r[::-1])(re.sub(r'[^a-zA-Z0-9]', '', s.lower()))