4
\$\begingroup\$

Find the most important character

The challenge is to write a program that outputs the most occurrent character in the input string, excluding these bracketing characters ()[]{}. Each submission should consist only of non-trivial* characters. Each solution is scored using the formula in the scoring section. Bracketing characters are allowed in the program, but may not be selected by the program as the most occurrent character. In cases where letters occur with the same frequency, any or all of the characters may be returned.

*Non-trivial is defined in this case to be a character that contributes to the functionality of the program. If the character can be removed without influencing how the program runs, it is a trivial character.

Scoring

Score is determined as follows:

$$(\frac{n_{char}}{n_{total}}) * 100(\%) $$

With nchar being the number of occurrences of the most frequent character in the input, and ntotal being the total number of non-trivial characters in the input. With this scoring criteria, all scores should be within the range \$[0,100]\$

Highest scoring solution per language wins.

Input

Solutions should take in any valid string as input. A convenient method of determining the most occurrent character in the program itself would be to use the program's source code as input to the program itself. The program does not have to check for non-trivial characters, as they should not be included in the program source.

Output

A single character with the highest percentage occurrence. Each solution should be capable of outputting standard ASCII, as well as the language's codepage (if applicable).

Errata

Creative solutions are encouraged. Boring solutions with long padded strings substituted in for numbers are discouraged. Standard loopholes are disallowed.

Examples

Program:

aabbbc

Output:

b


Program:

aabb

Output:

a or b or ab


Sandbox Link

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 99.999999999999999957%

1111111111111111111 ... (9352835025086960662 1's total) ... 11111g₅B.V

g gets the length of the large integer literal. Obviously this is too long for TIO, so try a version using 9352835025086960662 instead of the equivalent 1111...1111g.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ In theory you can get a higher score by just having worse 05ab1e code after base change? \$\endgroup\$ – Expired Data Feb 12 at 17:45
  • 1
    \$\begingroup\$ @ExpiredData yes, getting arbitrarily close to 100% is trivial. I could also use base 9999 instead of base 255 for extra bloat. This is the highest I can get while sticking to the pseudo-rule Boring solutions with long padded strings are discouraged. \$\endgroup\$ – Grimmy Feb 12 at 17:47
3
\$\begingroup\$

JavaScript (Node.js), 55.85% (or much more)

s=>eval(Buffer((g=n=>n?[+[n&127n],...g(n>>7n)]:[])(0b1001111010011001001100101001101110111000110111101100111110110111101111011110111011010111010011000001111111101101011110001010011011101110001110110111101111111111001011010111101101110111000111011011110111101010000111110011110111000110101100110011101011111011101101110110111001011011111110111110110101001010100010111101011011010111101111011101111011110111011010101000110010111000111100001110110011100001100101111001001011101110011n))+'')

Try it online!

This is a 'fair' version, in that only the payload binary data is included (7 bits per character). But the score could be made arbitrary large by padding each encoded character with as many zeros as desired. They would still be 'non-trivial' as per the challenge rule, as removing any of them would break the decoding.

The binary string is decoded as:

s.replace(m=o=/[^(){}[\]]/g,c=>(o[c]=-~o[c])<m?0:m=o[O=c])&&O
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

PHP, 75%

foreach(count_chars($argn)as$c)$o[]=$c*'1111111111111111111111111111111111111111001111111111111111111111111111111111111111111111111010111111111111111111111111111110101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'[$i++];arsort($o);echo chr(key($o));

Try it online!

Ungolfed:

// table of valid input chars - ASCII 0-255
// index is the ASCII number, and 1 or 0 if it is valid
$valid_chars = '1111111111111111111111111111111111111111001111111111111111111111111111111111111111111111111010111111111111111111111111111110101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111';

// get input string and aggregate count of each letter
$counted_chars = count_chars( $argn );

// iterate through each 
foreach( $counted_chars as $ascii => $count ) {
    // multiply count by either 1 if valid or 0 if not
    $out[] = $count * $valid_chars[ $ascii ];
}

// sort resulting list descending
arsort( $out );

// display the ASCII value of the "most popular" valid char
echo chr( key( $out ) );

Counts the frequency of each character and uses a lookup table to to determine if a given character is excluded (example: ()[]{}) from being counted. Sorts by frequency and returns the "most important" remaining char.

Note: PHP's count_chars() only counts ASCII chars 0-255 which corresponds to the array of valid chars, so is not arbitrarily or unnecessarily long. Using a lookup table of flags to test the validity of a result is, while not the most efficient in this case, a valid and commonly-used programming practice.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

05AB1E, tends to 50%

'""{}[]()"'""м.M"J".V"".V"".V".V

Try it online!

You can keep appending ".V" before the final J, any part of this string can't be removed and hence none of the characters themselves are non-trivial. Hence this tends to 50% of the program being " as you add more ".V"

Note: you couldn't have a single char command to tend to 66% inside the brackets like "J" as it can likely be shortened to ""

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 13.2% ;

float m;a[256];l;t;c;n(char*s){bzero(a,1024);l=0;for(;*s;){a[*s]++;s++;l++;};m=0;t=0;for(;t++<255;)if(a[t]>m)c=t,m=a[t];m*=100;printf("%c %f\n",c,m/l);}

This version doesn't pad the end with semicolons. It only contains a few unneeded ones to push up my score to the next percent.

Feel free to add semicolons up until you get 100%.

Try it online!

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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