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`
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
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'
#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;
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 requiredif(!age || !weight)return false// Validasi tipe data input harus numberif(!isNumber(age) || !isNumber(weight))return false// Validasi range umurif(age < 0 || age > 100)return false// Validasi range beratif(weight < 0 || weight > 10)return false// Validasi siap jual atau sebaliknyaif(age > 30 && weight > 1.5)return trueelsereturn 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)
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'
- }
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]
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()))