Getting the sum of all uneven indices of an int [closed] - rust

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
I'm new to rust and tried writing a function that adds all digits at the uneven positions together (For context: I'm trying to solve this Daily programmer #370. But somehow my results are off by a bit and I can't wrap my head around what#s going on.
number.to_string().chars()
.enumerate()
.filter(|(i, _)| i % 2 != 0)
.map(|(_, c)| c as u64 - 48)
.sum::<u64>()
Now if I input 4210000526 I get 13 as a result, although I should get 15. For some reason this only happens with a filter for uneven positions, "i % 2 == 0" works perfectly fine.

Related

Why is 2 raised to 4 equal 10? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I was looking into bitwise operators in rust, and I found that
println!("{:X}", 1 << 4);
prints out 10, but 2^4 should equal 16.
Further experimentation, using powers:
let base: i32 = 2;
for i in 1..=5 {
print!("{:X} ", base.pow(i));
}
will print out
2 4 8 10 20 when it should print out 2 4 8 16 32
Just wondering if you can point me out to anything in the Rust docs that highlights why binary in Rust works like this? And what can I use to do 2^4 = 16?
{:X} print numbers in hexadecimal.
So it prints 10 in base 16 which is 16, the expected answer.
To get the expected result change {:X} to {}.

How can I access the 1st element of a nested list? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a nested list and I want to pull out the first element of every list of lists:
t = [
[['a',1],['b',2],['c',3]],
[['d',1],['e',2],['f',3]],
[['g',1],['h',2],['i',3]]
]
want = ['a','d','g']
I am getting the Comphrension wrong:
list = [x[0][0] for x in t]
It will work as long as you don't call your variable 'list' (call it 'new_t' or whatever) - list is a reserved word in python.

calculation of code is not right and i am not able to find where i make mistake [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've written a program to calculate the standard deviation of a set of numbers. The program is running with no errors, however it is returning an incorrect result.
#standard deviation
import math
def mean(values):
return sum(values)/len(values)
def stanDev(values):
length=len(values)
total_sum = 0
m = mean(values)
for i in range(length):
total_sum += (values[i]-m)**2
under_root=total_sum/(length-1)
return math.sqrt(under_root)
x=[1,2,4,1,2,42,12]
std=stanDev(x)
print(std)
With the current code, I'm getting an output of 3.3243075080628843, however using an online calculator, I'm getting a result of 14.993649449334 for the same set of data.
Do you have the correct indentation in your for loop? The code you supplied should look like this:
length=len(values)
total_sum = 0
m = mean(values)
for i in range(length):
total_sum += (values[i]-m)**2
under_root=total_sum/(length-1) #this line is performed once, after the for loop
return math.sqrt(under_root)

RANDBETWEEN excel function giving errors [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm pasting in the formula:
=RANDBETWEEN(DATE(2014, 1, 1),DATE(2015, 1, 1))
which is mentioned in several tutorials but getting the Excel "error with function message". I'm in Ireland which is a different format but either way it doesn't work.
Anything else I have to do to generate random dates?
Try :
=RANDBETWEEN(41640,42005)
And format as date.
For some countries:
=RANDBETWEEN(41640;42005)
And format as date.
And if the 2nd works maybe just:
=RANDBETWEEN(DATE(2014; 1; 1);DATE(2015; 1; 1))

Issue when running a groovy script for "function_score"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
def score = 0;
// terms: list of tokens
for(term in terms) {
q_term_freq = terms​.countBy { it }​[term]; // for frequency of each term in terms
term_freq = _index[field][term].tf();
doc_freq = _index[field][term].df();
score += term_freq * doc_freq * q_term_freq;
};
score;
When I run this I get an error `GroovyScriptExecutionException[MissingPropertyException[No such property: terms\u200b for class: Script86.
What is going wrong? AFAIK countBy is valid function.
\u200b is unicode for Zero Width Space.
Rewrite the script or make sure there is no unicode character present with terms.

Resources