Skip to main content
added 339 characters in body
Source Link
David G. Stork
  • 42.3k
  • 3
  • 37
  • 106

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution does not contain strings of variable length. As the question poser stated in a comment: "I want to generate 4 and 5 element lists that match the criteria i.e. {HHHH},{THHHH}"


Here's the shortest code I could find:

StringJoin /@ 
 Flatten[(Select[Tuples[{"H", "T"}, #], 
      Counts[#]["H"] == 4 && Last[#] == "H" &] & /@ {4, 5, 6}), 1]

(* {"HHHH", "HHHTH", "HHTHH", "HTHHH", "THHHH", "HHHTTH", "HHTHTH", "HHTTHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution does not contain strings of variable length. As the question poser stated in a comment: "I want to generate 4 and 5 element lists that match the criteria i.e. {HHHH},{THHHH}"

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution does not contain strings of variable length. As the question poser stated in a comment: "I want to generate 4 and 5 element lists that match the criteria i.e. {HHHH},{THHHH}"


Here's the shortest code I could find:

StringJoin /@ 
 Flatten[(Select[Tuples[{"H", "T"}, #], 
      Counts[#]["H"] == 4 && Last[#] == "H" &] & /@ {4, 5, 6}), 1]

(* {"HHHH", "HHHTH", "HHTHH", "HTHHH", "THHHH", "HHHTTH", "HHTHTH", "HHTTHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

added 132 characters in body
Source Link
David G. Stork
  • 42.3k
  • 3
  • 37
  • 106

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution does not contain strings of variable length. As the question poser stated in a comment: "I want to generate 4 and 5 element lists that match the criteria i.e. {HHHH},{THHHH}"

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution does not contain strings of variable length.

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution does not contain strings of variable length. As the question poser stated in a comment: "I want to generate 4 and 5 element lists that match the criteria i.e. {HHHH},{THHHH}"

added 21 characters in body
Source Link
David G. Stork
  • 42.3k
  • 3
  • 37
  • 106

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution isdoes not contain strings of variable length.

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution is not variable length.

Here is an inelegantly programmed solution, which can be streamlined, if you wish.

Here is a list of all strings of length exactly 6 that have exactly 4 Hs:

xxx = StringJoin /@ 
  Select[Tuples[{"H", "T"}, 6], Counts[#]["H"] == 4 &]

(* {"HHHHTT", "HHHTHT", "HHHTTH", "HHTHHT", "HHTHTH", "HHTTHH", "HTHHHT", "HTHHTH", "HTHTHH", "HTTHHH", "THHHHT", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter in a string is a T, then that string is "useless" (because the game would have already ended) and should be deleted. Here's a set that has this property, i.e., is either a length-6 string ending in H or a length-5 string with exactly 4 Hs:

yyy = If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ xxx

(* {"HHHHT", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

If the last letter of a string in this list is a T, then that string is "useless" and should be deleted:

If[StringEndsQ[#, "T"], StringDrop[#, -1], #] & /@ yyy

(* {"HHHH", "HHHTH", "HHHTTH", "HHTHH", "HHTHTH", "HHTTHH", "HTHHH", "HTHHTH", "HTHTHH", "HTTHHH", "THHHH", "THHHTH", "THHTHH", "THTHHH", "TTHHHH"} *)

This is the final list that satisfies all the questioner's requests. Note that the "accepted" solution does not satisfy the request. For instance, the accepted solution does not include {HHHH} whereas the questioner explicitly demanded that this string be included. Moreover, the questioner explicitly worried about "variable length" strings, and the "accepted" solution does not contain strings of variable length.

added 130 characters in body
Source Link
David G. Stork
  • 42.3k
  • 3
  • 37
  • 106
Loading
Source Link
David G. Stork
  • 42.3k
  • 3
  • 37
  • 106
Loading