Testing for highest numerical value in list of items

Sorry for the perhaps confusing title, but essentially what I am doing is testing a list of things held by the player, each of which are of the same kind and have a numerical value attached to them. I am looking for the thing that has the highest such value.

Strangely enough, I seem to have figured this out on my own. So my question is not how to do it, but whether the solution I have come up with is the most efficient one and/or does not contain some pitfall that I am missing.

Here is the code:

A gadget is a kind of thing. A gadget has a number called complexity. The complexity is usually 1.

Understand the command "evaluate" as something new. Evaluating is an action applying to nothing. Understand "evaluate" and "eval" as evaluating.

Carry out evaluating:
	if the player carries a gadget:
		let the previous rating be 0;
		let the chosen one be nothing;
		Repeat with item running through gadgets held by the player:
			let current rating be complexity of item;		
			if current rating is greater than previous rating:
				now previous rating is current rating;
				now the chosen one is item;
		let stash be the number of gadgets held by the player;
		say "You are carrying a total of [stash in words] gadget[s], [if stash is greater than 1]the most complex of which is [end if][the chosen one] (complexity level [complexity of the chosen one]).";
	otherwise:
		say "You are not carrying any gadgets."
		
The Gadgetorium is a room. "Here is where you keep all your gadgets, from the least to the most complex." The blorg, the zeret, the klaxta, the mip, and the weecog are gadgets in the Gadgetorium. The complexity of the zeret is 4. The complexity of the klaxta is 2. The complexity of the mip is 3. The complexity of the weecog is 5.

test me with "eval / take blorg / eval / take zeret / eval / take all / eval".

(I am aware that the above code will not distinguish between two gadgets of equal complexity. So, for example, if I had two gadgets with a complexity of 5, the result here would be whichever gadget comes later in the list. I did briefly consider refining the code to take this into account, but for what I am using this for I will not need such a distinction: Each item will have a different value attached to it.)

I gotta admit that I’m kind of chuffed to have figured this out myself. But I also know that the regulars here are far more informed (ahem) than I am and will no doubt be able to point out places where this code can be improved, either in terms of efficiency or coding practices.

Thanks in advance for any pointers.

If you want to run through the loop and do the computation, that’s the right way to do it. You aren’t missing any serious efficiency bets.

(Oh, you could increment a counter variable instead of evaluating “the number of gadgets held by the player” a second time. But this is a very minor improvement.)

However, it turns out that Inform has a built-in feature for finding the highest/lowest property in a collection. See chapter 15.17. It would look something like this:

Definition: a gadget is complex if its complexity is 3 or more.

	if the player carries a gadget:
		let G be the complexest gadget held by the player;
		say "Complexity of [G] is [complexity of G].";
		[...and so on...]

“3” is an arbitrary value here. It means that some of the gadgets “are complex” and others aren’t, but you don’t have to use that fact. What’s useful is that the compiler magically understands “complexer” and “complexest” as comparatives.

(Not, unfortunately, “more complex” / “most complex”. It’s not that magical.)

For what it’s worth, I have a uservoice suggestion to allow the author to specify “more complex” and “most complex” when defining “complex” as a scalar adjective. More votes would be welcome!

Thanks for the tips, zarf!

Ah, good catch. That would be more efficient.

Well, that is indeed handy. You know, when I was writing the code above I kept thinking that I had once seen something that would be a lot easier, but I couldn’t remember where. Now I know. I will have to give that a whirl later when I get the chance.

Thank you again!

You now have more votes. :smiley: