Calculate the difficulty of a Code Golf question

43

3

Briefing

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

formula

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:

clamp

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 seperated string or as seperate 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.

Shaun Wild

Posted 2016-08-17T12:09:05.320

Reputation: 2 329

@SeanBean Trailing spaces allowed? – Luis Mendo – 2016-08-17T12:38:49.667

@LuisMendo Yes. – Shaun Wild – 2016-08-17T12:40:56.520

3I find that LaTeX harder to understand that a simple formula string.. but whatever the majority wants I guess.. – Shaun Wild – 2016-08-17T13:07:35.207

1Will v or a ever be negative? – Leaky Nun – 2016-08-17T13:16:05.763

3You should almost add [[tag:underhanded]] for the question being underhanded. – Adám – 2016-08-17T13:28:26.860

Given the current formula, unanswered questions will always have a difficulty score of 10 (assuming the convention that dividing a positive number by zero gives positive infinity). This feels unfair to me. – Joe Z. – 2016-08-17T14:05:23.287

5This is a Code Golf question. Not an actual system being implemented into the site. Who cares if it's unfair? – Shaun Wild – 2016-08-17T14:11:33.763

Also I don't know if all the answers below actually work with 0 questions. – Joe Z. – 2016-08-17T14:13:35.497

13its kinda early so I may be missing something here, but why /700 * 10 instead of /70? – Kevin L – 2016-08-17T14:21:36.107

4@KevinL Ssshhhh ;) – Shaun Wild – 2016-08-17T14:34:46.823

1@Adám Tag wiki for [tag:underhanded]: This tag exists for historical reasons. New questions of the type previously posted under this tag are not welcome. – haykam – 2016-08-17T19:24:34.783

What should occur when views and/or answers ==0? (granted, views==0 implies answers==0, but hey). – Delioth – 2016-08-17T19:52:23.570

It doesn't matter really. – Shaun Wild – 2016-08-17T20:49:38.223

3@Peanut I know, but here the OP isn't calling for underhanded answers, it is the OP's challenge which is underhanded: SeanBean incredibly manages to fool most (all?) the experienced golfers into writing unnecessarily long code! – Adám – 2016-08-18T05:22:01.467

1You should add a test case where v/a is evenly divisible by 70, e.g., 70, 1, 1. – Dennis – 2016-08-18T08:01:27.063

I was really hoping that this would be a competition for writing code that tries to predict some measure of difficulty based on the text of the question. – Mark S. – 2016-08-18T12:41:08.920

@MarkS. I'll try and think of a question based around that idea if you like? Or you can do it yourself :P – Shaun Wild – 2016-08-18T13:52:22.757

How do you build a code to test how hard this question is?... – None – 2016-08-18T15:55:21.710

@DonielF I just edit the question everynow and again, running it through my version of the program. – Shaun Wild – 2016-08-18T15:59:47.007

@Sean That was a failed joke. Apparently you missed the humor in it. Oh well. – None – 2016-08-18T16:23:54.537

@DonielF Care to explain? – Shaun Wild – 2016-08-18T20:16:19.697

Will v or a ever be zero? What should the outputs be in those cases? – FryAmTheEggman – 2016-08-20T22:15:46.440

Answers

49

JavaScript (ES6), 40 39 bytes

v=>a=>"**********".substring(10-v/a/70)

Because substring provides the required clamping and "ceil"ing behaviour. Edit: Normally I'm too lazy to bother, but because it got 4 upvotes, I've followed @MarsUltor's advice to save 1 byte by currying.

Neil

Posted 2016-08-17T12:09:05.320

Reputation: 95 035

Oh, that is a nice use of substring :-) – Dylan Meeus – 2016-08-17T12:32:11.020

7Use currying: v=>a=> – ASCII-only – 2016-08-17T12:53:22.450

3Can you use substr instead? I know the second parameter makes a difference, but not sure about the first... – Dom Hastings – 2016-08-17T18:25:52.593

1@DomHastings: Yes, though slice would be even shorter. – Yay295 – 2016-08-17T18:57:33.367

5@DomHastings No, both substr and slice interpret a negative argument as counting back from the end of the string. – Neil – 2016-08-17T19:07:38.553

Ah of course, I forget they're so different, always assume they're pretty much synonyms. Thanks for confirming! – Dom Hastings – 2016-08-17T19:32:49.227

Ah, I didn't notice it could be negative. – Yay295 – 2016-08-18T13:54:21.310

Got it! 37 bytes: v=>a=>"**********".slice(0,v/a/70+.5) – Yay295 – 2016-08-18T14:03:21.807

@Yay295 I don't think that works for v=71, a=1. – Neil – 2016-08-18T14:14:41.183

Dangit, you're right. v=>a=>"**********".slice(0,v/a/70-.5|0+1) fixes that, but it's 41 bytes. – Yay295 – 2016-08-18T14:24:04.323

This answer has got four(ty two) upvotes. So you were right to use currying. – wizzwizz4 – 2016-08-21T15:52:50.143

38

I've been wanting to do this for a while...

HTML + CSS 491 487 485 bytes

-4 bytes thanks to Conor O'Brien
-2 bytes thanks to Releasing Helium Nuclei

Input is taken as the width and height of the page window; width being the number of Views, and height being the number of Answers.

<style>p{overflow:hidden;width:1ch}@media(max-aspect-ratio:70/2){p{width:1ch}}@media(max-aspect-ratio:70/3){p{width:2ch}}@media(max-aspect-ratio:70/4){p{width:3ch}}@media(max-aspect-ratio:70/5){p{width:4ch}}@media(max-aspect-ratio:70/6){p{width:5ch}}@media(max-aspect-ratio:70/7){p{width:6ch}}@media(max-aspect-ratio:70/8){p{width:7ch}}@media(max-aspect-ratio:70/9){p{width:8ch}}@media(max-aspect-ratio:7/1){p{width:9ch}}@media(max-aspect-ratio:70/11){p{width:10ch</style><p>**********

You can try it in your browser by entering

data:text/html,<style>p{overflow:hidden;width:1ch}@media(max-aspect-ratio:70/2){p{width:1ch}}@media(max-aspect-ratio:70/3){p{width:2ch}}@media(max-aspect-ratio:70/4){p{width:3ch}}@media(max-aspect-ratio:70/5){p{width:4ch}}@media(max-aspect-ratio:70/6){p{width:5ch}}@media(max-aspect-ratio:70/7){p{width:6ch}}@media(max-aspect-ratio:70/8){p{width:7ch}}@media(max-aspect-ratio:70/9){p{width:8ch}}@media(max-aspect-ratio:7/1){p{width:9ch}}@media(max-aspect-ratio:70/11){p{width:10ch</style><p>**********

as a url in a new tab.

Yay295

Posted 2016-08-17T12:09:05.320

Reputation: 650

11+1 for thinking out of the box – ehm, thinking of the box... – Adám – 2016-08-18T05:24:58.340

2Do you need the closing p tag? – Conor O'Brien – 2016-08-18T06:21:58.473

I love how it updates as I change the window's size. – YSC – 2016-08-18T11:36:47.703

@ConorO'Brien: Good point! – Yay295 – 2016-08-18T13:27:14.303

1You don't need the last two }s too. – betseg – 2016-08-18T23:24:08.933

12

05AB1E, 11 bytes

/70/î'*T×s£

Explanation

/            # divide v by a
 70/         # divide by 70
    î        # round up, call this n
     '*T×    # push 10 asterisks
         s£  # take n up to 10 asterisk
             # implicitly print

Try it online

Emigna

Posted 2016-08-17T12:09:05.320

Reputation: 50 798

12

Javascript (ES6), 37 36 bytes

v=>a=>"*".repeat((v/=a*70)<9?v+1:10)

Saved 1 byte by currying, thanks to TheLethalCoder

let F=
v=>a=>"*".repeat((v/=a*70)<9?v+1:10)

console.log("Test #1:", F(163)(2))    // **
console.log("Test #2:", F(548)(22))   // *
console.log("Test #3:", F(1452)(24))  // *
console.log("Test #4:", F(1713)(37))  // *
console.log("Test #5:", F(4162)(32))  // **
console.log("Test #6:", F(3067)(15))  // ***
console.log("Test #7:", F(22421)(19)) // **********

Arnauld

Posted 2016-08-17T12:09:05.320

Reputation: 111 334

3Can you use v=>a=> instead of (v,a)=>? – TheLethalCoder – 2016-08-17T14:56:51.487

@TheLethalCoder - Updated. Thanks! – Arnauld – 2016-08-17T15:02:54.857

2Doesn't work for v=70, a=1 does it? – Neil – 2016-08-17T19:09:14.887

1@Neil - That's right. It is off by 1 point if the number of answers is an exact divisor of the number of views. Or put in other words, it's anticipating the next view. ;-) – Arnauld – 2016-08-18T10:55:35.807

11

Mathematica, 38 35 bytes

StringRepeat["*",10,⌈#/#2/70⌉]&

Thanks to @MartinEnder for 3 bytes

for Monica

Posted 2016-08-17T12:09:05.320

Reputation: 1 172

1Hello, and welcome to PPCG! This is a great answer! – NoOneIsHere – 2016-08-17T15:06:18.500

@NoOneIsHere Thanks! Originally I was thinking of Clip, which has pretty much the same syntax as OP's Clamp, but then I saw that StringRepeat has the optional third argument for truncation. – for Monica – 2016-08-18T06:47:16.867

3There are Unicode characters for the left and right ceiling bracket which together make up only 6 bytes instead of the 9 you need for Ceiling[]. – Martin Ender – 2016-08-18T07:31:16.090

9

EXCEL, 29 bytes

If you count Excel as a representation of VBA Excel, then you can use

=REPT("*",MIN(1+v/(70*a),10))

where v and a are the name of reference cells.

Anastasiya-Romanova 秀

Posted 2016-08-17T12:09:05.320

Reputation: 1 673

3Hi, and welcome to PPCG! This is a nice first post! And valid also. – Rɪᴋᴇʀ – 2016-08-20T22:08:46.657

Hi too @EasterlyIrk. Thanks for the warm welcome :) – Anastasiya-Romanova 秀 – 2016-08-20T22:12:43.233

8

CJam, 18 15 14 bytes

Saved 1 byte thanks to Peter Taylor and 3 bytes thanks to Adnan

'*A*q~d/70/m]<

Try it online

'*A*            e# Push "**********"
    q~d/        e# Get the input and divide the two numbers
        70/     e# Divide by 70
           m]   e# Ceil, yielding x
             <  e# Slice the string, taking the first x elements

Business Cat

Posted 2016-08-17T12:09:05.320

Reputation: 8 927

7

C#, 68 49 48 bytes

v=>a=>"**********".Substring((int)(10-v/a/70d));

This is the C# version of this excellent answer by Neil.

Saved another 19 bytes thanks to Neil

TheLethalCoder

Posted 2016-08-17T12:09:05.320

Reputation: 6 930

Try (int)System.Math.Floor(10-v/a/70) or just (int)(10-v/a/70). – Neil – 2016-08-17T13:16:53.327

@Neil Looks like I had to leave the 70d alone but works better thanks – TheLethalCoder – 2016-08-17T13:44:13.737

1Sorry, didn't see the d there. – Neil – 2016-08-17T15:07:45.483

Another answer that could save one byte with currying, I think: v=>a=> – Brian McCutchon – 2016-08-18T03:10:04.573

@BrianMcCutchon Didn't even realise how I could do it in C# thanks – TheLethalCoder – 2016-08-18T08:11:18.670

If C# rounding is the same as Java, then when you cast to an integer, it rounds the double, it doesn't use the ceiling operator. – None – 2016-08-18T11:38:02.737

@Advancid Neither language rounds, as far as I know. What makes you think otherwise? – Neil – 2016-08-18T15:06:39.780

@Neil Sorry I meant remove decimals instead of round. – None – 2016-08-18T15:59:49.160

@Advancid That's why you subtract it from 10 first. 10-ceil(x) == floor(10-x). – Neil – 2016-08-18T18:30:22.083

7

Java 8, 57 bytes

Uses a lambda to save bytes, performs the calculation and substrings to return the answer.

(v,a)->"**********".substring(Math.max(0,(700*a-v)/70/a))

Here is my class for testing it.

public class DifficultyCalculator{
    static interface h{ String f(int v, int a);}
    static void g(h H){
        System.out.print(H.f(163,2));System.out.println("\t**");
        System.out.print(H.f(548,22));System.out.println("\t*");
        System.out.print(H.f(1452,24));System.out.println("\t*");
        System.out.print(H.f(1713,37));System.out.println("\t*");
        System.out.print(H.f(4162,32));System.out.println("\t**");
        System.out.print(H.f(3067,15));System.out.println("\t***");
        System.out.print(H.f(22421,19));System.out.println("\t**********");
    }
    public static void main(String[] args) {
        g( // 70
            (v,a)->"**********".substring(java.lang.Math.max(0,(int)(10-v/70d/a)))
        );
    }
}

Update

  • -3 [16-08-19] Utilized integer division
  • -10 [16-08-18] Removed unnecessary import, thanks to @OlivierGrégoire!
  • -18 [16-08-17] Return string instead of print

NonlinearFruit

Posted 2016-08-17T12:09:05.320

Reputation: 5 334

2Nice, a Java answer that isn't a train! – Ismael Miguel – 2016-08-17T20:40:13.420

4No need for java.lang. since it's the default included package. – Olivier Grégoire – 2016-08-18T07:25:13.200

You are rounding not ceiling! – None – 2016-08-18T11:34:53.470

1@Advancid I tested it and System.out.println((int)2.99); prints 2 and since I take the pre-floored value from 10 and then floor it, it is the same as taking the ceiling away from 10. – NonlinearFruit – 2016-08-18T12:53:24.990

6

MATL, 12 bytes

/70/0:9>42*c

Try it online!

Explanation

The rounding up and clamping are done simultaneously as follows: the number x = v/a/70 is compared against each element of the array [0 1 ... 9]. The numbers of that array that are exceeded by x will become asterisks, and the rest will be spaces.

/      % Take the two numbers implicitly. Divide. (Example: for inputs 3067, 15
       % we get 204.47)
70/    % Divide by 70 (we get 2.92)
0:9    % Push array [0 1  ... 9]
>      % See which of those are exceeded by the previous number (2.92 exceeds
       % 0, 1 and 2, so we get [1 1 1 0 ... 0]). This does the rounding up
       % and the clamping
42*    % Multiply by 42, which is the ASCII code of '*' (we get [42 42 42 0 ... 0])
       % Char 0 will be displayed as space
c      % Convert to char. Implicitly display

Luis Mendo

Posted 2016-08-17T12:09:05.320

Reputation: 87 464

5

Python2, 32 bytes

saved 3 + 2 bytes and corrected off by one error thanks to Leaky Nun

lambda v,a:('*'*10)[:~-v/a/70+1]

similar to Neils answer. Uses the fact that Python2 does integer division.

mathause

Posted 2016-08-17T12:09:05.320

Reputation: 341

fails when v=70 and a=1 – Leaky Nun – 2016-08-17T13:26:51.277

The f= can be removed – Leaky Nun – 2016-08-17T13:27:11.090

v, a can become v,a – Leaky Nun – 2016-08-17T13:27:16.730

thanks! should work now. May be wrong for v=0, a=1 now, but this case cannot exist, can it? – mathause – 2016-08-17T13:50:18.453

That would not be wrong for v=0, a=1. – Leaky Nun – 2016-08-17T13:51:51.093

Instead of using (v-1), you can use ~-v. – Leaky Nun – 2016-08-17T13:52:03.827

thanks - no idea how that works – mathause – 2016-08-17T14:00:11.867

~ is bitwise negation, which turns n to -(n+1). (here for more info) – Leaky Nun – 2016-08-17T14:00:48.900

Why 10 then substring? Multiply with the right amount straight away: `lambda v,a:(''*(~-v/a/70+1))` – Karl Napf – 2016-08-17T15:20:00.057

@KarlNapf beucase ~-v/a/70+1 may generate a number higher than 10 (last test for example) – Rod – 2016-08-17T15:37:46.863

5

Haskell, 35 bytes

v#a=[1..min(ceiling$v/a/70)10]>>"*"

[1..min(ceiling$v/a/70)10] creates a range from 1 to the computed difficulty (an empty list for difficulty 0). a>>b repeats the list b length a often.

Laikoni

Posted 2016-08-17T12:09:05.320

Reputation: 23 676

4

C#, 97 89 87 77 42 41 bytes

v=>a=>new string('*',(v/=a*70)<9?v+1:10);

Saved 10 bytes thanks to Adám

Saved a few bytes thanks to Arnauld

TheLethalCoder

Posted 2016-08-17T12:09:05.320

Reputation: 6 930

You can save a whole lot by replacing (int)System.Math.Ceiling(v/a/70d) by (v+69)/(70*a)... Note that moreover v/a can not be negative, so c can be simplified a lot because you don't need to check for that. – Tom van der Zanden – 2016-08-17T14:20:34.123

4

R, 68, 50 52 bytes

f=function(v,a)cat(rep("*",1+min(v/a/70,10)),sep="")

rep implicitly places a min on number of 0.

Thanks to @plannapus and @Anastasiya-Romanova秀 for spotting my error.

user5957401

Posted 2016-08-17T12:09:05.320

Reputation: 699

You can remove f= – Cyoce – 2016-08-18T18:11:01.763

1Outputs of your code are not equal to the Test Data. You should add 2 more bytes 1+ after min( in order to get the same outputs – Anastasiya-Romanova 秀 – 2016-08-22T07:02:18.650

4

Perl, 35 32 bytes

say"*"x(10-($-=10-pop()/70/pop))

Use -E to activate say and give the arguments in the reverse order:

perl -E 'say"*"x(10-($-=10-pop()/70/pop))' 2 163

If arguments on STDIN are allowed the following is 29 bytes:

(echo 163; echo 2) | perl -pe '$_="*"x(10-($-=10-$_/70/<>))'

Ton Hospel

Posted 2016-08-17T12:09:05.320

Reputation: 14 114

I can't remember if it'd exactly the same, but can you have 0| instead of $-=? (Thinking operator precedence might not be right...) – Dom Hastings – 2016-08-17T18:44:10.967

@DomHastings 0| makes a negative number into a huge number (leading to zero *s), $-= clips to 0 (leading to ten *s), which is what I need here – Ton Hospel – 2016-08-17T18:59:11.933

Ah, of course, it's only ever a positive integer! Thanks for reminding. I'm sure I'll forget that when I need it though... – Dom Hastings – 2016-08-17T19:30:49.867

4

Pyke, 13 9 bytes

/70/\**T<

Try it here!

Explanation:

/         -    num_1 / num_2
 70/      -   ^ / 70
    \**   -  "*" * ^
       T< - ^[:10]

Blue

Posted 2016-08-17T12:09:05.320

Reputation: 26 661

3

Pyth, 17 13 bytes

4 bytes in credit to Luis Mendo for his algorithm.

*\*htS[0T.EccFQ70
*\*s>LccFQ70T

Test suite.

Leaky Nun

Posted 2016-08-17T12:09:05.320

Reputation: 45 011

3

Javascript ES6, 48 bytes

a=>b=>"*".repeat(Math.ceil(Math.min(a/b/70,10)))

user54187

Posted 2016-08-17T12:09:05.320

Reputation:

3

C, 54, 51, 50, 49 bytes

Assuming that v is positive or zero and a positive, the x < min clamping case is never met, since there is no way the result of the ceiling operation can be negative. Additionally, integer maths on non-negative values always yields the floor of the result, so we add 1 to get the ceiling.

This solution requires a write function, works on Linux at least.

F(v,a){write(1,"**********",(v/=a*70)>9?10:v+1);}

Test main:

int main() {
  F(163, 2);
  putchar('\n');
  F(548, 22);
  putchar('\n');
  F(1452, 24);
  putchar('\n');
  F(1713, 37);
  putchar('\n');
  F(4162, 32);
  putchar('\n');
  F(3067, 15);
  putchar('\n');
  F(22421, 19);
  putchar('\n');
}

Stefano Sanfilippo

Posted 2016-08-17T12:09:05.320

Reputation: 1 059

1Replacing (v=v/a/70) with (v/=a*70) saves 1 byte. – ceilingcat – 2016-08-19T06:32:48.023

Nice catch @ceilingcat! – Stefano Sanfilippo – 2016-08-19T09:35:51.757

2

javascript: 82 73 bytes

 (v,a)=>console.log("*".repeat(Math.min(Math.max(0,Math.ceil(v/a/70),10)))
  • saved some bytes after Adám pointed out I overlooked the /700*10=/70, and the removal of parens

Dylan Meeus

Posted 2016-08-17T12:09:05.320

Reputation: 220

@Adám what's with the edit? – Martin Ender – 2016-08-17T12:49:11.333

@Adám If people read any of the answers they'll have the spoiler already. Rolling back, because currently that sentence is fairly useless and just makes people click on the revision history. – Martin Ender – 2016-08-17T13:01:08.080

@Adám That's what I usually use myself but I don't see any harm in the current version. – Martin Ender – 2016-08-17T13:56:10.213

No need to console.log it, returning is fine. You can also save a byte with v=>a=> instead of (v,a)=> – Cyoce – 2016-08-18T18:20:29.463

2

Dyalog APL, 15 bytes

'*'⍴⍨10⌊⌈⎕÷70×⎕

'*'⍴⍨ the character repeated this many times:
10⌊ min(10,...
⎕÷ input divided by
70× seventy times
input

TryAPL online!

Adám

Posted 2016-08-17T12:09:05.320

Reputation: 37 779

Would it be golfier to use Mendo's algorithm? – Leaky Nun – 2016-08-17T13:11:21.123

@LeakyNun I don't think so: '*'/⍨(⎕÷70×⎕)>⍳10 – Adám – 2016-08-17T13:19:17.847

2

Jellyfish, 18 bytes

P
#'*
mM/%i
10 %70

Takes input in the format [a v]. Try it online!

Explanation

  • % is reciprocal, so %70 is 1/70.
  • i is input, as a two-element array.
  • /% with inputs i and %70 reduces the array i by flipped division with initial value %70. In other words, it computes v/(a/(1/70)), which is equal to v / (70*a).
  • M takes the ceiling of this value, and m takes the maximum of that and 10.
  • #'* repeats the literal * character that many times.
  • P prints the result without quotes.

Zgarb

Posted 2016-08-17T12:09:05.320

Reputation: 39 083

2

MATLAB, 34 33 bytes

Because I like this challange so much, here is one for MATLAB (outputs trailing whitespaces):

@(v,a)[(ceil(v/a/70)>0:9)*42,'']

Inspired by @Luis Mendo's answer. Thanks to @pajonk for saving one byte.

mathause

Posted 2016-08-17T12:09:05.320

Reputation: 341

Nice approach! I had a 40-byte one to post... BTW, you can save one byte using [... ''] instead of char(...). And do you really need the ceil when at the end you are comparing with integers? – pajonk – 2016-08-17T15:44:38.423

2thanks @pajonk - one can really learn some things on this site to make my code even less readable ;) – mathause – 2016-08-17T15:56:00.650

2

dc, 110 108 104 98 bytes

This was a doozy since slicing isn't a thing. Also, dc doesn't manipulate strings. I just really was waiting for a string one that would be < 5 hours of coding. On the plus side, I finally started writing down common constructs, like for loops. Also had to formulate rounding/ceiling, so thanks for that.

[42P]sd[dsi[li0!=dli1-dsi0!=L]dsLx]sl[Isi[li0!=dli1-dsi0!=L]dsLx]sg[1+]saIk/70*0k1~0!=adI>ldI!>gIP

Invoked in bash:

echo 'v a (above)'|dc
# Wholly:
>> echo '163 2 [42P]sd[dsi[li0!=dli1-dsi0!=L]dsLx]sl[Isi[li0!=dli1-dsi0!=L]dsLx]sg[1+]saIk/70*0k1~0!=adI>ldI!>gIP'|dc
# outputs:
**
>> 

Replacing (above) with the code, and v and a with their respective counterparts above. The single quotes are important (otherwise you get bash's history stuff).


Explained:

[42P]sd   # Here we store a macro in register d to print 1 * without a newline

[dsi[li0!=dli1-dsi0!=L]dsLx]sl # Store the "less than" case, a for loop which
                        # uses the top-of the stack as it's number of iterations.
[Isi[li0!=dli1-dsi0!=L]dsLx]sg # Store the "greater than" case. It's the above,
                        # but it puts 10 on the stack to use instead.

[1+]sa # Store a macro to add 1 to whatever is the top-of-stack.


Ik # Set precision at non-zero to allow decimal division

/70* # Divide the top two of the stack, v/a; multiply by 70 (`/700*10` == `/70`)
             # dc is postfix and stack-based, so operators come after operands.

0k1~0!=a     # This is a ceiling function.
|> 0k  # set precision to 0 to perform integer division
|> 1~  # push the quotient of integer division by 1, and then the remainder. (r is top)
|> 0!=a # If the top-of-stack (decimal part) is not 0, add 1 to the quotient

dI>ldI!>g # Conditional statement
|> dI>l  # (d)uplicate the top, push 10 on. If 10 > the old top, execute the `l`ess-than
          # case, which loops top-of-stack times.
|> dI!>g # Complement of the above, using the `g`reater-than to loop 10 times.

IP # print a newline

This is probably more golf-able, but I was trying to get it finished to avoid premature optimization.

  • 2 bytes saved by duplicating-saving instead of saving-loading
  • 4 bytes saved dividing by 70
  • 6 bytes from daniero's suggestions (non-strings, ASCII nums instead; 10 => I)

Delioth

Posted 2016-08-17T12:09:05.320

Reputation: 221

[*]n => 42P. Every instance of 10 can be replaced by I. []p => IP – daniero – 2016-08-17T20:50:32.400

2

m4, 136 135 bytes

define(r,`ifelse($1,0,,eval($1>9),1,*`r(9)',*`r(decr($1))')')define(f,`r(ifelse(eval($1%($2*70)),0,eval($1/$2/70),eval($1/$2/70+1)))')

Defines a macro f which takes v and a, and expands to the correct output. Most of the program is an implementation of ceiling.

Program man

Posted 2016-08-17T12:09:05.320

Reputation: 531

2

Haskell, 35 bytes

This solution is as completely different from Laikonis answer as it gets for something this trivial. Yet the score (for now) is exactly the same.

v%a=take(ceiling$v/a/70)[0..9]>>"*" 

This produces ten stars, then shaves off some. Easy to extend to arbitrary difficulty with an infinite list.

I did manage to shave off one more byte. But while all test cases work, this shouldn't be correct in general.

v%a=take(1+div v(a*70))[0..9]>>"*"

MarLinn

Posted 2016-08-17T12:09:05.320

Reputation: 231

2

TI-Basic, 39 bytes

Prompt V,A
sub("**********",1,max(0,min(10,int(V/A/70)+1

Timtech

Posted 2016-08-17T12:09:05.320

Reputation: 12 038

1

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 -> **********

AdmBorkBork

Posted 2016-08-17T12:09:05.320

Reputation: 41 581

1

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

Cody

Posted 2016-08-17T12:09:05.320

Reputation: 447

You do need to include imports, but from math import * will save a couple bytes – NonlinearFruit – 2016-08-17T18:20:10.893

Included the import into the byte count – Cody – 2016-08-17T18:26:10.647

According to the spec, 0 is the minimum stars, not 1. Also save a whole 1 byte by import* with no space. – Program man – 2016-08-17T18:32:32.403

Oops I misread the minimum. Thanks for the tip – Cody – 2016-08-17T18:33:38.740

1@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. – Delioth – 2016-08-17T19:02:11.610

@Delioth I did not consider that 0 views implies 0 answers, so you're absolutely right. – Program man – 2016-08-17T19:40:46.937

1

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!

Beta Decay

Posted 2016-08-17T12:09:05.320

Reputation: 21 478

1

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

Mego

Posted 2016-08-17T12:09:05.320

Reputation: 32 998

1

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.

Samsquanch

Posted 2016-08-17T12:09:05.320

Reputation: 271

1

Ruby, 47 35 bytes

f=->(v,a){'*'*((v/=a*70)<9?v+1:10)}

Called with f.call(v,a)

Saved 12 bytes thanks to user3334690, now the function implicitly returns the result.

Ephi

Posted 2016-08-17T12:09:05.320

Reputation: 111

you can probably save a bit by doing string * number like: "q=->(v,a){''((v/=a*70)<9?v+1:10)}" – user3334690 – 2016-08-19T18:18:52.167

You can safely remove f= – Luis Masuelli – 2016-08-26T13:15:51.843

1

Ruby, 27 bytes

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

Luis Masuelli

Posted 2016-08-17T12:09:05.320

Reputation: 141

1

Perl 6: 32 bytes

As a lambda that takes two arguments:

{"*"x min 10,ceiling $^x/$^y/70}

smls

Posted 2016-08-17T12:09:05.320

Reputation: 4 352

0

Add++, 26 bytes

L^,/700/10*1+i10m1_Vc"*"Gy

Try it online! or see the test suite

It's not often I get to use the ^ function flag in a code golf.

How it works

L^,		; Create a lambda that returns the stack joined
		; Example arguments:	[3067 15]
	/	; Divide;		[204.46]
	700/	; Divide by 700;	[0.2921]
	10*	; Times by 10;		[2.91]
	1+i	; Ceiling;		[3]
	10m	; Minimum with 10;	[3 10 3]
	1_	; Decrement;		[3 10 2]
	Vc	; Keep the top value;	[2]
	"*"G	; Push '*' below;	['*' 2]
	y	; Repeat;		['*' '*' '*']

caird coinheringaahing

Posted 2016-08-17T12:09:05.320

Reputation: 13 702

0

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.

Neil

Posted 2016-08-17T12:09:05.320

Reputation: 95 035

Batch makes my eyes bleed, but nice answer! – Mark K Cowan – 2016-08-18T14:57:34.143