|
- # Josua Kugler, Christian Merten
-
- library(tidyverse)
-
- `%o%` <- function(f, g) function(...) f(g(...))
-
- # ich bin mal so dreist und lese das gesamte file direkt ein
- # wenn hier unbedingt read_lines verwendet werden soll, dann füge
- # man eben zuerst mit paste alle zeilen wieder zusammen
- lines <- read_file("books.txt")
-
- sort_char <- function(s) paste(sort(unlist(str_split(s, ""))), sep="", collapse="")
-
- cleared <- str_match_all(str_to_upper(str_replace_all(lines,
- pattern="\\(.*?\\)|[^a-zA-Z0-9]",
- replacement="")),
- "([A-Z]+)([0-9]+)")[[1]]
-
- unsorted_data <- tibble(category = sapply(cleared[,2], sort_char),
- count = as.integer(cleared[,3]))
-
- data <- unsorted_data[order(unsorted_data[,1]),]
-
- books_of_category <- function(data, cat_let) {
- apply(data[str_detect(data$category, cat_let), ], 1,
- function(x) str_glue("We have {x[2]} books of category {x[1]}"))
- }
|