English 中文(简体)
simple logic question comparing variables
原标题:
  • 时间:2009-12-17 20:19:06
  •  标签:
  • logic
  • max

If I have variables with numbers how to figure out which are the three with the highest value?

最佳回答

I don t want to say that I m a newb, because I don t think I am. However this question was rather difficult to answer. I ve only been coding PHP for around 8 months, and I imagine that there is a much better way of accomplishing such an effect. Based on my skill set I choose to use PHP and came up with this:

<?php

$variable = array();
$variable[1] = 15;
$variable[2] = 30;
$variable[3] = 9;
$variable[4] = 86;
$variable[5] = 46;
$variable[6] = 12;
$variable[7] = 86;

## Clean the array of duplicates
$variable = array_unique($variable);

## Sort array from greatest to lease in DESC order
rsort($variable);


for ($i = 0; $i < 3; $i++):
    echo $variable[$i]."<br />";
endfor;

?>

The breakdown

  • First declare your array
  • Then your array items
  • Clean the array with "array_unique" to remove reiterated values
  • Use "rsort" to sort array from greatest to least
  • Initiate a for loop in which $i equals 0, run only while $i is less then 3, add one to $i for each iteration
  • Echo the array item

Good luck!

问题回答

You can either manually iterate through them and extract the top 3 (maintaining the information you have so far in some form) or just sort them all and take the top 3

This really depends on the language you re using.

I would suggest putting them all in an array, sort them from highest to lowest, and then the first three elements would be your highest.

You may sort them in descending order and the first three will be the ones you want.

Don t forget that there are a couple odd cases here:

  1. A case where the highest value occurs more than 3 times so that there isn t just one correct answer. For example, if there a dozen variables all with a value of zero.

  2. If you have less than 3 variables for such a program there could be a problem.





相关问题
Prolog difference routine

I need some help with a routine that I am trying to create. I need to make a routine that will look something like this: difference([(a,b),(a,c),(b,c),(d,e)],[(a,_)],X). X = [(b,c),(d,e)]. I really ...

Python: Analyzing complex statements during execution

I am wondering if there is any way to get some meta information about the interpretation of a python statement during execution. Let s assume this is a complex statement of some single statements ...

Python nested lists and recursion problem

I m trying to process a first order logic formula represented as nested lists and strings in python so that that its in disjunctive normal form, i.e [ & , [ | , a , b ], [ | , c , d ]] ...

prolog - why this strange trace

here is the prolog code (which i sort of follow). len([],0). len([_|T],N) :- len(T,X), N is X+1. and here is the trace for it (im running linux, swi) [trace] ?- len([d,f,w,c],X). Call: (7) ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

how to create a parser for search queries

for example i d need to create something like google search query parser to parse such expressions as: flying hiking or swiming -"**walking in boots **" **author:**hamish **author:**reid or ...

Applications of Unification?

What are the (practical) applications of Unification? Where it is actually being used in real world? I couldn t understand the whole idea of what it is really about and why it s considered as a part ...

热门标签