TypeScript Is Ipv 4 Adress
function isIPv4Address(inputString: string): boolean {
const parts = inputString.split(".");
for (const v of parts) {
const n = Number(v);
if (v === "" || Number.isNaN(n) || n > 255 || v !== String(Math.trunc(n))) {
return false;
}
}
return parts.length === 4;
}
This splits the string by dots and validates each part as a normal IPv4 octet.
Bash Ladder
ladder() {
local -n _aArr="$1"
local -n _bArr="$2"
local -n _outArr="$3"
local _size=${#_aArr[@]}
local _maxB
_maxB=$(printf '%s\n' "${_bArr[@]}" | sort -n | tail -1)
local _mod=$(( (1 << _maxB) - 1 ))
local _limitA
_limitA=$(printf '%s\n' "${_aArr[@]}" | sort -n | tail -1)
local -a _fib=(0 1)
local _i
for ((_i = 2; _i < _limitA + 2; _i++)); do
_fib[_i]=$(( (_fib[_i-1] + _fib[_i-2]) & _mod ))
done
_outArr=()
for ((_i = 0; _i < _size; _i++)); do
_outArr[_i]=$(( _fib[_aArr[_i]+1] & ((1 << _bArr[_i]) - 1) ))
done
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
C++ Ladder
#include <algorithm>
#include <vector>
std::vector<int> ladder(const std::vector<int>& a, const std::vector<int>& b)
{
std::size_t size = a.size();
std::vector<int> r(size, 0);
int maxB = *std::max_element(b.begin(), b.end());
long long mod = (1LL << maxB) - 1;
int maxA = *std::max_element(a.begin(), a.end());
std::vector<long long> fib(maxA + 2, 0);
fib[1] = 1;
for (int i = 2; i < maxA + 2; ++i) {
fib[i] = (fib[i - 1] + fib[i - 2]) & mod;
}
for (std::size_t i = 0; i < size; ++i) {
r[i] = static_cast<int>(fib[a[i] + 1] & ((1LL << b[i]) - 1));
}
return r;
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
C# Ladder
static int[] Ladder(int[] a, int[] b)
{
var size = a.Length;
var r = new int[size];
var mod = (1 << b.Max()) - 1;
var limit = a.Max();
var fib = new int[limit + 2];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < limit + 2; i++)
{
fib[i] = (fib[i - 1] + fib[i - 2]) & mod;
}
for (int i = 0; i < size; i++)
{
r[i] = fib[a[i] + 1] & ((1 << b[i]) - 1);
}
return r;
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Elixir Ladder
defmodule Ladder do
import Bitwise
def ladder(a, b) do
mod = (1 <<< Enum.max(b)) - 1
limit = Enum.max(a)
fib = build_fib(limit, mod)
a
|> Enum.zip(b)
|> Enum.map(fn {ai, bi} -> Map.get(fib, ai + 1) &&& (1 <<< bi) - 1 end)
end
defp build_fib(limit, mod) do
Enum.reduce(2..(limit + 1)//1, %{0 => 0, 1 => 1}, fn i, fib ->
value = (Map.get(fib, i - 1) + Map.get(fib, i - 2)) &&& mod
Map.put(fib, i, value)
end)
end
end
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Erlang Ladder
-module(ladder).
-export([ladder/2]).
%% Erlang integers are arbitrary precision, so unlike the PHP version we
%% don't need to mask the running Fibonacci total against max(B) on every
%% step to dodge overflow; masking once at the end with each B(i) is enough.
ladder(A, B) ->
MaxA = lists:max(A),
Fib = build_fib(MaxA),
[array:get(Ai + 1, Fib) band ((1 bsl Bi) - 1) || {Ai, Bi} <- lists:zip(A, B)].
build_fib(Limit) ->
Arr0 = array:set(1, 1, array:set(0, 0, array:new(Limit + 2))),
build_fib(2, Limit + 1, Arr0).
build_fib(I, Limit, Arr) when I > Limit ->
Arr;
build_fib(I, Limit, Arr) ->
V = array:get(I - 1, Arr) + array:get(I - 2, Arr),
build_fib(I + 1, Limit, array:set(I, V, Arr)).
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Go Ladder
func ladder(a, b []int) []int {
size := len(a)
result := make([]int, size)
maxB := b[0]
for _, v := range b {
if v > maxB {
maxB = v
}
}
mod := (1 << maxB) - 1
maxA := a[0]
for _, v := range a {
if v > maxA {
maxA = v
}
}
fib := make([]int, maxA+2)
fib[0], fib[1] = 0, 1
for i := 2; i < maxA+2; i++ {
fib[i] = (fib[i-1] + fib[i-2]) & mod
}
for i := 0; i < size; i++ {
result[i] = fib[a[i]+1] & ((1 << b[i]) - 1)
}
return result
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Haskell Ladder
import Data.Array (Array, listArray, (!))
import Data.Bits (shiftL, (.&.))
ladder :: [Int] -> [Int] -> [Int]
ladder a b = [fibArr ! (ai + 1) .&. ((1 `shiftL` bi) - 1) | (ai, bi) <- zip a b]
where
maxA = maximum a
globalMask = (1 `shiftL` maximum b) - 1
fibs = 0 : 1 : zipWith (\x y -> (x + y) .&. globalMask) fibs (tail fibs)
fibArr = listArray (0, maxA + 1) (take (maxA + 2) fibs) :: Array Int Int
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Java Ladder
import java.util.Arrays;
public class Solution {
public static int[] ladder(int[] a, int[] b) {
int size = a.length;
int[] r = new int[size];
int maxB = Arrays.stream(b).max().orElse(0);
int mod = (1 << maxB) - 1;
int maxA = Arrays.stream(a).max().orElse(0);
int[] fib = new int[maxA + 2];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < maxA + 2; i++) {
fib[i] = (fib[i - 1] + fib[i - 2]) & mod;
}
for (int i = 0; i < size; i++) {
r[i] = fib[a[i] + 1] & ((1 << b[i]) - 1);
}
return r;
}
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Lisp Ladder
(defun ladder (a b)
(let* ((veca (coerce a 'vector))
(vecb (coerce b 'vector))
(size (length veca))
(modulus (1- (ash 1 (reduce #'max vecb))))
(limit (reduce #'max veca))
(fib (make-array (+ limit 2) :initial-element 0)))
(setf (aref fib 1) 1)
(loop for i from 2 below (+ limit 2)
do (setf (aref fib i) (logand (+ (aref fib (1- i)) (aref fib (- i 2))) modulus)))
(loop for i from 0 below size
collect (logand (aref fib (1+ (aref veca i))) (1- (ash 1 (aref vecb i)))))))
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.