Skip to content

Instantly share code, notes, and snippets.

@pvsousalima
Created October 7, 2014 21:34
Show Gist options
  • Save pvsousalima/0dd162845241efed0085 to your computer and use it in GitHub Desktop.
Save pvsousalima/0dd162845241efed0085 to your computer and use it in GitHub Desktop.
Missouri's State CSC-333 Warmup to Project 1 using TCL language
set comment {
PROBLEM B: GNOME SEQUENCING
In the book All Creatures of Mythology, gnomes are kind, bearded creatures, while goblins tend to be
bossy and simple-minded. The goblins like to harass the gnomes by making them line up in groups of three,
ordered by the length of their beards. The gnomes, being of different physical heights,
vary their arrangements to confuse the goblins. Therefore, the goblins must actually measure the beards
in centimeters to see if everyone is lined up in order.
Your task is to write a program to assist the goblins in determining whether or not
the gnomes are lined up properly, either from shortest to longest beard or from longest to shortest.
Input: The input starts with line containing a single integer N, 0 < N < 30, which is the number of groups to process.
Following this are N lines, each containing three distinct positive integers less than 100.
Output: There is a title line, then one line per set of beard lengths.
Sample (input):
8
40 62 77
88 62 77
91 33 18
45 95 35
50 10 80
33 88 55
99 98 97
1 2 3
Sample (output):
Gnomes:
Ordered
Unordered
Ordered
Unordered
Unordered
Unordered
Ordered
Ordered
}
# Compare three values in list
proc compare {a} {
# Set first value
set A [lindex $a 0]
# Set second value
set B [lindex $a 1]
# Set third value
set C [lindex $a 2]
if {($A > $B && $B > $C) || ($A < $B && $B < $C)} {
return "Ordered"
} else {
return "Unordered"
}
}
# Open file in read mode
set input [open "ripoff.in" r]
# read the file content
set file_data [read $input]
# split the data in lines
set data [split $file_data "\n"]
# Format output
puts "Gnomes:"
# Foreach line compare values
foreach line $data {
set len [llength $line]
if {$len > 1} {
set flag [compare $line]
puts "$flag"
}
}
# Close file
close $input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment