Sample win/loss data generator based on certain criteria

6

1

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:

0011011111
1111101100
1010011111
..........

Rules:

  • 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:

1110110100111111010000101111111100110001100110111101101011111011011011011111111110110001111011010001101011100101111101001011011110101101001100001111101010110001111110111111

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

1101110100111111010000101111111100110001100110111101101011111011011011011111111110110001111011010001101011100101111101001011011110101101001100001111101010110001111110111111

Farhan Anam

Posted 2016-01-31T16:56:36.337

Reputation: 189

1What is the input format? – Downgoat – 2016-01-31T17:03:35.733

@Doᴡɴɢᴏᴀᴛ How could I forget that... sorry but now I've posted it. – Farhan Anam – 2016-01-31T17:06:00.970

Can we take input as an array or separate arguments? – Downgoat – 2016-01-31T17:06:43.647

1How 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. – Denker – 2016-01-31T17:07:02.157

<Total> <Won> <Lost> <LWS> <LLS> is a single string. – Farhan Anam – 2016-01-31T17:07:03.137

3Why so strict on the input? – Addison Crump – 2016-01-31T17:09:08.017

Given your edit, my program could always output the same array if its called with the same parameters multiple times? – Denker – 2016-01-31T17:09:09.807

@DenkerAffe Yes.. no problem with that. same thing is allowed as long as it is correct. – Farhan Anam – 2016-01-31T17:09:45.290

No. Winning streak will be <= total wins <= total matches. similarly losing streak will be <= total losses <= total matches. – Farhan Anam – 2016-01-31T17:11:08.230

So we have to take in all the input data or just some? – Downgoat – 2016-01-31T17:14:40.997

@Doᴡɴɢᴏᴀᴛ Sorry, could you elaborate... :0 – Farhan Anam – 2016-01-31T17:15:47.553

@FarhanAnam In my program, I don't need to take in the Total as input as. Does it still have to be in the input? Like, can I take just 7 3 5 2 for input – Downgoat – 2016-01-31T17:17:23.433

The total is unnecessary really.. didn't notice that. I'll remove it. – Farhan Anam – 2016-01-31T17:19:03.540

Do all outputs need to be equiprobable? "Random" might as well be taken as "choose randomly between two possible outputs – Luis Mendo – 2016-01-31T19:33:27.510

Answers

1

JavaScript ES6, 83 70 63 bytes

(b,c,d,e)=>'1'[r='repeat'](d)+'0'[r](e)+'1'[r](b-d)+'0'[r](c-e)

Try it online

Downgoat

Posted 2016-01-31T16:56:36.337

Reputation: 27 116

Now this doesn't work for (50, 20, 10, 7). – Farhan Anam – 2016-01-31T20:26:31.363

1

Python3 - 239 238 bytes

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

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.

Testcases

Input: 7 3 5 2
Output: 0011011111

Input: 15 10 15 5
Output: 0000011111111111111100000

Yytsi

Posted 2016-01-31T16:56:36.337

Reputation: 3 582

0

Pyth - 33 bytes

J_E.W|n/H1hQnJmeShMd.gekrH8mO2sQY

Try it online here.

Rejection testing, very slow.

Maltysen

Posted 2016-01-31T16:56:36.337

Reputation: 25 023

0

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));
    Collections.shuffle(z);
    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:

1111100011
100100010110111111110011011100101111011010110011011101111010010010101111101111000010010000110100010111001100100110010011101100111111111111110110110101011100111111111100001011111000111011111010010001010001100111110011111011011011101010101011101011011101101001001100100101100111111101

Kevin Cruijssen

Posted 2016-01-31T16:56:36.337

Reputation: 67 575

0

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
 append(b,c)
 d <- a$1 + a$2
 while(d >= 0){append(b,c(1,0)); d <- d-2}
 d

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.

Juan Sebastian Lozano

Posted 2016-01-31T16:56:36.337

Reputation: 431