Lets say we have this existing data:

Total - 10
Won   -  7
Lost  -  3
Longest Winning Streak - 5
Longest Losing Streak - 2

Now you need to write a function which generates an array of random boolean values (true representing a win and false representing a loss) which fulfills the above criteria.

So, in this case the output can be any of the following:



  • Solution must be function which takes in Won, Lost, Longest Winning Streak and Longest Losing Streak and returns a boolean array or string formed of a boolean array like (110011001010). It can also display output instead of returning the generated data.
  • You can safely assume there are no ties/draws/no-results.
  • This is code-golf so shortest code wins.
  • Input format can be in any form (Standard Input/Command-Line,etc.). And the real input format should be - <Won> <Lost> <LWS> <LLS> where <..> are place-holders or they can be separate parameters for the function.. whatever you deem best for your code. The parameter Total is unnecessary. So you needn't use it.
  • As far as random is concerned, you can see that in the example, there are three (and more) possible outputs. Just output any one at random.
  • If you have any other question, ask in the comments below.

Important - Not any answer as of now is correct.

I provide another example (this one's real data) which is possible but not working with the codes in the current answers:

Total - 171
Won   - 111
Lost  -  60
Longest Winning Streak - 10
Longest Losing Streak  - 4

Real Data:


Another possible output (Basically the same, just the third and fourth digits are interchanged):

  • 1
    \$\begingroup\$ What is the input format? \$\endgroup\$
    – Downgoat
    Commented Jan 31, 2016 at 17:03
  • \$\begingroup\$ @Doᴡɴɢᴏᴀᴛ How could I forget that... sorry but now I've posted it. \$\endgroup\$ Commented Jan 31, 2016 at 17:06
  • \$\begingroup\$ Can we take input as an array or separate arguments? \$\endgroup\$
    – Downgoat
    Commented Jan 31, 2016 at 17:06
  • 1
    \$\begingroup\$ How do you measure "random"? I could just go and construct the Array in a deterministic way with the given parameters. If you wanna keep the random requirement, you should add some rules for it. \$\endgroup\$
    – Denker
    Commented Jan 31, 2016 at 17:07
  • 3
    \$\begingroup\$ Why so strict on the input? \$\endgroup\$ Commented Jan 31, 2016 at 17:09

5 Answers 5


JavaScript ES6, 83 70 63 bytes


Try it online

  • \$\begingroup\$ Now this doesn't work for (50, 20, 10, 7). \$\endgroup\$ Commented Jan 31, 2016 at 20:26

Python3 - 239 238 bytes

from itertools import*
P=lambda A,B:len(max(A.split(str(B)),key=len))
for C in product("01",repeat=W+L):
 if P(C,0)==O and P(C,1)==S and W==C.count("1")and L==C.count("0"):

Well, this is way too long, but it works. Very slow. Takes the inputs from STDIN as whitespace separated in the same order as OP does.


Input: 7 3 5 2
Output: 0011011111

Input: 15 10 15 5
Output: 0000011111111111111100000

Pyth - 33 bytes


Try it online here.

Rejection testing, very slow.


Java 7, 319 317 bytes

import java.util.*String c(int a,int b,int c,int d){String r="",w="",l="";List z=new ArrayList();int i,q=a+b-c-d;for(i=;i++<c;w+=1);for(i=0;i++<d;l+=0);for(;a---c>0;z.add(1));for(;b---d>0;z.add(0));Collections.shuffle(z);for(i=0;i<q;r+=z.get(i++));return new StringBuilder(r).insert(new Random().nextInt(q),w+l)+"";}

NOTE: The winning streak and losing streak will always be right after one-another, everything else, including their positions in the resulting string, is random.

Ungolfed & test cases:

Try it here.

import java.util.*;
class M{
  static String c(int a, int b, int c, int d){
    String r = "",
           w = "",
           l = "";
    List z = new ArrayList();
    int i,
        q = a+b-c-d;
    for(i = 0; i++ < c; w += 1);
    for(i = 0; i++ < d; l += 0);
    for( ; a-- - c > 0; z.add(1));
    for( ; b-- - d > 0; z.add(0));
    for(i = 0; i < q; r += z.get(i++));
    return new StringBuilder(r).insert(new Random().nextInt(q), w+l)+"";

  public static void main(String[] a){
    System.out.println(c(7, 3, 5, 2));
    System.out.println(c(171, 111, 10, 4));

Possible output:


R - (I don't know how to calculate bytes)

 a <- read.csv("a.csv")
 b <- seq(1,1, seq.length = a$3)
 a$1 <- a$1-a$3
 c <- seq(0,0, seq.length = a$4)
 a$2 <- a$2 - a$4
 d <- a$1 + a$2
 while(d >= 0){append(b,c(1,0)); d <- d-2}

where a.csv is a file with the four inputs separated by commas. This program will output a long string of 1's (for the win streak), and then a long string of 0's for the loss streak, and finally a long string of 0,1 for the rest of the games.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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