48
\$\begingroup\$

Briefing

The difficulty of a Code Golf question can be calculated as such:

$$\text{clamp} \left( \left\lceil \left( \frac v a \div 700 \right) \times 10 \right\rceil, 0, 10 \right)$$

Where \$v\$ is the number of views a question has

and \$a\$ is the number of answers a question has

and \$⌈x⌉\$ is the ceiling operator.

Also:

$$\text{clamp}(x, min, max) = \begin{cases} min & x < min \\ x & min \le x \le max \\ max & max < x \end{cases}$$

This question's current difficulty: ***

Task

Write a program that will take two integers (v and a) and output the difficulty in asterisks (*).

The input can be in the form of an array, a separated string or as separate function arguments

Test Data

Views Answers Difficulty Program Output 163 2 2 ** 548 22 1 * 1452 24 1 * 1713 37 1 * 4162 32 2 ** 3067 15 3 *** 22421 19 10 ********** 

Example with pseudocode

v: 1713 a: 37 out = clamp(ceil(((v/a)/700)*10), 0, 10); // evaluates to 1 //program will output '*' 

The shortest code in bytes wins! Trailing/ leading spaces are allowed.

\$\endgroup\$
22
  • 3
    \$\begingroup\$ I find that LaTeX harder to understand that a simple formula string.. but whatever the majority wants I guess.. \$\endgroup\$ Commented Aug 17, 2016 at 13:07
  • 3
    \$\begingroup\$ You should almost add [underhanded] for the question being underhanded. \$\endgroup\$ Commented Aug 17, 2016 at 13:28
  • 6
    \$\begingroup\$ This is a Code Golf question. Not an actual system being implemented into the site. Who cares if it's unfair? \$\endgroup\$ Commented Aug 17, 2016 at 14:11
  • 19
    \$\begingroup\$ its kinda early so I may be missing something here, but why /700 * 10 instead of /70? \$\endgroup\$ Commented Aug 17, 2016 at 14:21
  • 5
    \$\begingroup\$ @KevinL Ssshhhh ;) \$\endgroup\$ Commented Aug 17, 2016 at 14:34

42 Answers 42

1
2
1
\$\begingroup\$

PowerShell v2+, 47 bytes

-join(('*'*11)[1..($args[0]/$args[1]/70+.499)]) 

Somewhat a port of @Neil's JavaScript answer.

Takes input $args and divides them, then divides that by 70, and adds .499. Since PowerShell does banker's rounding, this is effectively ceil to two decimal points of precision. If additional precision is required, tack on as many additional 9s as required.

Along with the 1.., this forms a range index into a string. The string is '*'*11, i.e. '***********'. That results in a char-array, so we -join it together back into a string. That string is left on the pipeline and output is implicit. Like Neil's answer, this effectively "clamps" the output to be between 1 and 10 stars.

Test Suite

PS C:\Tools\Scripts\golfing> @(@(163,2), @(548,22), @(1452,24), @(1713,37), @(4162,32), @(3067,15), @(22421,19))|%{($_-join', ')+" -> " +(.\difficulty-of-a-question $_[0] $_[1])} 163, 2 -> ** 548, 22 -> * 1452, 24 -> * 1713, 37 -> * 4162, 32 -> ** 3067, 15 -> *** 22421, 19 -> ********** 
\$\endgroup\$
1
\$\begingroup\$

Python 3, 69 68 bytes

I didn't want to copy the Python 2 answer, so mine is slightly longer.

from math import* x=lambda v,a:print(max(0,min(ceil(v/a/70),10))*'*') 

Saved 1 byte thanks to Program man

\$\endgroup\$
6
  • \$\begingroup\$ You do need to include imports, but from math import * will save a couple bytes \$\endgroup\$ Commented Aug 17, 2016 at 18:20
  • \$\begingroup\$ Included the import into the byte count \$\endgroup\$ Commented Aug 17, 2016 at 18:26
  • \$\begingroup\$ According to the spec, 0 is the minimum stars, not 1. Also save a whole 1 byte by import* with no space. \$\endgroup\$ Commented Aug 17, 2016 at 18:32
  • \$\begingroup\$ Oops I misread the minimum. Thanks for the tip \$\endgroup\$ Commented Aug 17, 2016 at 18:33
  • 1
    \$\begingroup\$ @Programman Though the spec says 0 is minimum, division and multiplication of non-negative, non-zero integers is guaranteed to != 0, and the ceiling operator will make anything between 0-1 and make it 1. Although I suppose there could be the case of 0 views, however 0 views implies 0 answers, which leads to undefined behavior (division by 0). It's likely provable that 0 is impossible and should not even be mentioned. \$\endgroup\$ Commented Aug 17, 2016 at 19:02
1
\$\begingroup\$

Actually, 14 bytes

:70a\\u9ukm'** 

Try it online!

Takes advantage of the fact that 0 views and 0 answers is impossible, and thus ceil(v/a) > 0.

Explanation:

:70a\\u9ukm'** :70 push 70 ([70 a v]) a invert stack ([v a 70]) \\ integer division twice ([v//a//70]) u add 1 ([v//a//70 + 1]) 9uk push 10, make stack into list ([[v//a//70+1, 10]]) m minimum of list '** push "*", repeat 
\$\endgroup\$
1
\$\begingroup\$

Fourier, 46 bytes

All division in Fourier is integer division, so I just add one after division.

I*10/I/700^~X<0{1}{0~X}X>10{1}{10~X}X(42ai^~i) 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP 5.6, 66 bytes

function a($v,$a){echo str_repeat('*',max(0,min(10,1+$v/$a/70)));} 

First we simplify the equation to v/a/70. From there we add 1 since PHP will use this number as an integer for the str_repeat, essentially doing abs($number+1). Then we use the hand-rolled clamp function of max($min_number, min($max_number, $the_number)) to keep it between 1 and 10.

The substr version is a few bytes shorter (due to not having to have to build in the clamp functionality), but this one was more fun to make.

\$\endgroup\$
1
\$\begingroup\$

Ruby, 27 bytes

->v,a{(?**10)[1..(v/a/70)]} 
\$\endgroup\$
1
\$\begingroup\$

Perl 6: 32 bytes

As a lambda that takes two arguments:

{"*"x min 10,ceiling $^x/$^y/70} 
\$\endgroup\$
1
\$\begingroup\$

Add++, 17 bytes

L^,/70/i9b<"*"$yp 

Try it online!

Not often I get to use the ^ flag in

How it works

L^,		; Create a lambda that returns the stack joined 		; Example arguments:	[3067 15] 	/	; Divide;		[204.46] 	70/	; Divide by 70;		[2.921] 	i	; Floor;		[2] 	9b>	; Minimum with 9;	[2] 	"*"	; Push '*';		[2 '*'] 	y	; Repeat with an extra;	['*' '*' '*'] 
\$\endgroup\$
1
\$\begingroup\$

Japt, 12 bytes

Aç* Æ/V/#F¨Y 

Try it

\$\endgroup\$
0
\$\begingroup\$

Batch, 81 bytes

@set/an=(700*%2-%1)/%2/70,n*=!(n^>^>31) @set s=********** @call echo %%s:~%n%%% 

Port of my JavaScript answer, except that Batch uses integer arithmetic, so I wrote the formula as (700*a-v)/a/70 which will truncate towards zero, and then the n*=!(n^>^>31) clears n if it is negative.

\$\endgroup\$
2
  • \$\begingroup\$ Batch makes my eyes bleed, but nice answer! \$\endgroup\$ Commented Aug 18, 2016 at 14:57
  • \$\begingroup\$ I could probably save a byte by using " instead of two ^s but I can't be bothered to check. \$\endgroup\$ Commented Aug 3, 2024 at 8:55
0
\$\begingroup\$

Thunno 2, 10 bytes

/70/ṃ'*T×ɱ 

Try it online!

Explanation

/70/ṃ'*T×ɱ '# Implicit input / # Divide v by a 70/ # Divide this by 70 ṃ # Take the ceiling '*T× '# Push 10 asterisks ɱ # Take that many characters # (with a maximum of 10) # from the string # Implicit output 
\$\endgroup\$
0
\$\begingroup\$

Uiua, 13 bytes

▽:@*↧10⌈÷×70 

Try it!

▽:@*↧10⌈÷×70 ÷×70 # divide v by 70a ⌈ # ceiling ↧10 # clamp to 10 ▽:@* # convert to *s 
\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.