17
\$\begingroup\$

A doubling sequence is an array of numbers where each subsequent number is at least twice the previous number.

Given an ordered list of numbers, determine if the numbers in the list (\$n_{x}\$) have the property that:

\$n_1 \times 2 \le n_2\$

\$n_2 \times 2 \le n_3\ ...\$

and so on until reaching the end of the list.

Input

A list of two or more numbers.

Output

Any distinct True or False value.

Examples:

[10,20,30] -> False
[10,20,40] -> True
[1,2,3] -> False
[1,2,4] -> True
[1,2,10] -> True
[1,1] -> False
[10,1] -> False
\$\endgroup\$
8
  • \$\begingroup\$ Can input be sorted in either order? \$\endgroup\$
    – mabel
    Feb 12, 2020 at 18:20
  • 1
    \$\begingroup\$ Pre-sorted in that it may be sorted ascending, descending, or some other order, but you don't have to change the order of the list that is fed to the function. \$\endgroup\$ Feb 12, 2020 at 18:20
  • \$\begingroup\$ Since inputs can be non-integers, do we care about floating point precision? Many of these solution fail for n_i+1 = n_i*(2-epislon) for some fairly large epsilons \$\endgroup\$
    – Vlo
    Feb 12, 2020 at 19:40
  • 14
    \$\begingroup\$ what about negative numbers? what about the sequence of zeros? \$\endgroup\$
    – mazzy
    Feb 12, 2020 at 20:03
  • \$\begingroup\$ "some other order" can be any reflexive transitive antisymmetric relation. In other words, your definition admits arbitrary order of the elements, which I'd guess is not what you intended. \$\endgroup\$
    – kyrill
    Feb 12, 2020 at 22:19

37 Answers 37

1
2
0
\$\begingroup\$

Python 3, 52 bytes

lambda l:all(2*l[i]<=l[i+1]for i in range(len(l)-1))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog Extended), 18 bytes

∧/{(2ׯ1↓⍵)≤(1↓⍵)}

Try it online!

Decomposition/Explanation:

⍵ is the input vector (test cases). Output will be 1 or 0; 1 is TRUE/truthy, 0 is FALSE/falsey.

            (1↓⍵)  - Drop the first element from the input vector (call it LASTN)
      ¯1↓⍵         - Drop the last element from the input vector, separately, and
   (2×    )        - multiply it by two (call the result DFIRSTN)
           ≤       - compare corresponding elements - Each element of LASTN should be 
                     equal to or greater than the corresponding element of DFIRSTN.
  {              } - encapsulate as a dfn to apply to the input vector. This dfn will
                     return 0 for elements where the comparison is FALSE, 1 where TRUE
∧/                 - AND (Boolean conjunction) reduction of the vector. ∧/ A B C D ... is
                     equivalent to A ∧ B ∧ C ∧ D ∧ ..., and if there are any FALSE values
                     in the vector, the result will be FALSE (0)

The linked TIO has the test cases and a REPL; you can test your own vectors by changing the input field, one vector per line.

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 51 bytes

r;f(a,l)int*a;{for(r=--l;l--;)r=*a*2>*++a?0:r;a=r;}

Pretty simple. For every element of the array, check if the current element times two is greater than the next element.

Returns zero if it is not a doubling sequence and non-zero (or more specifically the length minus one) otherwise.

-10 bytes thanks to ceilingcat!

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ @ceilingcat Thanks. I was trying to find a solution with no counter variable. \$\endgroup\$
    – S.S. Anne
    Feb 14, 2020 at 20:28
  • \$\begingroup\$ 49 bytes \$\endgroup\$
    – ceilingcat
    Feb 14, 2020 at 21:07
0
\$\begingroup\$

T-SQL, 64 bytes

This can handle positive values above 0

Returns -1 for true, 0 for false

SELECT~min(1/~x)FROM(SELECT a/lag(2*a)over(ORDER BY i)x FROM @)y

Try it online

\$\endgroup\$
0
\$\begingroup\$

C++ (gcc), 75 71 67 65 bytes

int f(int*a,int s){int r=1;for(;--s;)r*=a[s]>=2*a[s-1];return r;}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ @ceilingcat Thanks for the input! I thought the i++ placement was UB, so I dumped the variable - 67 bytes! \$\endgroup\$ Feb 23, 2020 at 15:24
  • \$\begingroup\$ @ceilingcat Should've thought of that! \$\endgroup\$ Mar 1, 2020 at 18:01
  • 1
    \$\begingroup\$ 64 \$\endgroup\$
    – ceilingcat
    Dec 7, 2020 at 10:06
0
\$\begingroup\$

Rockstar, 101 100 bytes

listen to F
D's0
while F
listen to T
let R be F/T-.5
turn up R
let D be D or R
let F be T

say not D

Try it here (Code will need to be pasted in, with each input integer on an individual line)

\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica), 25 15 bytes

Min@Ratios@#<2&

Returns True for false and False for true.

-10 bytes thanks to @att

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 15 bytes \$\endgroup\$
    – att
    Sep 15, 2020 at 21:24
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.