r/dailyprogrammer • u/jnazario 2 0 • May 10 '17
[2017-05-10] Challenge #314 [Intermediate] Comparing Rotated Words
Description
We've explored the concept of string rotations before as garland words. Mathematically we can define them as a string s = uv is said to be a rotation of t if t = vu. For example, the string 0011001 is a rotation of 0100110, where u = 00110 and v = 01.
Today we're interested in lexicographically minimal string rotation or lexicographically least circular substring, the problem of finding the rotation of a string possessing the lowest lexicographical order of all such rotations. Finding the lexicographically minimal rotation is useful as a way of normalizing strings.
Input Description
You'll be given strings, one per line. Example:
aabbccddbbaabb
Output Description
Your program should solve the lexicographically minimal string rotation and produce the size of the substring to move and the resulting string. Example:
10 aabbaabbccddbb
Which is, in Python parlance, "aabbccddbbaabb"[10:] + "aabbccddbbaabb"[:10].
Challenge Input
onion
bbaaccaadd
alfalfa
weugweougewoiheew
pneumonoultramicroscopicsilicovolcanoconiosis
Challenge Output
2 ionon
2 aaccaaddbb
6 aalfalf
14 eewweugweougewoih
12 amicroscopicsilicovolcanoconiosispneumonoultr
2
u/thorwing May 10 '17
Java 8
not really that hard for a "medium" challenge to be honest.
9
u/Unbelievabob May 10 '17 edited May 11 '17
Prolog
6
u/olzd May 10 '17 edited May 11 '17
Dyalog APL
edit: kinda step by step decomposition
{(⌽∘⍵)¨⍳⍴⍵}'onion'
{⊃⍋↑⍵}'niono' 'ionon' 'ononi' 'nonio' 'onion'
'onion'{⍵(⍵⌽⍺)}2
1
u/jnazario 2 0 May 10 '17 edited May 10 '17
F# solution
5
u/esgarth May 10 '17
scheme
4
u/MattieShoes May 10 '17
Simple C++ solution
Output:
3
u/gabyjunior 1 2 May 10 '17 edited May 13 '17
C
O(N) solution
EDIT 3
Source code of new version below to fix the issues pointed out by /u/kalmakka. It is implementing decomposition of string into Lyndon words, the last one found is the start of the lexicographically minimal rotation.
3
u/J_Gamer May 10 '17
C++
3
u/curtmack May 11 '17
C++11
I tried to make as much use of
stdas possible.3
u/allenguo May 11 '17
OCaml solution (brute-force):
2
u/alchzh 0 1 May 10 '17 edited May 12 '17
+/u/Compilebot python3
Doesn't produce size of substring, thinking of short way to do that2
u/Scroph 0 0 May 10 '17 edited May 10 '17
+/u/CompileBot D
Input:
2
u/fsrock May 10 '17
Why does "bbaaccaadd" become "aaccaaddbb" and not "aaddbbaacc" ? isnt "aadd" smaller than "aaccaadd" or am i getting this all wrong?
2
u/zatoichi49 May 10 '17 edited May 10 '17
Method:
Create a list of the concatenations of all subset rotations in the word, printing the minimum value in the list (which will also be the first in lexicographic order) and the subset length.
Python 3:
Output:
2
u/a_Happy_Tiny_Bunny May 10 '17
Haskell
2
u/goodygood23 May 10 '17
+/u/CompileBot R
2
u/Godspiral 3 3 May 11 '17
in J,
2
u/ReverendRocky May 11 '17
Prolog
I'm going to attempt a more efficient way to accomplish this tomorrow and submit it in a language I'm more comfortable in like Python or C
1
u/fsrock May 10 '17
JAVA
1
u/[deleted] May 10 '17 edited May 10 '17
Rust 1.17
Feedback welcome. Brute forces in O(n2) time. Not sure if there's a better way to do it.
1
u/Executable_ May 10 '17
Python3
+/u/Compilebot python
1
u/mrploszaj May 10 '17
D solution that creates all possible answers and finds the best one through comparison. Could have been done better or prettier but I wanted to fit it all on the one line so declaring i in the arguments and building the string with its index had to be done.
1
u/ChazR May 10 '17
Haskell
This is less efficient than it could be. I wrote allRotations to be simple. It would be better for this brief to maintain a counter as we create the rotations. Something like:
and cons up a list of those, then use a sortBy. But, hey, it's short and it works.
Having to use elemIndex and then hacking in a fromJust is messy. I might do a cleaner version later.
1
u/skeeto -9 8 May 10 '17 edited May 10 '17
C, brute force O(n2), and using wide characters just for practice, though this won't really work correctly for combining characters and such.
1
u/we_swarm May 11 '17
Elm with sandbox
1
u/popillol May 11 '17
Go / Golang Playground Link.
Code:
1
u/YetiEric May 11 '17
Qt (C++)
First time posting here!
I'm not quite familiar with the C++ std librairy, so I'll be using the Qt Framework instead.
I tried not to abuse the framework and used its classes only as containers (i.e. sorting a list of strings is a function call...)
Outputs :
I also did the linked Garland word :
Outputs :
1
u/nuri0 May 11 '17
Javascript
Neither fastest nor prettiest one, but fairly simple solution: Create all suffixes for the given string and sort them with the first being the one to rotate for.
1
u/shoffing May 11 '17
Scala
1
u/jeaton May 11 '17 edited May 11 '17
C
Python
1
u/InKahootz May 11 '17
+/u/CompileBot C#
Input
1
u/gHx4 May 12 '17 edited May 12 '17
Python 3
Here's a solution that first prunes the string for promising places to begin. Feel free to let me know if there's any room for improvement. I'm not exactly sure how clean codegolf needs to be and I try to adapt my coding style to fit the level of sophistication required.
Output:
1
u/benz05 May 13 '17
Python 3 solution (doubled). My first attempt was to write a function, but then with some inspiration from here I realised the function could be replaced by a one-liner. Thanks for the problem, I'm new to this all, and this is a great way to learn.
Output:
1
u/[deleted] May 13 '17
Python 3
Output:
1
u/numbersnletters May 15 '17 edited May 15 '17
+/u/CompileBot Go
Input:
1
u/guatsf Jun 07 '17
R
Comments/opinions much appreciated. +/u/Compilebot R
1
u/Vultyre Jun 11 '17
Haskell
Here is my late (and somewhat wrong) submission. I misread the problem and thought that we were to find the minimum rotation steps either forward or backward, not just forward. Anyway, here it is:
Code
Input and Output
1
u/ChemiCalChems Jul 10 '17
+/u/Compilebot c++ --date --time
1
u/congratz_its_a_bunny May 10 '17
python
0
u/[deleted] May 15 '17
[deleted] — view deleted comment