23
\$\begingroup\$

I'm sure most, if not all, of you have come across this at some point or another:

Aoccdrnig to a rscheeachr at Cmabrigde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer be at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe.

  • Create a program that inputs any amount of text. For testing purposes, use the unscrambled version of the above text, found below.

  • The program must then randomly transpose the letters of each word with a length of 4 or more letters, except the first and last letter of each word.

  • All other formatting must remain the same (capitalization and punctuation, etc.).

Testing text:

According to a researcher at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place. The rest can be a total mess and you can still read it without problem. This is because the human mind does not read every letter by itself but the word as a whole.

As usual, this is a code-golf. Shortest code wins.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Similar to How to randomize letters in a word, though in that one only a single word needs to be scrambled whereas here it's every word in a sentence. \$\endgroup\$
    – Gareth
    Commented Dec 20, 2012 at 16:44
  • \$\begingroup\$ I agree. The questions are similar enough that solutions for one problem can be used almost directly for the other. \$\endgroup\$
    – primo
    Commented Dec 20, 2012 at 17:25
  • 3
    \$\begingroup\$ Last letter is not right in rscheearch in your sample text. \$\endgroup\$
    – daniero
    Commented Dec 20, 2012 at 19:26
  • 13
    \$\begingroup\$ I'd be more impressed with a program that did the reverse (i.e. input is the scrambled text). \$\endgroup\$
    – Mr Lister
    Commented Dec 21, 2012 at 13:38
  • 3
    \$\begingroup\$ Must the position of the apostrophe in don't remain in the same position? The spec says All other formatting must remain the same (capitalization and punctuation, etc.). but I'm not sure how that works out here... \$\endgroup\$
    – Gaffi
    Commented Mar 20, 2013 at 17:25

28 Answers 28

10
\$\begingroup\$

Ruby - 50 48 chars, plus -p command line parameter.

gsub(/(?<=\w)\w+(?=\w)/){[*$&.chars].shuffle*''}

Thanks @primo for -2 char.

Test

➜  codegolf git:(master) ruby -p 9261-cambridge-transposition.rb < 9261.in
Acdrcinog to a racreseher at Cagribmde Ursvetniiy, it dsoen't mttaer in waht odrer the leertts in a word are, the olny ionarpmtt tnhig is that the fsirt and last letetr be at the rghit pcale. The rset can be a taotl mses and you can slitl raed it wthiuot perlbom. Tihs is buaecse the hmuan mind does not raed ervey lteetr by ietlsf but the word as a wlhoe.
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Ruby doesn't support \K for zero-width look-behind assertion? Also, the innermost grouping is unnecessary, using $& instead of $1. \$\endgroup\$
    – primo
    Commented Dec 20, 2012 at 17:47
  • \$\begingroup\$ @primo, I think not, it doesn't work, and neither have I found it in any reference page. Thanks for $& tip :) \$\endgroup\$
    – Dogbert
    Commented Dec 20, 2012 at 18:17
  • \$\begingroup\$ You're right. I guess I assumed they had taken perl regex directly, as php does ;) \$\endgroup\$
    – primo
    Commented Dec 20, 2012 at 19:01
  • 3
    \$\begingroup\$ tell me more about this codegolf script \$\endgroup\$
    – Sparr
    Commented Feb 2, 2016 at 17:27
  • 1
    \$\begingroup\$ Many years later, but: There is no need to create a new array before the shuffle: [*$&.chars] => $&.chars, saving 3 bytes. \$\endgroup\$
    – daniero
    Commented Jul 17, 2018 at 1:02
5
\$\begingroup\$

PHP 84 bytes

<?for(;$s=fgets(STDIN);)echo preg_filter('/\w\K\w+(?=\w)/e','str_shuffle("\0")',$s);

Using a regex to capture words that are at least 4 3 letters long, and shuffling the inner characters. This code can handle input with multiple lines as well.

If only one line of input is required (as in the example), this can be reduced to 68 bytes

<?=preg_filter('/\w\K\w+(?=\w)/e','str_shuffle("\0")',fgets(STDIN));

There's only one letter in the middle, so it doesn't matter if you shuffle it.

\$\endgroup\$
5
\$\begingroup\$

Python, 118

Python is terribly awkward for things like this!

from random import*
for w in raw_input().split():l=len(w)-2;print l>0and w[0]+''.join((sample(w[1:-1],l)))+w[-1]or w,

Bonus

I tried some other things that I thought would be clever, but you have to import all sorts of things, and a lot of methods don't have return values, but need to be called separately as its own statement. The worst is when you need to convert the string to a list and then joining it back to a string again.

Anyways, here are some of the things that I tried:

Regex!
import re,random
def f(x):a,b,c=x.group(1,2,3);return a+''.join(random.sample(b,len(b)))+c
print re.sub('(\w)(\w+)(\w)',f,raw_input())
Permutations!
import itertools as i,random as r
for w in raw_input().split():print''.join(r.choice([x for x in i.permutations(w)if w[0]+w[-1]==x[0]+x[-1]])),
You can't shuffle a partition of a list directly and shuffle returns None, yay!
from random import*
for w in raw_input().split():
 w=list(w)
 if len(w)>3:v=w[1:-1];shuffle(v);w[1:-1]=v
 print ''.join(w),
\$\endgroup\$
2
  • \$\begingroup\$ Actually, the Regex version is the only one that works. The others fail for words that are followed by punctuation, because the punctuation is treated as the last letter. \$\endgroup\$
    – movatica
    Commented Jan 8, 2021 at 12:09
  • \$\begingroup\$ Looks like you're right, @movatica. I almost haven't touched Python in years (this post is 8 years old!), but I'll see if I'll get around to fixing it eventually. edit: Oh, you fixed it (well, kinda; python 3). Have an upvote ;) \$\endgroup\$
    – daniero
    Commented Jan 9, 2021 at 14:31
5
\$\begingroup\$

Retina, 10 bytes

?V`\B\w+\B

Try it online!

Hey, this old challenge was made for the new Retina!

Explanation

\B\w+\B matches groups of letters between non-boundaries, that is groups of letters that don't begin or end a word. Since regexes are greedy, this will match all the letters of a word except the first and the last one.

V is the "reverse" stage, that reverses the order of characters in each match of the regex. With the ? option it scrambles them instead.

\$\endgroup\$
1
3
\$\begingroup\$

J (48)

''[1!:2&4('\w(\w+)\w';,1)({~?~@#)rxapply 1!:1[3

Explanation:

  • 1!:1[3: read all input from stdin
  • rxapply: apply the given function to the portions of the input that match the regex
  • ({~?~@#): a verb train that shuffles its input: # counts the length, this gets applied to both sides of ? giving N distinct numbers from 0 to N, { then selects the elements at those indices from the input array.
  • ('\w(\w+)\w';,1): use that regex but only use the value from the first group
  • [1!:2&4: send unformatted output to stdout
  • ''[: suppress formatted output. This is necessary because otherwise it only outputs that part of the output that fits on a terminal line and then ends with ....
\$\endgroup\$
2
\$\begingroup\$

Nibbles, 15 bytes

This was a little awkward since nibbles doesn't have shuffle or even randomness (nor does it have regex).

+.%~@ \$a
  :@
    ::`($
      ++=~
        `)\`'$
        `# $ 0
      $

(Nibbles isn't on TIO yet). But use it like:

> nibbles cambridge.nbl < input.txt
Aoinrccdg to a raeeercshr at Caibrmgde Uvteiinrsy, it doesn't mttaer in waht oerdr the ltteers in a word are, the olny itoanrmpt tinhg is taht the first and last ltteer be at the rihgt palce. The rest can be a ttoal mess and you can stlil raed it wutoiht poelbrm. Tihs is buaecse the huamn mind does not raed every ltteer by itelsf but the word as a wolhe. 

That's the literate form which is 30 nibbles.

That last $ could be left out but there is no point since this program is 30 nibbles (it would be 29 half bytes then rounded up)

How?

+.%~@ \$a          concat the map of the split by alpha numeric chars
  :@               append the value that preceded the split match
    ::`($          append the first value of the match to
      ++=~         concat the concat of the group by of
        `)\`'$     the tail of the reverse of the transpose of the tail of the match
        `# $ 0     hash of the char (0 says range is infinite)
      $            the head of the reversed thing from earlier

Essentially it selects groups of alphanums, then selects the inner part of that using uncons twice. Then for randomness does a group by (which also sorts by) the hash of a character.

The transpose part is so that doing uncons on an empty string returns an empty string instead of null character (which becomes a space). You could leave it out but you'd get an extra space on 1 letter words like "a".

\$\endgroup\$
1
\$\begingroup\$

APL 107

Unfortunately my APL interpreter does not support regexs so here's a home rolled version where the text to be scrambled is stored in the variable t:

⎕av[((~z)\(∊y)[∊(+\0,¯1↓n)+¨n?¨n←⍴¨y←(~z←×(~x)+(x>¯1↓0,x)+x>1↓(x←~53≤(∊(⊂⍳26)+¨65 97)⍳v←⎕av⍳,t),0)⊂v])+z×v]

Essentially the code partitions the text into words based on the letters of the alphabet only and then into the letters between the first and last letters of those words. These letters are then scrambled and the whole character string reassembled.

\$\endgroup\$
1
\$\begingroup\$

APL, 58 49

I believe this works in IBM APL2 (I don't have IBM APL)

({⍵[⌽∪t,⌽∪1,?⍨t←⍴⍵]}¨x⊂⍨~b),.,x⊂⍨b←' ,.'∊⍨x←⍞,' '

If not, then in Dyalog APL, add to the front:

 ⎕ML←3⋄

which adds 6 chars


This assumes the only non-word characters are space, comma, and period.

\$\endgroup\$
1
  • \$\begingroup\$ Still golfable, but I don't have APL symbols on iPhone... \$\endgroup\$
    – TwiN
    Commented Dec 28, 2012 at 12:01
1
\$\begingroup\$

VBA, 351 373/409

Sub v(g)
m=1:Z=Split(g," "):j=UBound(Z)
For u=0 To j
t=Z(u):w=Len(t):l=Right(t,1):If Not l Like"[A-Za-z]" Then w=w-1:t=Left(t,w):e=l Else e=""
If w>3 Then
n=Left(t,1):p=w-1:s=Right(t,p):f=Right(t,1)
For p=1 To p-1
q=w-p:r=Int((q-1)*Rnd())+1:n=n & Mid(s,r,1):s=Left(s,r-1) & Right(s,q-r)
Next
Else
n=t:f=""
End If
d=d & n & f & e & " "
Next
g=d
End Sub

Alternate (larger) Method:

Sub v(g)
m=1:Z=Split(g," "):j=UBound(Z)
For u=0 To j
t=Split(StrConv(Z(u),64),Chr(0)):w=UBound(t)-1:l=Asc(t(w)):If l<64 Or (l>90 And l<97) Or l>122 Then e=t(w):w=w-1 Else e=""
If w>3 Then
n=t(0):p=w-1:s=""
For i=-p To -1
s=t(-i) & s
Next
f=t(w)
For p=1 To p-1
r=Int((w-p)*Rnd())+1:n=n & Mid(s,r,1):s=Left(s,r-1) & Right(s,w-p-r)
Next
n=n & s
Else
n=Z(u):f="":e=""
End If
d=d & n & f & e & " "
Next
g=d
End Sub

Both of these methods change the value of the variable passed to the Sub. i.e.

Sub Test()
strTestString = "This is a test."
v strTestString
Debug.Print strTestString
End Sub

will output something like this:

"Tihs is a tset."

Also, this does randomize mid-word punctuation, so this may not fit the spec 100%.

\$\endgroup\$
1
\$\begingroup\$

APL NARS 172 chars

r←g x;i;s;d;k
s←⎕AV[98..123]∪⎕A
i←1⋄v←''⋄r←''⋄k←⍴x
A:d←''⋄→C×⍳i>k⋄d←x[i]⋄→C×⍳∼d∊s⋄v←v,d⋄i+←1⋄→A
C:v←{t←¯2+⍴r←⍵⋄t≤1:r⋄r[1+t?t]←⍵[1+⍳t]⋄r}v
r←∊r,v,d
v←''⋄i+←1⋄→A×⍳i≤k
g x←⍞

13+17+18+44+41+8+17+5+9=172; This function g() has input as a string; has output as a string. I add the input command because i don't know how insert \' in a quoted string. Commented

∇r←g x;i;s;d;k
   ⍝ words are element of  a-zA-Z separed from all other
   s←⎕AV[98..123]∪⎕A ⍝a-zA-Z ascii ⎕IO = 1
   i←1⋄v←''⋄r←''⋄k←⍴x
A:   d←''⋄→C×⍳i>k⋄d←x[i]⋄→C×⍳∼d∊s⋄v←v,d⋄i+←1⋄→A
C:      v←{t←¯2+⍴r←⍵⋄t≤1:r⋄r[1+t?t]←⍵[1+⍳t]⋄r}v
        r←∊r,v,d
        v←''⋄i+←1⋄→A×⍳i≤k
∇

result

g x←⍞
According to a researcher at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place. The rest can be a total mess and you can still read it without problem. This is because the human mind does not read every letter by itself but the word as a whole.
  Androiccg to a rhraeecser at Cgirbdmae Uirevtsiny, it deson't mtetar in waht oderr the ltrtees in a wrod are, the olny intro
  apmt tinhg is taht the frsit and lsat lteter be at the rghit pacle. The rset can be a ttaol mses and you can siltl rae
  d it wtuhoit poeblrm. Tihs is bcsauee the hmaun mnid deos not raed eervy lteter by isletf but the wrod as a wolhe.
\$\endgroup\$
1
\$\begingroup\$

PHP 7.1, not competing, 80 bytes

for(;$w=$argv[++$i];)echo$w[3]?$w[0].str_shuffle(substr($w,1,-1)).$w[-1]:$w," ";

takes input from command line arguments. Run with -nr. (will obviously fail at punctuation)

\$\endgroup\$
1
\$\begingroup\$

PHP, 94+1 bytes

+1 for -R flag

<?=preg_replace_callback("/(?<=\w)\w+(?=\w)/",function($m){return str_shuffle($m[0]);},$argn);

Pipe input through php -nR '<code>'.

Note: preg_replace_callback came to PHP in 4.0.5; closures were introduced in php 5.3;
so this requires PHP 5.3 or later.

Unfortunately, the match is always sent as an array, even if there are no sub patterns,
thus I cannot just use str_shuffle as the callback, which would save 29 bytes.

\$\endgroup\$
1
+50
\$\begingroup\$

JavaScript, 76 67 bytes

thanks to Arnauld for -9 bytes.

t=>t.replace(/\B\w+\B/g,m=>[...m].sort(_=>Math.random()-.5).join``)

Try it online!


Ungolfed

t =>                  // function with a single argument
     t.replace(       // Replace ...
         /\B\w+\B/g,  // every match of the regex
         m => ...     // with the return value of the replacement function
     )

/       /g            // Match all occurences of
   \w+                // 1 or more word chars ...
 \B   \B              // ... that aren't on the beginning or end of the word

m =>                  // Replacement function
     [...m]           // convert matched string to a list of chars
       .sort(_ => Math.random()-.5) // sort with a random comparision function
       .join``        // join the list into a single string
\$\endgroup\$
7
  • \$\begingroup\$ Note that "shuffle by doing sort like that is not uniform" \$\endgroup\$
    – user202729
    Commented Aug 5, 2018 at 15:40
  • \$\begingroup\$ You can use /\B\w+\B/g. (But for the bounty, note that code length isn't important.) \$\endgroup\$
    – Arnauld
    Commented Aug 5, 2018 at 19:54
  • 1
    \$\begingroup\$ @Arnauld thanks a lot. As this is still codegolf, every byte counts. \$\endgroup\$
    – ovs
    Commented Aug 5, 2018 at 21:10
  • \$\begingroup\$ @Arnauld Serious contender rule still applies. \$\endgroup\$
    – user202729
    Commented Aug 6, 2018 at 12:03
  • 1
    \$\begingroup\$ @trejder I added an explanation that should help you to modify the code for your needs. In its current form the code should run well in most browsers. If you want to use in this in real code, you should probably change the way it shuffles the characters to a uniform algorithm. \$\endgroup\$
    – ovs
    Commented Aug 6, 2018 at 15:23
1
\$\begingroup\$

Jelly, 22 20 bytes

e€ØẠ+ƝỊŻœṗµḢ,ṪjW)ẎẊ€

Try it online!

This was fun to make.

Each non-alphabet character is grouped into a single character string, and the first and last characters of each word are grouped the same way as well. That way, taking a random permutation of those characters will not change their order.

Made with a lot of help from caird coinheringaahing.

-2 bytes from caird coinheringaahing.

Explanation

e€ØẠ+2\ỊŻœṗ⁸µḢ,ṪjW)ẎẊ€ 
 €                     for each character:
e ØẠ                   check if it's in the alphabet
    +2\                add overlapping pairs
       Ị               check if they are ≤ 1
        œṗ⁸            and partition the input using that bitmask
           µ      )    execute the following on each part:
            Ḣ,ṪjW      get head, tail, and join with the modified list
                   Ẏ   join the list of triplets together
                    Ẋ€ get a random permutation of each of the elements
\$\endgroup\$
1
1
\$\begingroup\$

Python 3, 113 bytes

based on the original python answer by daniero

import re,random
print(re.sub('(\w)(\w+)(\w)',lambda m:m[1]+''.join(random.sample(m[2],len(m[2])))+m[3],input()))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Stax, 15 14 13 bytes

â#]ø\┘‼↕ip2╚_

Run and debug it

Regex is busted.

-2 bytes from recursive.

\$\endgroup\$
1
\$\begingroup\$

R, 159 bytes

function(x){l=attr(w<-el(gregexpr("[a-zA-Z]+",x)),"m")
Map(function(i,j)substr(x,i,j)<<-Reduce(paste0,sample(substring(x,r<-i:j,r))),s<-w[l>3]+1,s+l[l>3]-3)
x}

Try it online!

Posted before realizing that there was already another R answer, but pleasingly the two approaches are quite different...

Ungolfed code:

cambridge_transpose=
function(x){                        # x is the input string,
  w=el(gregexpr("[a-zA-Z]+",x))     # w gets the words in x using a regex,
  l=attr(w,"match.length")          # l gets the lengths of each word,
  s=w[l>3]+1                        # s is the start letter to jumble in each word,
  e=s+l[l>3]-3                      # e is the end letter to jumble in each word.
  Map(function(i,j) ... ,s,e)       # Now, use this function on all elements i,j from s,e:
    substr(x,i,j)<<-                #   replace letters in x from i to j
      Reduce(paste0,                #   with these letters joined-together:
        sample(                     #     a random sample of
          substring(x,r<-i:j,r)))   #     the letters in x from i to j.
x}                                  # Finally, return (modified) x.
\$\endgroup\$
1
\$\begingroup\$

Python3, 110 bytes:

lambda x:re.sub('(?<=\w)\w{3,}(?=\w)',lambda x:''.join(r.sample(g:=x.group(),len(g))),x)
import re,random as r

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 102 bytes: lambda s:re.sub('(?<=\w)\w+(?=\w)',lambda m:''.join(r.sample(m[0],len(m[0]))),s) \$\endgroup\$
    – movatica
    Commented Jan 29 at 18:02
0
\$\begingroup\$

R, 179

Using the function I wrote for the randomize letters in a word problem:

Input:

s <- "According to a researcher at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place. The rest can be a total mess and you can still read it without problem. This is because the human mind does not read every letter by itself but the word as a whole."

Solution:

f=function(w){l=length;s=strsplit(w,"")[[1]];ifelse(l(s)<3,w,paste(c(s[1],sample(s[2:(l(s)-1)]),s[l(s)]),collapse=""))}
g=Vectorize(f)
paste(g(strsplit(s," ")[[1]]), collapse=" ")

Result:

[1] "Arioccdng to a reehaecrsr at Cabrgimde Uveirisnyt, it des'not mttear in waht odrer the lttrees in a wrod are, the olny inpotmart thnig is that the fsrit and lsat letetr be at the right palce. The rset can be a toatl mses and you can stlil raed it wutioht pmrlebo. This is bsuceae the hmuan mnid deos not read ervey lteetr by iesltf but the word as a wleho."
\$\endgroup\$
0
\$\begingroup\$

Pyth, 23 bytes

jdm?gld4++hd.<Ptd1eddcz

Non-competing because Pyth was made after this challenge was posted.

Try it online

\$\endgroup\$
0
\$\begingroup\$

Japt, 32 bytes

m@Xl ¨4?Xg0 +Xs1J ö(x) +XgJ:X}" 

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Can I run Japt directly in a browser? Without any external libraries, compilers etc.? If not then this unfortunately doesn't count as per bounty rules (need solution that works in pure web browser). Second thing, as I think the original rules for Cambridge Transposition were a bit different than showed here (in OP question). Is it possible to modify your code to scramble words 5+ letters long (instead of 4+ letters long, as in OP's question)? \$\endgroup\$
    – trejder
    Commented Aug 6, 2018 at 14:45
  • 1
    \$\begingroup\$ @trejder All submissions must conform to the rules in the original question. Modifying it like this would make it invalid. \$\endgroup\$
    – user202729
    Commented Aug 7, 2018 at 1:46
  • 1
    \$\begingroup\$ @trejder Japt can't run directly in a browser without a compiler. Secondly if you replace the 4 in the code with 5 then it should only scramble 5+ letter long words. \$\endgroup\$
    – Bejofo
    Commented Aug 7, 2018 at 5:48
0
\$\begingroup\$

Java, 1557 834 bytes Thanks to @JoKing for tips.

A bit late to the competition. Forgot that I had started on this problem.

Golfed

import java.util.*;public class s{ public static void main(String[] args){ Scanner s=new Scanner(System.in);String a=s.next();String[] q=a.split("\\s+");for (int i=0;i<q.length;i++) { q[i]=q[i].replaceAll("[^\\w]", ""); }String f="";for (String z:q) { f+=scramble(z);f+=" "; }System.out.print(f); }private static String scramble(String w){if(w.length()==1||w.length()==2){return w;}char[]l=w.toCharArray();char q=l[w.length()-1];String e=Character.toString(l[0]);char[]n=new char[l.length-2];for(int i=0;i<l.length-2;i++){n[i]=l[i+1];}HashMap<Integer,Character>s=new HashMap<>();int c=1;for(char p:n){s.put(c,p);c++;}HashMap<Integer,Integer>o=new HashMap<>();Random z=new Random();for(int i=0;i<w.length()-2;i++){int m=z.nextInt(n.length);while(o.getOrDefault(m,0) == 1){m=z.nextInt(n.length);}e+=s.get(m+1);o.put(m,1);}return e+=q;}}

Non-golfed

import java.util.HashMap;
import java.util.Random;

public class SentenceTransposition {
    public static void main(String[] args) {
        String input = "According to a researcher at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place. The rest can be a total mess and you can still read it without problem. This is because the human mind does not read every letter by itself but the word as a whole.";
        String[] words = input.split("\\s+");
        for (int i = 0; i < words.length; i++) {
            words[i] = words[i].replaceAll("[^\\w]", "");
        }
        String finalsentence = "";
        for (String word : words) {
            finalsentence += scramble(word);
            finalsentence += " ";
        }
        System.out.println(finalsentence);
    }

    private static String scramble(String word) {
        if (word.length() == 1 || word.length() == 2) {
            return word;
        }
        char[] letters = word.toCharArray();
        char lletter = letters[word.length()-1];
        String endword = Character.toString(letters[0]);
        char[] nletters = new char[letters.length-2];
        for (int i = 0; i < letters.length-2; i++) {
            nletters[i] = letters[i+1];
        }
        HashMap<Integer, Character> set = new HashMap<>();
        int count = 1;
        for (char nletter : nletters) {
            set.put(count, nletter);
            count++;
        }
        HashMap<Integer, Integer> chosen = new HashMap<>();
        Random random = new Random();
        for (int i = 0; i < word.length()-2; i++) {
            int cur = random.nextInt(nletters.length);
            while (chosen.getOrDefault(cur,0) == 1) {
                cur = random.nextInt(nletters.length);
            }
            endword += set.get(cur+1);
            chosen.put(cur, 1);
        }
        return endword += lletter;
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ It looks like there's a lot of whitespace you can remove. Have you had a look at Tips for golfing in Java? edit: also, you seem to have the input hard-coded. You should be taking input from the user instead \$\endgroup\$
    – Jo King
    Commented Sep 16, 2018 at 5:07
  • \$\begingroup\$ @JoKing ah ok. I will take input from the user. \$\endgroup\$
    – Jaden Lee
    Commented Sep 16, 2018 at 5:11
  • \$\begingroup\$ I managed to golf this down to 650 bytes before realizing that it doesn't work. \$\endgroup\$
    – Quintec
    Commented Sep 16, 2018 at 15:23
  • \$\begingroup\$ @Quintec do you mean that my code doesn't work? \$\endgroup\$
    – Jaden Lee
    Commented Sep 25, 2018 at 3:23
  • 1
    \$\begingroup\$ 539 bytes \$\endgroup\$
    – ceilingcat
    Commented Jun 19, 2021 at 18:44
0
\$\begingroup\$

Sidef, 89 85 bytes

Block (anonymous callable):

{.words.map{[_[0],(_.len-1?([_[1..^(_.len-1)]].shuffle...,_[1]):'')].join}.join(' ')}

Output, when used like { ... }('..'):

 I hvae nveer not ocne in my life slleepd nhedatarnel crtreolcy
 I have never not once in my lfie sepelld naetadenrhl ccrtloery

Somewhat ungolfed

.words.map{
  [
    .first,
    (_.len-1
      ? (  [ _[1..^(_.len-1)] ].shuffle..., .last )
      : '')
  ].join
}.join(' ')
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -p, 74 bytes

s|\pL\K\pL{2,}(?=\pL)|$,=$q=$&;$q=join'',shuffle$,=~/./g while$,eq$q;$q|ge

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Raku, 49 bytes

Moved from the duplicate thread.

{S:g/(\w)(\w*)(\w)/{$0~$1.comb.pick(*).join~$2}/}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 19 bytes

.γa}εDaićsD¨.rsθJ]J

Try it online.

Explanation:

.γ              # Consecutive group by:
  a             #  Is alphabetic (so all upper- and lowercase letters)
 }ε             # After the group-by, map each string to:
   D            #  Duplicate the current string
    ai          #  If it's alphabetic:
      ć         #   Extract head; pop and push the remainder-string and first character
                #   separated to the stack
                #    i.e. "According" → STACK: "ccording","A"
       s        #   Swap so remainder-string is at the top again
                #    STACK: "A","ccording"
        D       #   Duplicate it
                #    STACK: "A","ccording","ccording"
         ¨      #   Remove the last character from the copy
                #    STACK: "A","ccording","ccordin"
          .r    #   Shuffle the remaining characters
                #    STACK: "A","ccording","occdrni"
            s   #   Swap so the remainder-string is at the top again
                #    STACK: "A","occdrni","ccording"
             θ  #   Only leave its last character
                #    STACK: "A","occdrni","g"
              J #   Join the three back together to a string again
                #    STACK: "Aoccdrnig"
  ]             # Close both the if-statement and map
   J            # And join all strings in the list back together
                # (after which the result is output implicitly)
\$\endgroup\$
0
\$\begingroup\$

Japt, 13 bytes

r"%B%w+%B"Èö¬

Try it

\$\endgroup\$
0
\$\begingroup\$

Go, 248 226 bytes

import(."math/rand";."regexp")
func f(s string)string{return MustCompile(`(\w)(\w+)(\w)`).ReplaceAllStringFunc(s,func(S string)(e string){k:=S[1:len(S)-1]
for _,i:=range Perm(len(k)){e+=k[i:i+1]}
return S[:1]+e+S[len(S)-1:]})}

Attempt This Online!

Embeds my answer for "How to randomize letters in a word" within this answer.

  • -22 from rand.Perm, string slices, and named return.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.