Program to keep track of success percentage

Avi Gross avigross at verizon.net
Sat Dec 8 17:54:07 EST 2018


Tim,

This may be a bit awkward.

I am not sure a question on the python list expects to get a one-liner, let alone in an unrelated language like AWK. Unless you install other environments like Cygwin, AWK does not tend to be available of platforms like Windows. Ditto for PERL and other languages or tools you want to use this way.

There are times people appreciate something sophisticated or clever or even innovative.

My personal take on this question is of someone who does not necessarily know what they need to do, let alone in python. And they did not specify how the data is supplied or whether it is a one-time thing.

The two numbers needed could be supplied on the command line and gotten using argv or gotten using two input statements or read from a file or stdin. Or they could be asking for a cumulative set of wins/losses to be contiuously evaluated and as soon as the threshold of 31% is attained, print a message. OR, there could be a previously agreed upon number of games, such as 164, and you get win/loss criteria some way and you want the percentage of wins divided by the 164 to be 31%. 

My GUESS is they are getting a stream of win/win/loss/win/loss/... type of data, maybe one per line of text, maybe as 0 versus 1, but who knows. If we knew exactly, it might lead to trivial solutions. For example, if they had a series of 0/1 you could do a sum() and a count() and divide. 

But what if the scenario was to provide a score like 5 - 3 using whatever notation. Now you need to figure out if your side won and perhaps deal with ties before counting and dividing.

Too many scenarios are possible but the simple answer is more like someone else said.

Count the number of wins, or keep track of it.
Count the total you need.
Divide one by the other and compare it to 0.31 or 31 depending on your calculation method.

Your solution in AWK assumes  lots of things. You assume the data is either on stdin or comes from automatically opening file names on the command line to generate a stream of lines. You assume a win is any line containing one or more lower or upper case instances of the letter W. You let AWK count lines and store that in NR and assume anything without a W is a loss. You print a running commentary and  only at the end of input state if they exceeded 31%.

Now that is quite possibly a way to go. But the data may not be set up that way or they may want to quit as soon as the threshold is reached or there may be blank lines or a notation for a tie. Some of those may not be one-liners. Yours would read much better if spaced out, but you might have written it this way when you were 😉

But, as mentioned, this is a python board. Would you care to do the same thing as a brief program in that language.

If we had that pesky := operator, maybe. 

Just kidding.

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On Behalf Of Tim Chase
Sent: Saturday, December 8, 2018 4:00 PM
To: Musatov <tomusatov at gmail.com>
Cc: python-list at python.org
Subject: Re: Program to keep track of success percentage

On 2018-12-08 10:02, Musatov wrote:
> I am thinking about a program where the only user input is win/loss. 
> The program let's you know if you have won more than 31% of the time 
> or not. Any suggestions about how to approach authoring such a 
> program? Thanks. --

Can be done with an awk one-liner:

awk '/[wW]/{w+=1}{printf("W: %i L: %i %i%%\n", w, NR-w, w * 100/NR)}END{if (w * 100/NR > 31) print "More than 31% winning"}'

-tkc



--
https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list