2
\$\begingroup\$

Challenge

  • Construct n strings, each with three distinct letters, chosen randomly with equal probability.

  • Print the proportion k/n where k is the number of strings such that all three characters are in ascending order. Print also those k strings.

Input

  • The only input is n (and thus it must be a positive integer).

Outputs

  • The proportion k/n must be truncated to two decimal places.

  • The strings that are in ascending order of letters.

Requirements

  • The letters must come from the English alphabet.

  • You cannot print the value of k.

Examples

  • Input: 6, Output: 0.16 agh

  • Input: 1, Output: 1.00 suv

  • Input: 9, Output: 0.22 hls jqr

How to Win

  • Fewest number of bytes wins.
\$\endgroup\$
  • 1
    \$\begingroup\$ Just curious: Why do you keep changing the examples? They are examples, not test cases! The output will obviously vary from run to run for any such program. The probability of making a word with 3 distinct letters which are in ascending order is exactly 1/6, and given n, the number k is a random variable that is binomially distributed with parameters (n, 1/6). The expected value for k is exactly n/6, and so the quotient k/n is just approximating 1/6. \$\endgroup\$ – Wisław Nov 10 at 13:34
  • 2
    \$\begingroup\$ The problem remains that the generation of the strings is an unobservable requirement. Since there are only 6 possible orderings of three distinct characters, and only one of those is in strict ascending order, the same result would be obtained by throwing a die n times and showing the proportion of 6’s. \$\endgroup\$ – Nick Kennedy Nov 10 at 13:37
  • \$\begingroup\$ @TheSimpliFire that makes them unobservable. For example, I think tio.run/##K/qfZvs/rTQvuSQzP0@jQjMtJz@/… would give the correct answers but doesn’t generate any strings. \$\endgroup\$ – Nick Kennedy Nov 10 at 13:39
  • 1
    \$\begingroup\$ Outputing both a list of ascending word, and the quotient k/n seems a bit overkill. Maybe it makes sense to only output the ascending words? The quotient k/n can easily be computed from the list of ascending words. But perhaps one shouldn't change the challenge once its posted.. \$\endgroup\$ – Wisław Nov 10 at 14:17
  • 4
    \$\begingroup\$ What is the point of "must be truncated to two decimal places? \$\endgroup\$ – Jonathan Allan Nov 10 at 19:39
3
\$\begingroup\$

Jelly, 31 bytes

13 bytes to cater for the truncation requirement!

ØaẊ⁸СḊḣ€3ṢƑƇðL÷+⁵×ȷ2ḞṾs2j”.Ḋṭ⁸

A monadic link which, given an integer, yields a list.

Try it online! (footer joins the list with spaces)


If (1) we remove the truncation requirement and (2) "The letters must come from the English alphabet" implies that we may choose any subset of three or more such letters then this 16 works:

“ḃ»Ẋ⁸С⁼ƇḢ$ðL÷;⁸

Try this one (only uses A, B, and C)


If we may also avoid the unobservable creation of all the strings as part of of our code. then this 15 works:

6ẋX€ỊƇ“ḃ»ẋðL÷;⁸

Try this one (only uses A, B, and C, and does not create any unordered strings)

\$\endgroup\$
  • \$\begingroup\$ Although it seems an unnecessary requirement, the current spec calls for truncation at 2 decimal places. \$\endgroup\$ – Nick Kennedy Nov 10 at 19:32
  • \$\begingroup\$ Thanks, I didn't notice that, what an odd requirement. Updated. \$\endgroup\$ – Jonathan Allan Nov 10 at 19:41
  • \$\begingroup\$ sorry to be a pain, but the official spec is to truncate rather than round. I don’t think Jelly has a built in, so (unless I’m missing something) it’s either convert to string and head, or it’s multiply by 100, floor, divide by 100. \$\endgroup\$ – Nick Kennedy Nov 10 at 19:42
  • \$\begingroup\$ LOL what a mess \$\endgroup\$ – Jonathan Allan Nov 10 at 19:46
  • \$\begingroup\$ Neither of those suggestions work if 1 must prodce 1.00 either. \$\endgroup\$ – Jonathan Allan Nov 10 at 19:50
3
\$\begingroup\$

Japt, 25 bytes

;ÆBö3Ãf_¶ñ
u»UÊ/N)h2)ú0,4

Try it

\$\endgroup\$
  • \$\begingroup\$ 20? \$\endgroup\$ – Shaggy Nov 10 at 22:16
  • \$\begingroup\$ @Shaggy I never knew that you could divide an array like that, and I forgot about N. Nice! \$\endgroup\$ – Embodiment of Ignorance Nov 10 at 22:21
  • \$\begingroup\$ Hooray for JS's implicit casting! \$\endgroup\$ – Shaggy Nov 10 at 22:23
  • \$\begingroup\$ Wait, N.h() won't work here due to the stupid & unnecessary requirement that the result be truncated to 2 decimal places. \$\endgroup\$ – Shaggy Nov 10 at 22:36
  • \$\begingroup\$ @Shaggy Fixed. It's really convenient that N.h() returns a string \$\endgroup\$ – Embodiment of Ignorance Nov 11 at 1:18
2
\$\begingroup\$

Pyth, 26 bytes

FNQKO.PG3IqKSK=ZhZK;.RcZQ2

Try it online!

Does what OP wants, +4 bytes due to truncation requirement.

How it works

FNQKO.PG3IqKSK=ZhZK;.RcZQ2
FNQ                        - For N in Input
   K                       - K is equal to
    O.P 3                  - A random permutation length 3 of...
       G                   - The lowercase alphabet
         IqKSK             - If K is sorted
              =ZhZ         - Increment Z (which is autoinitialized to 0)
                  K        - Implicitly print K
                   ;  cZQ  - End loop, float division of Z by the input
                    .R   2 - Round to 2 decimal points

Pyth, 24 bytes

FNQI!O6=ZhZSO.PG3;.RcZQ2

Try it online!

Cheat answer but since it only saves two bytes im not gonna bother. Also +4 due to truncation

\$\endgroup\$
  • \$\begingroup\$ How does the second one cheat? The two seem to behave the same \$\endgroup\$ – Embodiment of Ignorance Nov 11 at 22:10
  • \$\begingroup\$ It doesnt generate strings and check if theyre sorted; it only generates sorted strings with a 1/6 chance (which is the probability that a random string of length 3 will be sorted) \$\endgroup\$ – frank Nov 12 at 17:26
2
\$\begingroup\$

Python 3, 131 bytes

It seems like having to print the string complicates the program...

from random import*
n=int(input())
o=[]
a=['a','b','c']
x=0
for i in range(n):
 o+=[sample(a,3)]
 x+=a==o[-1]
print('%.2f'%(x/n),o)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 90 bytes BD \$\endgroup\$ – Dead Possum Nov 10 at 13:13
  • 1
    \$\begingroup\$ Please see the (hopefully final) edit. \$\endgroup\$ – TheSimpliFire Nov 10 at 14:18
  • \$\begingroup\$ Along with fixing to fit the new spec with respect to printing the strings it also needs to truncate rather than round (1->1.00, 0.166667->0.16, 0->0.00). \$\endgroup\$ – Jonathan Allan Nov 10 at 20:24
1
\$\begingroup\$

J, 44 bytes

((4j2":%~),@,~[:u:97+26/:~@?~]#3:)1#.0=?@$&6

Try it online!

Note: This one does not actually construct the strings (the one below does) but its output has an identical probability distribution. I agree with Nick Kennedy on this point.


J, 48 bytes

(];~4j2":(%~#))[:<@u:"1[:(97+(-:/:~)"1#])26?~#&3

Try it online!

Note: Because the required output does not have a homogeneous data type, J requires us to box it.

\$\endgroup\$
  • \$\begingroup\$ Strings in your first submission dont seem to be in ascending order \$\endgroup\$ – frank Nov 11 at 2:00
  • \$\begingroup\$ @frank ty. fixed now. \$\endgroup\$ – Jonah Nov 11 at 5:21

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