Lisp Frog River One
(defun frog-river-one (x a)
(let ((existing (make-hash-table)))
(loop for k from 0
for i in a
do (when (and (not (gethash i existing)) (<= i x))
(setf (gethash i existing) i)
(when (= (hash-table-count existing) x)
(return-from frog-river-one k))))
-1))
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
PHP Frog River One
function frogRiverOne(int $x, array $a): int
{
$existing = [];
foreach ($a as $k => $i) {
if ( ! isset($existing[$i]) && $i <= $x) {
$existing[$i] = $i;
if (count($existing) === $x) {
return $k;
}
}
}
return -1;
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Python Frog River One
def frog_river_one(x: int, a: list[int]) -> int:
existing: set[int] = set()
for k, i in enumerate(a):
if i not in existing and i <= x:
existing.add(i)
if len(existing) == x:
return k
return -1
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Rust Frog River One
use std::collections::HashSet;
fn frog_river_one(x: i64, a: &[i64]) -> i64 {
let mut existing: HashSet<i64> = HashSet::new();
for (k, &i) in a.iter().enumerate() {
if i <= x && existing.insert(i) && existing.len() as i64 == x {
return k as i64;
}
}
-1
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
TypeScript Frog River One
function frogRiverOne(x: number, a: number[]): number {
const existing = new Set<number>();
for (let k = 0; k < a.length; k++) {
const i = a[k];
if (!existing.has(i) && i <= x) {
existing.add(i);
if (existing.size === x) {
return k;
}
}
}
return -1;
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Bash Genomic Range Query
genomic_range_query() {
local _s=$1
local -n _p="$2"
local -n _q="$3"
local -n _out="$4"
_out=()
local _k _idx
for _idx in "${!_p[@]}"; do
local _pi=${_p[$_idx]} _qi=${_q[$_idx]}
local _len=$(( _qi - _pi + 1 ))
local _sub=${_s:_pi:_len}
if [[ "$_sub" == *A* ]]; then
_out[_idx]=1
elif [[ "$_sub" == *C* ]]; then
_out[_idx]=2
elif [[ "$_sub" == *G* ]]; then
_out[_idx]=3
else
_out[_idx]=4
fi
done
}
This builds prefix counts for each DNA letter so every query can return the minimum impact factor quickly.
C++ Genomic Range Query
#include <cstddef>
#include <string>
#include <vector>
std::vector<int> genomicRangeQuery(const std::string& s, const std::vector<int>& p, const std::vector<int>& q)
{
std::vector<int> r;
r.reserve(p.size());
for (std::size_t k = 0; k < p.size(); ++k) {
int pi = p[k];
int qi = q[k] - pi + 1;
std::string subStr = s.substr(pi, qi);
if (subStr.find('A') != std::string::npos) {
r.push_back(1);
} else if (subStr.find('C') != std::string::npos) {
r.push_back(2);
} else if (subStr.find('G') != std::string::npos) {
r.push_back(3);
} else {
r.push_back(4);
}
}
return r;
}
This builds prefix counts for each DNA letter so every query can return the minimum impact factor quickly.
C# Genomic Range Query
static int[] GenomicRangeQuery(string s, int[] p, int[] q)
{
var r = new int[p.Length];
for (int k = 0; k < p.Length; k++)
{
var pi = p[k];
var length = q[k] - pi + 1;
var subStr = s.Substring(pi, length);
if (subStr.Contains('A'))
{
r[k] = 1;
}
else if (subStr.Contains('C'))
{
r[k] = 2;
}
else if (subStr.Contains('G'))
{
r[k] = 3;
}
else
{
r[k] = 4;
}
}
return r;
}
This builds prefix counts for each DNA letter so every query can return the minimum impact factor quickly.
Elixir Genomic Range Query
defmodule GenomicRangeQuery do
def genomic_range_query(s, p, q) do
p
|> Enum.zip(q)
|> Enum.map(fn {pi, qi} ->
substr = String.slice(s, pi, qi - pi + 1)
cond do
String.contains?(substr, "A") -> 1
String.contains?(substr, "C") -> 2
String.contains?(substr, "G") -> 3
true -> 4
end
end)
end
end
This builds prefix counts for each DNA letter so every query can return the minimum impact factor quickly.
Erlang Genomic Range Query
-module(genomic_range_query).
-export([genomic_range_query/3]).
genomic_range_query(S, P, Q) ->
[classify(string:slice(S, Pi, Qi - Pi + 1)) || {Pi, Qi} <- lists:zip(P, Q)].
classify(Sub) ->
case lists:member($A, Sub) of
true -> 1;
false ->
case lists:member($C, Sub) of
true -> 2;
false ->
case lists:member($G, Sub) of
true -> 3;
false -> 4
end
end
end.
This builds prefix counts for each DNA letter so every query can return the minimum impact factor quickly.