21
1
Intro
We have had histograms and counting, but not listing all of them.
Every year, Dyalog Ltd. holds a student competition. The challenge there is to write good APL code. This is a language agnostic code-golf edition of this year's sixth problem.
I have explicit permission to post this challenge here from the original author of the competition. Feel free to verify by following the provided link and contacting the author.
Problem
The term k-mer typically refers to all the possible substrings of length k that are contained in a string. In computational genomics, k-mers refer to all the possible subsequences (of length k) from a read obtained through DNA Sequencing. Write a function/program that takes a string and k (the substring length) and returns/outputs a vector of the k-mers of the original string.
Examples
[4,"ATCGAAGGTCGT"]
→ ["ATCG","TCGA","CGAA","GAAG","AAGG","AGGT","GGTC","GTCG","TCGT"]
k > string length? Return nothing/any empty result:
[4,"AC"]
→ []
or ""
or [""]
4Does the order of the output matter? When a substring occurs multiple times, should it be repeated in the output? – feersum – 2017-05-01T08:02:17.310
1
Can I return a string of the required substrings separated by newlines instead of an array of strings, like this?
– Leaky Nun – 2017-05-01T08:39:35.683May we also input and output the string as an array of characters (like
['A', 'T', 'C', 'G']
instead of"ATCG"
? – Adnan – 2017-05-01T08:39:56.723Are Dyalog APL answers allowed in this PPCG challenge (because the challenge is also hosted by Dyalog)? – user41805 – 2017-05-01T09:14:49.773
Is it acceptable to return an empty string for the empty result while returning an array of strings for the non-empty result? – Jonathan Allan – 2017-05-01T09:28:26.040
@JonathanAllan Yes. – Adám – 2017-05-01T12:22:41.883
1@feersum Order matters, and repetitions should be repeated. This is just like a sliding window. – Adám – 2017-05-01T12:29:31.687
@LeakyNun Yes, if that would be normal for your language. – Adám – 2017-05-01T12:30:05.743
@Adnan Yes, if that would be normal for your language. – Adám – 2017-05-01T12:30:26.593
@KritixiLithos Yes, because this is [tag:code-golf] while that challenge is for quality code. – Adám – 2017-05-01T12:30:54.787
Related (sliding window on a list) – FlipTack – 2017-12-27T18:16:51.327