Sembra un lavoro per il linguaggio di programmazione preferito di tutti, R!
library(XML)
tables <- readHTMLTable('http://www.uesp.net/wiki/Skyrim:Ingredients',
stringsAsFactors=FALSE)
potions <- tables[[1]]
twoway <- data.frame(t(combn(potions$Name,2)))
threeway <- data.frame(t(combn(potions$Name,3)))
BAM!
> head(twoway)
X1 X2
1 Abecean Longfin Bear Claws
2 Abecean Longfin Bee
3 Abecean Longfin Beehive Husk
4 Abecean Longfin Bleeding Crown
5 Abecean Longfin Blisterwort
6 Abecean Longfin Blue Butterfly Wing
> head(threeway)
X1 X2 X3
1 Abecean Longfin Bear Claws Bee
2 Abecean Longfin Bear Claws Beehive Husk
3 Abecean Longfin Bear Claws Bleeding Crown
4 Abecean Longfin Bear Claws Blisterwort
5 Abecean Longfin Bear Claws Blue Butterfly Wing
6 Abecean Longfin Bear Claws Blue Dartwing
utilizzare il comando write.csv
per salvare le tabelle come file CSV.
/Modifica: Per spiegare cosa sto facendo: Il pacchetto XML contiene la funzione readHTMLTable, che estrae tutte le tabelle html da un sito Web come data.frames e le salva come elenco. La prima tabella in questa lista è quella che vogliamo. La funzione combn trova tutti i modi a 2 vie, 3 vie e n combinations di nomi di pozioni e restituisce il risultato come matrice. Io uso la funzione t per trasporre questa matrice, quindi ogni combinazione è una riga e quindi convertirla in un frame di dati. Questo si estende facilmente alle combinazioni di n ingredienti.
/Modifica 2: Ho scritto una funzione per salvare la tabella n-way in un file csv specificato dall'utente. L'ho anche rielaborato un po ', perché la trasposizione di enormi matrici è computazionalmente costosa. Questa versione dovrebbe consentire di calcolare la tabella a 4 vie, anche se richiede molto tempo e non so se è rilevante per il gioco.
nway <- function(n, filepath, data=potions) {
nway <- combn(data$Name, n, simplify = FALSE)
nway <- do.call(rbind,nway)
write.csv(nway,filepath, row.names=FALSE)
}
nway(4,'~/Desktop/4way.csv')
/Edit 3: Ecco alcuni codice per trovare le pozioni di lavoro effettivi. Non è molto efficiente e probabilmente può essere notevolmente migliorata:
#Given an ingredient, lookup effects
findEffects <- function(Name) { #Given a name, lookup effects
potions[potions$Name==Name,3:6]
}
#2-way potions
intersectTwoEffects <- function(x) {
Effects1 <- findEffects(x[1])
Effects2 <- findEffects(x[2])
Effects <- unlist(intersect(Effects1,Effects2))
Effects <- c(x[1],x[2],Effects)
length(Effects) <- 6
names(Effects) <- NULL
c(Effects,sum(is.na(Effects)))
}
twoway <- lapply(twoway,intersectTwoEffects)
twoway <- do.call(rbind,twoway)
twoway <- twoway[twoway[,7]<4,-7] #remove combos with no effect
write.csv(twoway,'~/Desktop/twoway.csv',row.names=FALSE)
#3-way potions
intersectThreeEffects <- function(x) {
Effects1 <- findEffects(x[1])
Effects2 <- findEffects(x[2])
Effects3 <- findEffects(x[3])
Effects <- c(intersect(Effects1,Effects2),intersect(Effects1,Effects3),intersect(Effects2,Effects3))
Effects <- unlist(unique(Effects))
Effects <- c(x[1],x[2],x[3],Effects)
length(Effects) <- 8
names(Effects) <- NULL
c(Effects,sum(is.na(Effects)))
}
threeway <- lapply(threeway,intersectThreeEffects)
threeway <- do.call(rbind,threeway)
threeway <- threeway[threeway[,9]<5,-9] #remove combos with no effect
write.csv(threeway,'~/Desktop/threeway.csv',row.names=FALSE)
(+1) la risposta di Nizza. – chl
+1 per una risposta utile, ma sta generando ogni combinazione in realtà un modo efficace per trovare le ricette di pozioni di lavoro? –
@David B: Cosa pensi che sarebbe un approccio più efficiente? – Zach