Python Forum
looking for code: combinations of letter in order
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking for code: combinations of letter in order
#1
i am looking for code (preferably a function) that when given a sequence, such as a string of letters, will create a list or set (of strings or tuples) or tuple of all possible combinations of the elements of the given sequence, in the order present in that given sequence. the order of the resulting combinations is not important, hence a set is valid for the results. for example:

given string:
"skap"

a valid result:
["s","sk","ska","skap","sap","sa","sp","skp","k","ka","kap","ap","a","p","kp"]

other orders of the same set of strings would be valid.  any sequence or set with any of these (not a complete list) would be an invalid result:
"ss", "spk", "paks"

given list:
[ 3, 1, 4]

a valid result:
[[3], [3,1], [3,1,4], [3,4], [1], [1,4], [4]]

again, the order does not matter, so this is also a valid result:
[[4], [1,4], [3,4], [1], [3,1,4], [3], [3,1]]

there should not be duplicates, so this should be considered to be an invalid result:
[[4], [1,4], [1], [3,4], [1], [3,1,4], [3], [3,1]]

you would not have this issue with sets:
{(3,),(3,1),(3,1,4),(3,4),(1,)(1,3),(4,)}

i don't know the mathematical terminology for this.  i think it would be part of set theory.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
looking for code: combinations of letter in order - by Skaperen - Nov-16-2017, 02:34 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020