### Communities

tag:snake search within a tag
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
created:<1w created < 1 week ago
post_type:xxxx type of post
Challenges

+12
−0

# Challenge

Given a list of n numbers and x, compute $a + bx^1 + cx^{2} + ... + zx^{n-1}$, where a is the first value in the list, b is the second, etc. n is at most 256 and at least 0. The input value(s) can be any 32-bit float

Input can be in any format of choice, as long as it is a list of numbers and x. (And this'll likely stay this way, even if input rules change over time)

# Test inputs

[1.0], 182 -> 1
[1.0, 2.0], 4 -> 9
[2.5, 2.0], 0.5 -> 3.5
[1.0, 2.0, 3.0, 4.0], 1.5 -> 24.25

# Example ungolfed program (Rust)

// dbg! is a logging function, prints the expression and it's output.
// Good for seeing what's happening

// Test setup
pub fn main() {
let inp: &[f32] = &[1.0, 2.0, 3.0, 4.0];
let x: f32 = 1.5;
dbg!(evaluate_polynomial(inp, x)); // take inputs, print result
}

pub fn evaluate_polynomial(inp: &[f32], x: f32) -> f32 {
let mut accum: f32 = 0.0;

for (idx, val) in inp.iter().enumerate() {
// x.pow(idx) * val
accum += dbg!(x.powf(idx as f32) * val);
}

return accum;
}

Why does this post require moderator attention?
Why should this post be closed?

+9
−0

# APL (Dyalog Unicode), 113 1 byte

⊥


Try it online!

Anyone who can golf this further gets a cookie!

Function submission which takes reversed coefficients as right argument and $x$ as left argument.

-8 bytes from dzaima and rak1507(APL Orchard).

Uses a mixed base conversion.

Why does this post require moderator attention?

+5
−0

# Ruby, 50 bytes

def f(k,x)k.length>1?k+f(k[1..-1],x)*x:k[-1]end


Try it online!

This uses the Horner's method recursively, because I think it'll be slightly shorter than using a loop or builtin array functions.

Also, this is my first post on this website ... er my first real attempt at golfing something in Ruby, so please feel free to suggest ways to shorten my solution.

Why does this post require moderator attention?

Tips (44 bytes) (1 comment)
+5
−0

# Japt-x, 65bytes

Ë*VpE


Try it

Ë*VpE     :Implicit input of array U and float V
Ë         :Map each element D at 0-based index E
*        :  Multiply D by
VpE     :  V raised to the power of E
:Implicit output of sum of resulting array
Why does this post require moderator attention?

+4
−0

# Japt, 12 bytes

ÊÆgX *VpXÃr+


Loops through the range of integers 0 to n-1, calculates each term, and sums.

Try it

Why does this post require moderator attention?

+4
−0

# JavaScript (Node.js), 40 bytes

f=(a,b,c=1)=>a.reduce((d,e)=>d+e*(c*=b))


Try it online!

Why does this post require moderator attention?

+4
−0

# Ruby, 38 bytes

Simple map and sum over the coefficients. No TIO link, this uses numbered lambda parameters which require Ruby 2.7.

->l,x{l.each_with_index.sum{_1*x**_2}}

Why does this post require moderator attention?

+4
−0

# Raku, 19 bytes

(*Z*(*X**0..*)).sum


Try it online!

Is it concerning that my solution is over 30% asterisks?

### Explanation

(             ).sum  # Get the sum of
*Z*(        )       # The input list zip multiplied by
*X**            # The second input to the power of
0..*        # 0 to infinity


An alternate curried solution for 19 bytes is:

{*.reduce(*×$_+*)}  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +4 −0 # Vyxal, 6, 5, 4 bytes Źe*∑  Try it Online! Takes input in the format coeffs, x ## Explained Źe*∑ Ź # Generate range [0, len(coeffs)) e # Calculate x ** [0, len(coeffs) (vectorising) * # Multiply the coefficients by the exponated x's. The lists are extended with 0s to be the same length ∑ # Sum that list and output  Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +4 −0 # Haskell, 20 bytes f x=foldl((+).(x*))0  Try it online! Takes input coefficients from highest degree to lowest. 21 bytes x%(h:t)=h+x*x%t x%_=0  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +3 −0 # Jelly, 1 byte ḅ  Try it online! Essentially just Razetime's APL answer, except in that ḅ vectorizes rather than carrying out mixed base conversion--irrelevant if, as is the case here, the provided base is scalar. Takes a reversed coefficient list on the left and$x\$ on the right.

Why does this post require moderator attention?

+2
−0

# Pyth, 10 bytes

s*R^H~hZhA


Try it online!

Alternate 10 byte solution:

s.e*b^eQkh


Try it online!

Why does this post require moderator attention?

+2
−0

# JavaScript (Node.js), 33 bytes

a=>n=>a.reduce((o,x,y)=>o+x*n**y)


Try it online!

Why does this post require moderator attention?

+1
−0

# Scala, 18 bytes

x=>_.:\(.0)(_+_*x)


Try it online!

Not too complicated. Takes the list of coefficients and folds from the right, multiplying the accumulator by x each time and adding the next coefficient. reduceRight would've worked instead of :\(0), but it's golfier, and I haven't used /: or :\ in a while.

Why does this post require moderator attention?

+1
−0

# C++ (gcc), 61 bytes

float f(int n,float*p,float x){return n?*p+x*f(n-1,p+1,x):0;}


Try it online!

Why does this post require moderator attention?

+1
−0

# Python 3, 46 bytes

f=lambda p,x,a=0:p and f(p[1:],x,a*x+p)or a


Try it online!

Takes input reversed.

Why does this post require moderator attention?

General (1 comment)
+1
−0

# Python 3, 46 bytes

lambda a,x:sum(c*x**i for i,c in enumerate(a))


Try it online!

Why does this post require moderator attention?

+0
−0

# J, 2 bytes

p.


Try it online!

J likes inflections so it won't beat APL, but J is still a contender :).

Why does this post require moderator attention?

+0
−0

# Python 3, 16712711811794 63 bytes

def f(a,b):
y=0
for z in range(len(a)):y+=a[z]*b**z
print(y)


Try it online!

Close gap to @user's lambda answer!

Golfed 40 bytes thanks to @user's advice. Golfed another 9 bytes thanks to @user's advice.

Why does this post require moderator attention?

+0
−0

# Ruby, 29 bytes

->l,x{a,*b=l;a ?a+x*f[b,x]:0}


Try it online

Why does this post require moderator attention?

+0
−0

# BQN, 13 bytes

{+´(𝕨⋆↕≠𝕩)×𝕩}

{           }  # fn
+´            # sum reduce
(𝕨⋆↕≠𝕩)     # x^i for each term
×𝕩   # times each coefficient


Try it

Why does this post require moderator attention?