Shell script for solving the math question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script for solving the math question
# 1  
Old 06-21-2015
Shell script for solving the math question

Can such Puzzle solve through UNIX script? if yes, what could be the code?
This has been solve in C language. we were trying to solve this through shell but could not because of not able to pass 1st argument with multiple value. we are not expert in unix scripting. Below is the puzzle

John is a papaya merchant. He visits different papaya orchards and collects papayas from farmers in the fruit containers. Each farmer could provide different quantity of papayas. John is smart and labels the number of papayas collected in each container. All the containers are of the same size and capacity.

At the end of the day, when John returns from this fruit collection tour, he has to ensure that all the containers carry same number of papayas so that they can be subsequently shipped to his various distribution channels. It is important to equi-distribute the number of papayas in each container for efficient logistics and keep his distributors happy.

The question is – Given n containers with different number of papayas say q1, q2,..qn in each container, how many papayas will John have to move and what is the least possible number of moves it will take him to ensure that papayas are equally distributed. Obviously, he is tired from his day long work and he would prefer to move minimum possible papayas in least number of moves. (one move consists of transferring any number of papayas from one container to another one)

The input consists of number of papayas in each container separated by space.

Expected Output : Number of papayas moved and number of moves. (if it is not possible to equally distribute the papayas, then the output should be -1)

Code:
Example1

Command line program Input: 2 1 4 3 5

Program output: papayas:3 Moves: 2

Explanation:

John opens container with 5 papayas and moves 2 papayas to container that originally had 1 papaya.  (move 1)
He opens container with 4 papayas and moves 1 papaya to container that originally had 2 papayas. (move 2)

Code:
Example2

Input: 2 1 4 3

Output: -1

Explanation:

Since there are total of 10 papayas and 4 containers, it is not possible to equally distribute the papayas in all containers.


Moderator's Comments:
Mod Comment code tags please, not HTML

Last edited by vbe; 06-21-2015 at 05:30 AM..
# 2  
Old 06-21-2015
Anything that can be solved by an algorithm can be implemented by any Turing complete programming language, which the shell certainly is.

Please show the algorithm already used or its C implementation.
# 3  
Old 06-21-2015
Sounds very much like homework.
Please follow the guidelines for posting homework question.
See: Rules for Homework & Coursework Questions Forum
# 4  
Old 06-21-2015
Thanks Sea for your update.
I am trying to remove this thread from this forum but not seeing any option.
I will start this thread as suggested by you.
# 5  
Old 06-21-2015
Moderator's Comments:
Mod Comment This thread is closed.

We will expected to see a new thread from this submitter in the Homeword & Coursework Forum with a completely filled out homework questionnaire.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bit of a math question

I have a number, say 174. I need to write bash code that will find the first larger number that ends in 99. That would be 199 in this case. If the number were 1263, I would be looking for 1299, for 175438, I would want 175499, etc. If the numbers were always three digit, I could just grab the... (11 Replies)
Discussion started by: LMHmedchem
11 Replies

2. UNIX for Dummies Questions & Answers

Variables and math in Old skool Bourne Shell

Hey everybody, I've been searching google and these forums and have found some solutions to the issues I've been having today within the OLD Bourne Shell. I am following chapter 6 of the Guide to Unix using Linux 4th Edition. I am working on some basic calculations with variables in the BASH... (3 Replies)
Discussion started by: mr.rhtuner
3 Replies

3. Shell Programming and Scripting

Math calculation over shell

Hi I am trying to calculate the rate at which something is happening. I have 2 files- a1 and b1. I want to calculate something like this ((wc -l a1)/(wc -l a1 + wc -l b1))*100 over a loop for different a and b. Is this possible, help me out fellas. Thanks a lot :) (5 Replies)
Discussion started by: jamie_123
5 Replies

4. Shell Programming and Scripting

A Math problem using shell script

Have a bit complicated math query .. Basically i am given a number which is > 50 .. I am suppose to find the calculation to get a number which is equal or more than the input number and is also a multiple of any number between 20 - 30 . For example . Input number is 60 . Now 20x3 =60 ... (2 Replies)
Discussion started by: greycells
2 Replies

5. Shell Programming and Scripting

Script math calculation

Hi Gurus, I'm currently using HP-UX B.11.23. I've a simple calculation script which performs the task below. -> echo "240021344 / 1024 /1024" | bc Output: 228 240021344 is KB value. When I tried to perform the same calculate in Ms Excel, it produces a different result: 228.9021912.... (12 Replies)
Discussion started by: superHonda123
12 Replies

6. UNIX for Dummies Questions & Answers

Can Any help me with the math on this shell script?

Develop a grade calculating program. This program will process all students in the file. This program should neatly display each field of each student's record *and* adds the following items: Course Average and Letter Grade. The course average is calculated by the following weights: 50% for quiz... (7 Replies)
Discussion started by: knp808
7 Replies

7. UNIX for Dummies Questions & Answers

Linux Shell Question: how to print the shell script name ?

Suppose I have a script named "sc.sh" in the script how to print out its name "sc.sh"? (3 Replies)
Discussion started by: meili100
3 Replies

8. UNIX for Dummies Questions & Answers

Simple Math question...

Hi All , I'm trying to do a simple math expression ...but unsuccessfull :-( Anyone can help me? days=23 amount=`expr ${days} / 30 \* -125` echo $amount but as result i got 0 when i expect 95.833333 Another question...how i can limit only to two or three decimal fields? Thanks in... (1 Reply)
Discussion started by: EDBGSK
1 Replies

9. Shell Programming and Scripting

a math related question

hello: First I know the rules on Homework related questions, I wrote my script, but I cannot seem to figure out how to do one math problem. How do I take a zip code and seperate the idvidual digits? I used the modulus expression and divided the number by 10 ^ n but that only worked... (9 Replies)
Discussion started by: jahjah
9 Replies
Login or Register to Ask a Question