compute Average in Bash [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Problem Statement
Given N integers, compute their average, correct to three decimal places.
Input Format
The first line contains an integer N.
This is followed by N integers, each on a new line.
Output Format
Display the average of the N integers, rounded off to three decimal places.
Input Constraints
1 <= N <= 500
-10000 <= x <= 10000 (x refers to elements of the list of integers for which the average is to be computed)
Sample Input
4
1
2
9
8
Sample Output
5.000
explaination
The '4' in the first line indicates that there are four integers whose average is to be computed. The average = (1 + 2 + 9 + 8)/4 = 20/4 = 5.000 (correct to three decimal places) Please include the zeroes even if they are redundant (eg. 0.000 instead of 0).

You can use this awk command:
awk 'NR==1{n=$1;next} {s+=$1} END{printf "%.3f\n", s/n}' file
5.000

Related

I could not understand this question for practicing python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Could anyone please help me in understanding the question for this.
I got this from https://www.geeksforgeeks.org/python-program-for-nth-multiple-of-a-number-in-fibonacci-series/
Given two integers n and k. Find position the n\’th multiple of K in the Fibonacci series.
Examples:
Input : k = 2, n = 3
Output : 9
3'rd multiple of 2 in Fibonacci Series is 34
which appears at position 9.
Input : k = 4, n = 5
Output : 30
5'th multiple of 5 in Fibonacci Series is 832040
which appears at position 30.
You are interested in the numbers that are divisible by k in the fibonacci sequence. Among these numbers, you seek the index of the nth, that is, the index i such that the ith fibonacci number is divisible by k, and such that there are exactly n-1 fibonacci numbers before that one that are also divisible by k.

Can Nested if be applicable to filter dynamically changing range in excel [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
Consider a data table in excel with 6 columns Min, Max, x, 17,18,19 and 3 user input. Output expected is an appropriate cell value to be returned from the table
when
my first userinput value lies within Min and Max(from table)
2nd user input value matching any of the cell in column x
3rd user input matching either 17/18 or 19
For Eg
Sample Data:
Min Max x 17 18 19
-------------------
1 7 0.5 1 2 3/n
8 10 1 2 5 7/n
8 10 2 8 4 9/n
8 10 3 0 7 4/n
11 12 0.5 3 2 1/n
If my user input is (8.4,2,18)
output expected is 4
=INDEX(D2:F6,SUMPRODUCT((A2:A6<=A9)*(B2:B6>=A9)*(C2:C6=A10)*ROW(A2:A6))-ROW(A1),MATCH(A11,D1:F1,0))
Where SUMPRODUCT calculates the row number meeting conditions for min, max and x minus the row number of the header row for correct indexing.
Match calculates the column for indexing.

How Do I Calculate The Difference of Each Element in A List (Python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm new to Python and I wanted to know that how can I subtract All the Elements in A List in Python
For Eg -
list [5,6,7,8,9]
I want to subtract it like this = 5 - 6 - 7 - 8 - 9
Use reduce, which expands out to subtracting all elements.
>>> from functools import reduce
>>> reduce(lambda x, y: x - y, [5,6,7,8,9])
-25
To subtract each number from the first one, you can subtract the sum of them:
>>> mylist = [5,6,7,8,9]
>>> print(mylist[0] - sum(mylist[1:]))
-25

Compare two numbers in Haskell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
http://i.stack.imgur.com/71w9v.png
I have to find how much money a player wins.
This must be done by creating a function called digits Int->Int->Int. For example if the first input is 13758455 and the second input of the function is 13758455 then I should get 1000000 in the compiler.
This is normally an easy task in Java but I wasn't able to find something like a counter to count up as I find more digits in a number.
As there is no state in Haskell, you cannot have counters like you would in imperative languages.
You will often use recursion for this matter. Here is an example:
module Main where
moneyWon :: Int -> Int
moneyWon 8 = 1000000
moneyWon 7 = 100000
moneyWon 6 = 8000
moneyWon 5 = 300
moneyWon 4 = 20
moneyWon 3 = 5
moneyWon 2 = 1
moneyWon _ = 0
digits :: Int -> Int -> Int
digits 0 0 = 0
digits x y = digits dx dy + if mx == my then 1 else 0
where (dx, mx) = divMod x 10
(dy, my) = divMod y 10
main = do
print $ (moneyWon . digits 12345678) 12345668
Notes:
counting corresponding digits and evaluating the prize are two different concerns, that's why I've separated them
the divMod x 10 is a way to get the lowest meaning digit in base 10
in real life, you should not use an integer to hold the digits since it is more a list matter than a number matter (that's why the number starts at 10000000 and not at 0, forcing the user to enter the right number of digits).
Using Int leaves with the task to check the number is valid.

how much string that length is n can we create from A and B that string will not contain two A consecutively [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
how much string that length is n can we create from A and B that string will not contain two A consecutively.
For example;
if n=1 , strings are A ve B => it has 2 solutions
if n=2 , strings are AB, BA, BB => it has 3 solutions
in n=3 , strings are ABB, ABA, BAB, BBA, BBB => it has 5 solutions ... etc.
I need a decrease and conquer algorithm , can you help me please ?
if n=1, result[1] = 2
if n=2, result[2] = 3
if n=3, result[3] = 5
if n=4, result[4] = 8
if n=5, result[5] = 13
so, this generates a Fibonacci sequence.
result[1] = 2
result[2] = 3
result[n] = result[n-1]+result[n-2];
Closed form of Fibonacci sequence:

Resources