r/dailyprogrammer • u/jnazario 2 0 • Mar 05 '18
[2018-03-05] Challenge #353 [Easy] Closest String
Description
In theoretical computer science, the closest string is an NP-hard computational problem, which tries to find the geometrical center of a set of input strings. To understand the word "center", it is necessary to define a distance between two strings. Usually, this problem is studied with the Hamming distance in mind. This center must be one of the input strings.
In bioinformatics, the closest string problem is an intensively studied facet of the problem of finding signals in DNA. In keeping with the bioinformatics utility, we'll use DNA sequences as examples.
Consider the following DNA sequences:
ATCAATATCAA
ATTAAATAACT
AATCCTTAAAC
CTACTTTCTTT
TCCCATCCTTT
ACTTCAATATA
Using the Hamming distance (the number of different characters between two sequences of the same length), the all-pairs distances of the above 6 sequences puts ATTAAATAACT at the center.
Input Description
You'll be given input with the first line an integer N telling you how many lines to read for the input, then that number of lines of strings. All strings will be the same length. Example:
4
CTCCATCACAC
AATATCTACAT
ACATTCTCCAT
CCTCCCCACTC
Output Description
Your program should emit the string from the input that's closest to all of them. Example:
AATATCTACAT
Challenge Input
11
AACACCCTATA
CTTCATCCACA
TTTCAATTTTC
ACAATCAAACC
ATTCTACAACT
ATTCCTTATTC
ACTTCTCTATT
TAAAACTCACC
CTTTTCCCACC
ACCTTTTCTCA
TACCACTACTT
21
ACAAAATCCTATCAAAAACTACCATACCAAT
ACTATACTTCTAATATCATTCATTACACTTT
TTAACTCCCATTATATATTATTAATTTACCC
CCAACATACTAAACTTATTTTTTAACTACCA
TTCTAAACATTACTCCTACACCTACATACCT
ATCATCAATTACCTAATAATTCCCAATTTAT
TCCCTAATCATACCATTTTACACTCAAAAAC
AATTCAAACTTTACACACCCCTCTCATCATC
CTCCATCTTATCATATAATAAACCAAATTTA
AAAAATCCATCATTTTTTAATTCCATTCCTT
CCACTCCAAACACAAAATTATTACAATAACA
ATATTTACTCACACAAACAATTACCATCACA
TTCAAATACAAATCTCAAAATCACCTTATTT
TCCTTTAACAACTTCCCTTATCTATCTATTC
CATCCATCCCAAAACTCTCACACATAACAAC
ATTACTTATACAAAATAACTACTCCCCAATA
TATATTTTAACCACTTACCAAAATCTCTACT
TCTTTTATATCCATAAATCCAACAACTCCTA
CTCTCAAACATATATTTCTATAACTCTTATC
ACAAATAATAAAACATCCATTTCATTCATAA
CACCACCAAACCTTATAATCCCCAACCACAC
Challenge Output
ATTCTACAACT
TTAACTCCCATTATATATTATTAATTTACCC
EDITED to correct the output of the first challenge.
Bonus
Try this with various other algorithms to measuring string similarity, not just the Hamming distance.
2
u/orangejake Mar 05 '18
I think I'm having issues what "closest to all of them" means here. I interpreted it as "Given a list of strings [str_1, str_2, ..., str_n], the distance from str_1 to all the others is d(str_1, str_2) + d(str_1, str_3) + ... + d(str_1, str_n)", where
d(x,y)is the hamming distance of them (or any other distance honestly).But, this viewpoint gets an incorrect answer of
AATATCTACAT(which is distance 19 from all the others) instead ofACATTCTCCAT(which is distance 21 from all the others) on the first input with 4 strings.As I've verified these distance computations "by hand", I'm guessing my interpretation is incorrect. Can anyone help me out?
32
u/olzd Mar 05 '18
Dyalog APL:
Examples:
2
u/jnazario 2 0 Mar 05 '18
FSharp Solution
This is a naive all-pairs distances approach, with O(N2 ) complexity.
8
u/thestoicattack Mar 05 '18 edited Mar 06 '18
C++17. With a bonus, the efficient lattice-based calculation of Levenshtein distance.
7
u/playnwin Mar 05 '18
Powershell First time posting a solution to one of these, just started lurking recently.
2
u/thestoicattack Mar 05 '18
ATTAAATAACTisn't one of the inputs for challenge 1.5
u/jgrindal Mar 05 '18
Python 3.6:
I put the input into a file that I read in and broke the problem down into it's core steps. Comments and suggestions are more than welcome!
8
u/Gprime5 Mar 05 '18 edited Mar 06 '18
Python 3
Essentially a one-liner. Just stick the whole input in as is.
4
u/leonardo_m Mar 05 '18 edited Mar 07 '18
Easy Rust code:
Edit: used read_string.
1
u/octolanceae Mar 05 '18
Challenge #1 says 10 inputs, but, there are 11.
3
u/skeeto -9 8 Mar 05 '18
C, straightforward O(n2). Initially I thought I'd do something clever by parsing the strings into 64-bit integers, XORing them, and using popcount, but I realized this would compute the Hamming distance incorrectly (01 and 10 differ by 2 rather than 1).
1
u/PiousLoophole Mar 05 '18
Python 3. Feedback is appreciated.
2
u/chunes 1 2 Mar 05 '18
Factor
2
u/shadowX015 Mar 05 '18
Solution in Java. Doesn't work for the example input of 4 but waiting on confirmation that the sample output is correct. I thought it would be fun to use this exercise to practice with Streams. It solves both challenge inputs and the first example.
2
u/zqvt Mar 05 '18 edited Mar 05 '18
F#
I think there's a mistake on the 4 line input, the solution should be the
("AATATCTACAT", 19)2
u/[deleted] Mar 05 '18 edited Mar 06 '18
Ruby using hamming distance.
one line version
2
u/[deleted] Mar 05 '18
C, using Hamming distance. Would love feedback from some C veterans.
2
u/thestoicattack Mar 06 '18 edited Mar 06 '18
Haskell.
2
u/GrapePowerade Mar 08 '18
Python3:
Any comments or suggestions?
2
u/nikit9999 Mar 08 '18
C#
2
u/TheoreticallySpooked Mar 14 '18
CoffeeScript using Hamming Distance
Output for the 21 line challenge
1
u/engageant Mar 05 '18
Posh
Basically a Powershell adaptation of /u/jgrindal 's code below.
1
u/Godspiral 3 3 Mar 05 '18
in J, I assume challenge involves finding mode at each position. ie. distance is either 0 (for match) or 1 (no match).
above is "best" string regardless of whether it exists in input.
the first string that has the most in common with the "best string"
1
u/clawcastle Mar 05 '18
C# Solution. I don't exactly know what complexity this is, maybe O(n*log(n))? Haven't yet tried anything other than the Hamming distance, but I went with injecting the interface to allow for other distance calculations in the future. Criticism is more than welcome!
1
u/octolanceae Mar 05 '18
C++
Output
1
u/tomekanco Mar 05 '18 edited Mar 07 '18
First Java ever. I have been spoiled by Python: Sad panda wants operator overloading.
1
u/TheMsDosNerd Mar 05 '18
Python 3: I could not fit it on one line.
1
u/fabikw Mar 05 '18
Solution in R
The input is given as a character vector.
1
u/Scara95 Mar 05 '18
Q
example:
Adding (semplicistic) input from file:
1
u/popillol Mar 06 '18
Go / Golang Playground Link
1
u/TiZ_EX1 Mar 06 '18
Crystal. Syntax highlighted.
1
u/TiZ_EX1 Mar 06 '18
D. Syntax highlighted.
1
u/minikomi Mar 06 '18 edited Mar 06 '18
oK:
Output:
Run it on the REPL: URL
1
u/DEN0MINAT0R Mar 06 '18
Python 3
I changed the problem a bit, and invented my own metric for string similarity. I don't know if it has a more formal name, but it doesn't seem to match any of the descriptions in the linked article. It essentially finds the most common term for each position in the strings, and finds the string with the most terms that match the most common term for that position. Anyways, here's the code.
Output
The outputs don't match the challenge outputs exactly, presumably due to the different measure I used; however, everything seems to be working as expected.
1
u/MakingTheEight Mar 06 '18 edited Mar 06 '18
Python 3.6
The input files are provided as the argument
input_fileon the terminal.Feedback welcome.
1
u/gabyjunior 1 2 Mar 06 '18 edited Mar 06 '18
C
Complexity is O(N), N being the number of input chars.
The number of words is not needed in the input and the program may work on a list of words with variable length (the words shorter than the maximum length are considered right-padded with CR/LF).
First the program computes the frequency of each character column by column, then computes the score of each word (the sum of its characters frequency). The closest word is the one with the highest score.
Output
EDIT Also tried with Daily Programmer's popular list of words: enable1.txt
1
u/conceptuality Mar 06 '18
HASKELL:
Fairly quick implementation, each pair wise distance is only computed once. Even the 21 challenge is instanteneous even in ghci.
code:
run it after loading as:
1
u/gentlegoatfarmer Mar 06 '18
Scala
1
u/hellajacked Mar 07 '18
C++ Only recently learned C++, so feel free to give some input on my code and where it could be improved!
1
u/Kendrian Mar 07 '18
Leaving out code to read input, a very short program in Julia:
However, this is neither the most efficient solution in time or space complexity (we don't need an array, and we don't need to compute the distance to every other string for every string). Instead, we can store just the distance from string i to strings i+1 to n and look up distances for strings 1 to i-1. The code with this algorithm is much uglier but also twice as fast; since it's written at a lower level it allocates a lot less, too.
This optimized version I measured at < 100 microseconds for the biggest input.
1
u/crudkin Mar 08 '18
Python 3.6:
1
u/zatoichi49 Mar 08 '18 edited Mar 08 '18
Method:
Create an array by splitting the input string into a list of lists. Transpose the array so that the columns switch to rows, and calculate the Hamming distance for each letter in the row. Transpose the array back to its original position (replacing each letter by the Hamming distance), and sum each row to get the total Hamming distance for each string in the sequence. Find the index of the string with the minimum distance, and return the string.
Python 3:
Output:
1
u/h2n0954 Mar 08 '18
bash
1
u/drewfer Mar 08 '18 edited Mar 08 '18
Rust solution
EDIT: Formatting
1
u/Rock_Crusher Mar 09 '18
C#
A quick, simple implementation. O(n2)
1
u/ff8c00 Mar 09 '18
JavaScript Gist
1
u/tet5uo Mar 09 '18
Haven't done these in a long time, so really rusty. But I want to get back into learning programming.
Here's my attempt in c#
1
u/vivitsum Mar 09 '18
Simple (maybe too simple) C solution - O(n2)
EDIT: Removed unnecessary comments
1
u/ybham6 Mar 09 '18
Python 3.6:
basically one line, the input is on a different line rn
Feedback would be great
1
u/[deleted] Mar 09 '18
C# with challenge
It's a bit ugly, but meh it works
Output:
1
u/BlasphemousJoshua Mar 10 '18
Swift 4
1
u/den510 Mar 11 '18
Ruby
I have only done the Hamming distance, but may try the others later on. Gist with some unit tests here.
edit: include Gist link
1
u/pancakeflippersunite Mar 11 '18
Java.
1
u/OutputStream Mar 11 '18 edited Mar 11 '18
Feedback welcomed! Just starting to learn Go.
Concurrent solution in Go/Golang:
1
u/zookeeper_zeke Mar 13 '18
Here's a solution in C:
1
u/[deleted] Mar 13 '18
(mostly) functional python 3
1
u/primaryobjects Mar 13 '18
R
Gist | Demo
Output
1
u/DeathAdder10 Mar 15 '18
First Submission, Hopefully I've done all the formatting right!
Code:
Output:
1
u/mmoneyinthebank Mar 19 '18
1
u/brianbarbieri Mar 21 '18 edited Mar 21 '18
My answer in Python 3.6:
It would be awesome to receive some feedback on how to improve my programming!
EDIT: See a lot of methods that make use of hamming distance, any reason my method is less efficient?
1
u/[deleted] Mar 21 '18
A bit late to the party but here's my solution in python 2.7
1
u/MohamedElshazly Mar 23 '18 edited Mar 23 '18
Python3
Would love feedback!!
1
u/Tetsumi- 1 0 Mar 27 '18
Racket
1
u/Bob_Dunbar Apr 02 '18
Java 😎
1
u/sha3bani Apr 06 '18
C++
1
u/[deleted] Apr 08 '18
F# way late to the party, but I've been slacking and I'm trying to catch up
Running
test()in FSI.exe results in the following output:1
u/zahid3160 Apr 12 '18
Javascript without bonus Feedback for improvements are welcomed
1
u/[deleted] Apr 16 '18
JAVA
1
u/[deleted] Apr 17 '18
Python 3
1
u/roxastheman Apr 17 '18
C#
I didn't bother doing the file I\O since I wrote unit test for validation.
1
u/gabeace Jun 07 '18
C#
Didn't do the input, but you know how that goes. This one did take me a bit to wrap my head around. Once I get to a third nested for loop I start to question my life choices, but I didn't see a more obvious way of doing this and I like that it's easy to follow what's happening... just check each string against every other string, letter by letter, and count if they're different, then compare the final tallies.
0
u/[deleted] Mar 05 '18
[deleted] — view deleted comment