Skip to content

Instantly share code, notes, and snippets.

@v6ak
Created January 12, 2011 19:17
Show Gist options
  • Select an option

  • Save v6ak/776686 to your computer and use it in GitHub Desktop.

Select an option

Save v6ak/776686 to your computer and use it in GitHub Desktop.
Generování stromů pro experiment se stromy - kód je místy psán narychlo. S kódem si můžete dělat co chcete, pokud po mě nebudete chtít jakékoli záruky k jeho kvalitám. Záruky k některým jeho nekvalitám poskytnout mohu.
import scala.util.Random
import java.util.NoSuchElementException
class Tree (val food:String, val n:Double, val ord:Int, val children:List[Tree]){
def value = "(\""+food+"\", "+n+")";
override def toString = toString(0, " | ", " -");
def toString(indent:Int, indentString:String, elString:String):String = "["+ord+"]\t"+indentString*indent +elString +value+
children.map("\n"+_.toString(indent+1, indentString, elString)).mkString("")
def length:Int = 1 + children.map(_.length).foldLeft(0)(_+_)
def leaves:Int = children match{
case Nil => 1
case _ => children.map(_.leaves).foldLeft(0)(_+_)
}
def asList:List[Tree] = this :: children.map(_.asList).foldLeft(List[Tree]())(_++_)
}
object Trees{
type Label = (String, Int)
type Labels = List[Label]
object Tr{
def apply(a:Tr*) = new Tr(a:_*);
}
class Tr (children:Tr*){
val n = new Random().nextInt(50)/10.0;
def buildTree(names:Labels):(Labels, Tree) = {
names match{
case first::others => {
var nms = others;
var chl = List[Tree]()
children.foreach{ ch =>
val res = ch.buildTree(nms)
nms = res._1
chl = res._2::chl
}
(nms, new Tree(first._1, n, first._2, chl.reverse))
}
case _ => throw new Exception("Not enough names!");
}
}
def length:Int = 1 + children.map(_.length).foldLeft(0)(_+_)
}
def main(x:Array[String]){
println("""
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
.element{
float: left;
border: 1px solid blue;
width: 40%;
height: 64px;
}
code, pre, hr{
clear: both;
}
.id{
float: right;
}
.page{
page-break-before: always;
}
</style>
""")
val max = 5
val sortings = Map[Int, (String, (Label, Label)=>Boolean)](
1 -> (("podle názvu", _._1<_._1)),
2 -> (("podle konce názvu", _._1.reverse<_._1.reverse))
)
for(i <- 1 to max){
val sorting = try{ sortings(i) }catch{ case e:NoSuchElementException => (null, null)}
val tree = genTree(sorting._2);
println("""<div class="page">""");
println(genHtml(i, tree));
println("<code><pre>#"+i+" "+sorting._1+"\n"+tree+"</pre></code>");
println("</div>");
}
}
val Names = List(
"jablko", "banán", "pomeranč", "jahoda", "broskev", "ananas", "kiwi", "mandarinka",
"hruška", "citrón", "meruňka", "malina", "mango", "meloun", "višeň", "třešeň",
"vanilka", "grep", "švestka", "kokos", "borůvka", "pistácie", "ostružina", "mrkev", "nektarinka", "pomelo",
"petržel", "křen", "celer", "cibule", "kukuřice", "špenát", "okurky", "hrášek", "květák", "česnek", "paprika",
"kapusta", "pórek", "brambora"
);
def genNames() = Random.shuffle(Names)
def genTree(sortFunction:(Label, Label)=>Boolean) = {
val names = genNames();
val structure = Tr(Tr(Tr(), Tr(Tr(Tr(), Tr()))), Tr(Tr(), Tr()), Tr(Tr(Tr(Tr(), Tr())), Tr()));
val numbers = Random.shuffle((1 to structure.length).toList);
val rawLabels = names.zip(numbers);
val labels = if(sortFunction != null) rawLabels.sort(sortFunction) else rawLabels
structure.buildTree(labels)._2;
}
def genHtml(id:Int, tree:Tree)=
tree.asList.map(x =>
"<div class=\"element\">" +
"<div class=\"id\">(#"+id+")</div>"+
"<div class=\"ord\">Tvoje číslo: "+x.ord+"</div>"+
"<div class=\"value\">Tvoje hodnota: "+x.value+"</div>" +
"<div class=\"children\">Tvoji potomci: " +
(x.children match {
case Nil => "Nemáš, jsi list!"
case _ => x.children.map(_.ord).mkString(", ")
}) +
"</div>" +
"</div>"
).mkString("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment