Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Python 3, 94 93 91 characters

Using a different technique. Might also work in Python 2.

from random import*
s=lambda x:x[0]+''.join(sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366https://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one expression, we can use a lambda (eliminating return, def, and a pair of parentheses).

Edit: from random import* to save 1 character, after the other Python submission.

Python 3, 94 93 91 characters

Using a different technique. Might also work in Python 2.

from random import*
s=lambda x:x[0]+''.join(sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one expression, we can use a lambda (eliminating return, def, and a pair of parentheses).

Edit: from random import* to save 1 character, after the other Python submission.

Python 3, 94 93 91 characters

Using a different technique. Might also work in Python 2.

from random import*
s=lambda x:x[0]+''.join(sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from https://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one expression, we can use a lambda (eliminating return, def, and a pair of parentheses).

Edit: from random import* to save 1 character, after the other Python submission.

I can't count
Source Link
Mechanical snail
  • 2.3k
  • 1
  • 18
  • 21

Python 3, 94 9393 91 characters

Using a different technique. Might also work in Python 2.

from random import*
s=lambda x:x[0]+''.join(sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one lineexpression, we can use a lambda (eliminating return, def, and a pair of parentheses).

Edit: from random import *import* to save 1 character, after the other Python submission.

Python 3, 94 93 characters

Using a different technique. Might also work in Python 2.

from random import*
s=lambda x:x[0]+''.join(sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one line, we can use a lambda (eliminating return, def, and a pair of parentheses).

Edit: from random import * to save 1 character, after the other Python submission.

Python 3, 94 93 91 characters

Using a different technique. Might also work in Python 2.

from random import*
s=lambda x:x[0]+''.join(sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one expression, we can use a lambda (eliminating return, def, and a pair of parentheses).

Edit: from random import* to save 1 character, after the other Python submission.

save 1 character
Source Link
Mechanical snail
  • 2.3k
  • 1
  • 18
  • 21

Python 3, 9494 93 characters

Using a different technique. Might also work in Python 2.

importfrom random as rimport*
s=lambda x:x[0]+''.join(r.sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one line, we can use a lambda (eliminating return, def, and a pair of parentheses).

Edit: from random import * to save 1 character, after the other Python submission.

Python 3, 94 characters

Using a different technique. Might also work in Python 2.

import random as r
s=lambda x:x[0]+''.join(r.sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one line, we can use a lambda (eliminating return, def, and a pair of parentheses).

Python 3, 94 93 characters

Using a different technique. Might also work in Python 2.

from random import*
s=lambda x:x[0]+''.join(sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x

The ... if x[0:-1] else x gives x if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.

The sample() is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.

Since it's one line, we can use a lambda (eliminating return, def, and a pair of parentheses).

Edit: from random import * to save 1 character, after the other Python submission.

Source Link
Mechanical snail
  • 2.3k
  • 1
  • 18
  • 21
Loading