Problem of the Day
A new programming or logic puzzle every Mon-Fri

Letter Shuffle

It has been proven that we can read words where the first and last letters are in the correct position and the inner letters have been shuffled around. Your goal today is to create a program that takes in some text and shuffles the inner letters. For example: could -> cuold.

Permalink: http://problemotd.com/problem/letter-shuffle/

Comments:

  • David - 10 years, 5 months ago

    Python

    from sys import argv  # This import allows this script to operate as a stand alone executable
    from random import shuffle # Required import
    
    # For "single word" string
    def innerShuffle(inStr):
        temp = list(inStr[1:len(inStr)-1])
        shuffle(temp)
        return inStr[0] + ''.join(temp) + inStr[-1]
    
    # For "sentence" string
    def innerShuffleSentence(inSent):
        output = ''
        for word in inSent.split():
            innerShuffle(word)
            if output is not '':
                output += ' '
            output += innerShuffle(word)
        return output
    
    # Example Usage
    # "Main"
    if(len(argv) > 1):
        for arg in argv[1:]:
            print innerShuffle(arg)
    else:
        print "No args given. Observe strambling 'omlettes': %s" % innerShuffle('omlettes')
        print innerShuffleSentence("Now observe your ability to read a whole sentence that has been scrambled by the derived function") + ' innerShuffleSentence()'
    

    reply permalink

  • Anonymous - 10 years, 5 months ago

    pretty simple in haskell:

    import System.Random
    import Data.List
    
    main = do
      g <- getStdGen
      putStrLn "Word to scramble:"
      w <- getLine
      let r = next g
      let x = scramble w $ mod (fst r) ((length w) - 2)
      print x
    
    
    scramble :: String -> Int -> String
    scramble old@(x:xs) i = if new == old then scramble old (mod (i+1) (length old - 2)) else new
        where
            scramble' ys = permutations ys !! i
            new = (x : (scramble' (init xs))) ++ [last xs]
    
    

    reply permalink

  • bumbleguppy - 10 years, 5 months ago

    javascript doesn't have a built-in shuffle, but I did steal David's sentence :-)

    function innerShuffle(sentence){
    
        var sentenceArr = sentence.split(' ');
    
        for(var i = 0, len = sentenceArr.length; i < len; i++){
            sentenceArr[i] = shuffle(sentenceArr[i]);
        }
    
        return sentenceArr.join(' ');
    
    
        function shuffle(str){
            var len = str.length;
    
            if(len < 4) return str;
    
            var midStr = str.substring(1, len - 1);
            var randArr = [];
            var retVal = '';
            var i = 0;
    
            for(i = 0, len -= 2; i < len; i++){
                randArr[i] = [midStr.charAt(i), Math.random()]; //[each letter in sequence, random number] 
            }
    
            randArr.sort(function(a,b){ 
                return (a[1] < b[1] ? -1 : (a[1] > b[1] ? 1 : 0));
                }); //sort the multidimensional array by the second element within each element
    
            for(i = 0; i < len; i++){
                retVal += randArr[i][0];
            }
    
            return str.charAt(0) + retVal + str.charAt(len + 1);
        }
    }
    
    console.log(innerShuffle('Now observe your ability to read a whole sentence that has been scrambled by the derived function'));
    

    reply permalink

  • Driphter - 10 years, 5 months ago

    Clojure:

    (defn inner-shuffle
      [value]
      (str (first value)
           (->> value (drop-last) (rest) (shuffle) (reduce str))
           (last value)))
    

    reply permalink

  • Emre Ozdemir - 10 years, 5 months ago

    Solution in C++. Sadly only works with words.

    #include <iostream>
    #include <string>
    #include <time.h>
    using namespace std;
    
    int main(){
        srand(time(NULL));
        string word;
        char wordSwap=0;
        cin >> word;
    
        for(int i = 0; i < word.size(); i++)
        {
            int random1 = rand() % (word.size()-2)+1;
            int random2 = rand() % (word.size()-2)+1;
    
            wordSwap = word[random1];
            word[random1] = word[random2];
            word[random2] = wordSwap;
        }
    
        cout << word;
    
        getchar();
        getchar();
        return 0;
    }
    

    reply permalink

Content curated by @MaxBurstein