Evaluative
2 minutes to read
We are given the following website to write a coding solution:

Problem
We need to write a program that is evaluate a polynomial with given coefficients
"1 -2 3 -4 5 -6 7 -8 9","5"->"2983941"
Solution
Since we are allowed to use C, C++, Python and Rust, let’s write a solution on every language, just because.
C
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
// take in the numbers
char input[4096] = { 0 };
fgets(input, sizeof(input), stdin);
long coeffs[1024] = { 0 };
char* token = strtok(input, " ");
int i = 0;
while (token != NULL) {
coeffs[i++] = atol(token);
token = strtok(NULL, " ");
}
int length = i;
long x = 0;
scanf("%ld", &x);
// calculate answer
long answer = 0;
for (i = 0; i < length; i++) {
answer += coeffs[i] * (long) pow(x, i);
}
// print answer
printf("%ld\n", answer);
return 0;
}
C++
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
int main() {
// take in the coeffs
std::string s;
std::vector<long> coeffs;
long n;
std::getline(std::cin, s);
std::istringstream iss(s);
while (iss >> n) {
coeffs.push_back(n);
}
long x;
std::cin >> x;
// calculate answer
long answer = 0;
for (long i = 0; i < coeffs.size(); i++) {
answer += coeffs[i] * (long) pow(x, i);
}
// prlong answer
std::cout << answer << std::endl;
return 0;
}
Python
# take in the number
coeffs = map(int, input().split())
x = int(input())
# calculate answer
answer = sum(c * x ** i for i, c in enumerate(coeffs))
# print answer
print(answer)
Rust
use std::io;
fn main() {
// take in the numbers
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
let coeffs: Vec<i64> = input
.trim()
.split(" ")
.map(|s| s.parse().expect("Please enter a valid number"))
.collect();
input.clear();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
let x: i64 = input.trim().parse().expect("Please enter a valid number");
// calculate answer
let answer: i64 = coeffs
.iter()
.enumerate()
.map(|(i, c)| c * x.pow(i as u32))
.sum();
// print answer
println!("{}", answer);
}
Flag
Using any of these programs, we are able to solve the chalenge and capture the flag:
HTB{eV4LuaT1nG_p0LyN0M1aL5_f0R_7H3_w1N}