Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get range percentage of a length string

I'm facing a problem. I'm learning c++. I took a poker context ; The goal is to get a certain hand range from a string length ; the string represents all starting hands in a poker game , in the order stronger first.

 string hand = "AA,KK,QQ,JJ,AKs,AKo,AQs,AQo,TT,AJs,ATs,AJo,KQs,KJs,KTs,QJs,ATo,QTs,JTs,A9s,A9o,KQo,A8s,A8o,A7s,A7o,A6s,A6o,A5s,A5o,A4s,99,A4o,A3s,A2s,KJo,J9s,T9s,Q9s,QJo,KTo,Q9o,88,77,66,QTo,A3o,A2o,JTo,K9s,K8s,K7s,K6s,K5s,K4s,K3s,K2s,Q8s,Q7s,Q6s,Q5s,K9o,J8s,T8s,98s,97s,87s,86s,76s,96s,75s,65s,64s,J9o,T9o,55,54s,53s,52s,K8o,43s,32s,42s,J7s,T7s,K7o,44,33,22,Q4s,Q3s,Q2s,J6s,J5s,T6s,T5s,J4s,K6o,Q8o,J8o,T8o,98o,97o,87o,85s,K5o,K4o,K3o,K2o,95s,74s,76o,65o,54o,84s,94s,Q7o,J7o,T7o,Q6o,J3s,T4s,J2s,Q5o,T3s,T2s,Q4o,J6o,86o,T6o,96o,93s,Q3o,74o,84o,75o,64o,T2o,94o,53o,93o,63o,43o,92o,73o,83o,52o,82o,42o,62o,72o,J5o,63s,92s,73s,Q2o,J4o,83s,85o,82s,T5o,95o,J3o,62s,T4o,J2o,72s,T3o,32o";

The length of the string hand is 662 ; To get the range we need to exclude a percentage of the starting hand and exclude a percentage of the ending hand .

So if I need the last 10% hands, I will exclude first 90% . if I need between 30% and 60% , I will exclude first 29% and last 39% .

It's difficult because the string cannot cut anywhere, a good output should be for example 72o,J5o,63s,92s,73s,Q2o,J4o,83s, with or without the comma to the end, this is not the most important.

I tried to create a substring like this :

  int startpos = 0;
  int stoppos= (662 * 7) / 100;
  string str2 = hand.substr(startpos, stoppos);
  cout <<  str2 << endl;

But this not the answer to the range problem. It gets only the first X%, and the cut is bad , output is : AA,KK,QQ,JJ,A and should be : AA,KK,QQ,JJ,AKs

I spent many hours on this. I'm open for advice, directions, and even a solution ..

Regards, gui

like image 493
guillaume importexport Avatar asked Nov 17 '25 18:11

guillaume importexport


1 Answers

Another approach: find the indexes of the substring we're interested in, and extract that substring. Not much shorter, though.

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  // percentage markers to keep, 30% -- 60% here
  int start = 30, stop = 60;
  string hands = "AA,KK,QQ,JJ,AKs,AKo,AQs,AQo,TT,AJs,ATs,AJo,KQs,KJs,KTs,QJs,\
ATo,QTs,JTs,A9s,A9o,KQo,A8s,A8o,A7s,A7o,A6s,A6o,A5s,A5o,A4s,99,A4o,A3s,A2s,\
KJo,J9s,T9s,Q9s,QJo,KTo,Q9o,88,77,66,QTo,A3o,A2o,JTo,K9s,K8s,K7s,K6s,K5s,K4s,\
K3s,K2s,Q8s,Q7s,Q6s,Q5s,K9o,J8s,T8s,98s,97s,87s,86s,76s,96s,75s,65s,64s,J9o,\
T9o,55,54s,53s,52s,K8o,43s,32s,42s,J7s,T7s,K7o,44,33,22,Q4s,Q3s,Q2s,J6s,J5s,\
T6s,T5s,J4s,K6o,Q8o,J8o,T8o,98o,97o,87o,85s,K5o,K4o,K3o,K2o,95s,74s,76o,65o,\
54o,84s,94s,Q7o,J7o,T7o,Q6o,J3s,T4s,J2s,Q5o,T3s,T2s,Q4o,J6o,86o,T6o,96o,93s,\
Q3o,74o,84o,75o,64o,T2o,94o,53o,93o,63o,43o,92o,73o,83o,52o,82o,42o,62o,72o,\
J5o,63s,92s,73s,Q2o,J4o,83s,85o,82s,T5o,95o,J3o,62s,T4o,J2o,72s,T3o,32o";

  int nhands = count(hands.begin(), hands.end(), ',');
  start = start * nhands / 100;     // percentage -> comma count
  stop = stop * nhands / 100;       // percentage -> comma count

  int start_ = start, stop_ = stop; // copies
  int i = 0, substr_start, substr_end;

  for (i = 0; i < hands.length() && start_ > 0; ++i) {
    if (hands[i] == ',')
      start_--;
  }
  substr_start = i;

  stop_ -= start - 1;               // already counted 'start' commas
  for (; i < hands.length() && stop_ > 0; ++i) {
    if (hands[i] == ',')
      stop_--;
  }
  substr_end = i;

  cout << hands.substr(substr_start, substr_end - substr_start - 1) << endl;
  return 0;
}
like image 55
Roshan Mathews Avatar answered Nov 20 '25 09:11

Roshan Mathews