Finding Minimum and Maximum
Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 50 of 78.
Arrays have the methods min() and max(), which return the smallest and the largest element:
var temps := [18, 25, 9, 31, 22]
print(temps.min()) # 9
print(temps.max()) # 31The global functions min() and max() compare the numbers you pass them, which is handy for two or three values:
print(max(4, 12, 7)) # 12
var hp: int = min(120, 100) # 100, never above 100Strings are compared character by character, by their Unicode code. Every capital letter comes before every lowercase letter:
var words := ["kiwi", "apple", "Fig"]
print(words.min()) # Fig
print(words.max()) # kiwiThe same rule applies to numbers stored as text: "9" is larger than "56", because the character 9 comes after 5. Convert input with int() before you compare numbers.
On an empty array, both methods return null. To find where the largest value is, pass it to find(): temps.find(temps.max()) is 3. For a dictionary, take the largest of its values and look up its key with find_key(), which returns the first key with that value:
var scores := {"Mia": 120, "Leo": 95, "Ana": 140}
var best: int = scores.values().max()
print(scores.find_key(best), " ", best) # Ana 140max() returns a value of any type, so declare the type of the variable (var best: int) instead of writing :=.
Challenge
EasyThe program reads two lines: whole numbers separated by spaces, and words separated by spaces. Each line has at least one value.
Print the smallest and the largest number, then the smallest and the largest word, with min() and max(). Compare the numbers as numbers, not as text. For the inputs 42 17 23 56 9 34 and kiwi apple banana cherry date, the output is:
Smallest number: 9
Largest number: 56
Smallest word: apple
Largest word: kiwiTry it yourself
extends Node
func _ready():
var number_texts := OS.read_string_from_stdin().split(" ", false)
var words := Array(OS.read_string_from_stdin().split(" ", false))
# Convert the numbers to int, then print the four results
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String MethodsSearching in StringsReplace and TrimSplit and JoinRecap - Username Check4Dictionaries Part 2
Nested DictionariesArrays of DictionariesMerging DictionariesCopying DictionariesRecap - Frequency Counter2Advanced Decision Making
Match With VariablesMatch GuardsArray PatternsDictionary PatternsMembership ChecksRecap - Command Parser5Contact Book Application
Display MenuAdd Contact8Student Records Manager
Project OverviewAdd Student3Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysJagged ArraysCommon 2D Array PatternsRecap - Game Board6Sets with Dictionaries
What is a Set?Dictionary as a SetAdding and RemovingUnion and IntersectionSet DifferenceRecap - Remove Duplicates9Advanced Data Aggregation
Summing ValuesFinding Minimum and MaximumCustom SortingRecap - Dictionary SorterPractice on your own: Online GDScript compiler