Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Created April 23, 2016 22:46
Show Gist options
  • Save lovasoa/55c878da3eabd8545bfc2201c81ee7e8 to your computer and use it in GitHub Desktop.
Save lovasoa/55c878da3eabd8545bfc2201c81ee7e8 to your computer and use it in GitHub Desktop.
Construit un tableau depuis les résultats de clingo.
<html>
<head>
<meta charset="utf8" />
<style>
table {
border-collapse: collapse;
border-spacing: 0px;
}
td {
padding: 5px;
border: 1px solid black;
}
</style>
</head>
<body>
<table id="res"></table>
<textarea id="t"></textarea>
<button onclick="mkTab()">tableau</button>
<script>
var area = document.getElementById("t");
var htable = document.getElementById("res");
function mkTab (){
var s = area.value;
var re = /participe\(([^,]+),([^,]+),([^\)]+)/g;
var ps;
var tours = [], tables = {};
while(ps = re.exec(s)) {
let equipe = ps[1], tour = parseInt(ps[2])-1, table = ps[3];
if (!tours[tour]) tours[tour] = {};
if (!tours[tour][table]) tours[tour][table] = {};
tours[tour][table][equipe] = true;
tables[table] = true;
}
console.log(tours);
var tb = "<thead><tr><th>tour\\table</th>" +
orderKeys(tables).map(h => "<th>"+h+"</th>").join("") +
"</tr></thead>";
tours.forEach((otable, tour) => {
tb += "<tr>";
tb += "<th>"+(tour+1)+"</th>";
forEachOrder(tables, (table) => {
let equipes = otable[table] || {};
tb += "<td>" + orderKeys(equipes).join("/") + "</td>";
});
tb += "</tr>";
});
htable.innerHTML = tb;
}
function orderKeys(o) {
return Object.keys(o).sort();
}
function forEachOrder(o, fun){
return Object.keys(o).sort().forEach(k => fun(k, o[k]));
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment