将百分比转换为“简单”比率


16

您经营着一个政治网站,并且确定与以百分比(“ 71%” )表示的选举时相比,赢得选举或失败选举的机会与比例(“ 7 分之 5”)对人们有更好的直觉理解。)。

但是,您也不想显示诸如“ 82中的58”之类的令人迷惑的比率,即使它们不太精确,也希望它们更容易理解。

因此,给定一个介于0.1%和99.9%之间的百分比,请使用以下规则返回最接近的“易于理解”比率“ x in y

  1. 大多数值(请参见下面的异常)应返回最接近的比率,即10或更低。55%的用户应返回“ 5 in 9”,而不是“ 11 in 20”。
  2. 比率应降至最低水平。65%的用户应返回“ 2 in 3”,而不是“ 4 in 6”。
  3. 小于10%的值应返回n中的1最接近比率,其中n是(10,12,15,20,30,40,50,60,70,80,90,100)中的一个。例如,6%应返回“ 15分之一”。
  4. 超过90%的值应返回最接近的比率,形式为“ n-1 in n ”,其中n是(10,12,15,20,30,40,50,60,70,80,90,100)中的一个。例如,98.7%应该返回“ 79 in 80”。
  5. 小于1%的值应返回“ <100分之一
  6. 超过99%的值应返回“ > 99 in 100

或者,换一种方式考虑,您的程序应该从以下可能的输出中返回最接近的比率(为方便起见,我将它们的近似值包括在内):

<1 in 100
 1 in 100  = 1.00%
 1 in 90   = 1.11%
 1 in 80   = 1.25%
 1 in 70   = 1.43%
 1 in 60   = 1.67%
 1 in 50   = 2.00%
 1 in 40   = 2.50%
 1 in 30   = 3.33%
 1 in 20   = 5.00%
 1 in 15   = 6.67%
 1 in 12   = 8.33%
 1 in 10   = 10.00%
 1 in 9    = 11.11%
 1 in 8    = 12.50%
 1 in 7    = 14.29%
 1 in 6    = 16.67%
 1 in 5    = 20.00%
 2 in 9    = 22.22%
 1 in 4    = 25.00%
 2 in 7    = 28.57%
 3 in 10   = 30.00%
 1 in 3    = 33.33%
 3 in 8    = 37.50%
 2 in 5    = 40.00%
 3 in 7    = 42.86%
 4 in 9    = 44.44%
 1 in 2    = 50.00%
 5 in 9    = 55.56%
 4 in 7    = 57.14%
 3 in 5    = 60.00%
 5 in 8    = 62.50%
 2 in 3    = 66.67%
 7 in 10   = 70.00%
 5 in 7    = 71.43%
 3 in 4    = 75.00%
 7 in 9    = 77.78%
 4 in 5    = 80.00%
 5 in 6    = 83.33%
 6 in 7    = 85.71%
 7 in 8    = 87.50%
 8 in 9    = 88.89%
 9 in 10   = 90.00%
 11 in 12  = 91.67%
 14 in 15  = 93.33%
 19 in 20  = 95.00%
 29 in 30  = 96.67%
 39 in 40  = 97.50%
 49 in 50  = 98.00%
 59 in 60  = 98.33%
 69 in 70  = 98.57%
 79 in 80  = 98.75%
 89 in 90  = 98.89%
 99 in 100 = 99.00%
>99 in 100

其他规定:

  • 数值输入可以在0.1到99.9的范围内或在0.001到0.999的范围内,以较方便的为准。您必须处理至少3个有效数字。
  • 您必须输出比率(“ 3 in 4”),而不是等效分数(“ 3/4”)。
  • 如果有两个比率接近输入,则您的程序可以返回一个比率。7.5%可能会返回“ 12分之一”或“ 15分之一”。
  • 前导/尾随空白和/或换行都可以

例子

Input  :   Output
 0.5   :  <1 in 100
 1.0   :   1 in 100
 1.5   :   1 in 70
 7.5   :   1 in 15  or  1 in 12 (either is acceptable)
 9.2   :   1 in 10
13.1   :   1 in 8
29.2   :   2 in 7
29.3   :   3 in 10
52.7   :   1 in 2
52.8   :   5 in 9
72.0   :   5 in 7
73.9   :   3 in 4
88.8   :   8 in 9
90.8   :   9 in 10
94.2   :  19 in 20
98.7   :  79 in 80
98.9   :  89 in 90
99.0   :  99 in 100
99.1   : >99 in 100

这是一个挑战,每种语言中最短的代码将获胜。

(类似于但不重复:将小数转换为小数最近的小数n位精度的近似浮点数


If there are two ratios equally close to the input, your program can return either one. 7.5% could return "1 in 12" or "1 in 15"这是否意味着我们也可以返回7 in 100?Btw,1 in 14在这种情况下更接近输入。
DimChtz

@DimChtz不,违反规则3(在10%以下的值应表示为“1 Ñ ”,对于特定的可能值Ñ)。
BradC

哦,我没注意到。好的。
DimChtz

2
如果我们可以将分子和分母输出为元组/列表之类的任何格式,我就很喜欢它,但是已经有了竞争的答案,所以我认为现在为时已晚。不过,对于将来的挑战,我会考虑使用更灵活的I / O格式,因为当您需要字符串处理时,某些语言比其他语言失去了更多的竞争力。
HyperNeutrino

1
@BradC-大声笑。我当时只有538岁,我全是“哇!我得以此挑战高尔夫!”
Chas Brown

Answers:


6

T-SQL,385个字节

SELECT TOP 1IIF(i>.99,'>',IIF(i<.01,'<',''))+n+' in '+d
FROM t,(SELECT ISNULL(PARSENAME(value,2),'1')n,PARSENAME(value,1)d FROM
STRING_SPLIT('100,90,80,70,60,50,40,30,20,15,12,10,9,8,7,6,5,2.9,4,2.7,3.10,3,3.8,2.5,3.7,4.9,2,5.9,4.7,3.5,5.8,2.3,7.10,5.7,3.4,7.9,4.5,5.6,6.7,7.8,8.9,9.10,11.12,14.15,19.20,29.30,39.40,49.50,59.60,69.70,79.80,89.90,99.100',','))m
ORDER BY ABS(i-ABS(n)/d)

根据我们的IO标准,通过带有数字字段i的预先存在的表t进行输入。

该输入表与一个内存表结合在一起,该内存表是通过STRING_SPLIT(分隔行)和PARSENAME(通过分隔分子和分母.)从字符串中解析出来的。

该表按距输入值i的距离排序,并返回第一行,格式正确。


5

木炭,84字节

NθF¹¹«F⊖ι⊞υ⟦⊕κι⟧≔⎇⊖ι∨×χι¹²¦¹⁵ιF²⊞υ⟦∨κ⊖ιι⟧»≔Eυ↔⁻θ∕§ι⁰§ι¹η≔⌕η⌊ηη×<‹θ·⁰¹×>›θ·⁹⁹⪫§υη in 

在线尝试!链接是详细版本的代码。将输入作为小数而不是百分比。说明:

Nθ

输入分数。

F¹¹«

ñ=0ñ=10

F⊖ι⊞υ⟦⊕κι⟧

产生比率1个ññ-1个ñ

≔⎇⊖ι∨×χι¹²¦¹⁵ι

ñŤH121520100ñ

F²⊞υ⟦∨κ⊖ιι⟧»

生成比率ñ-1个ñ1个ñ

≔Eυ↔⁻θ∕§ι⁰§ι¹η

计算所有比率的十进制值,并取与原始输入的绝对差。

≔⌕η⌊ηη

1个224

×<‹θ·⁰¹

<0.01

×>›θ·⁹⁹

>0.99

⪫§υη in 

将适当比例的分子和分母连接在一起in并打印。


5

的JavaScript(ES7),164个 159 144字节

]01个[

r=>(g=m=>--n+11?g((q=n>1?n*10:n+10-~'13'[n],d=((p=r<.1?1:r>.9?q-1:n<0&&r*q+.5|0)/q-r)**2)>m?m:(o=p+' in '+q,d)):r<.01?'<'+o:r>.99?'>'+o:o)(n=11)

在线尝试!

怎么样?

p/q。对于其中每个,我们计算:

d=p/q-[R2

d

我们从最高的价值出发 q

已评论

r => (g = m =>               // r = input; g() = recursive function, taking m = best score
  --n + 11 ?                 // decrement n; if n is still greater than or equal to -10:
    g(                       //   do a recursive call to g():
      ( q =                  //     compute q = denominator:
        n > 1 ?              //       if n is greater than 1:
          n * 10             //         q = n * 10 (20, 30, ..., 100)
        :                    //       else:
          n + 10 - ~'13'[n], //         q = 12 if n = 0, 15 if n = 1, n + 11 if n < 0
        d = ((               //     compute d = (p / q - r)²:
          p =                //       compute p = numerator:
          r < .1 ?           //         if r is less than 0.01:
            1                //           p = 1
          :                  //         else:
            r > .9 ?         //           if r is greater than 0.90:
              q - 1          //             p = q - 1
            :                //           else:
              n < 0 &&       //             if n is negative (i.e. q is in [1,10]):
              r * q + .5 | 0 //               p = round(r * q)
                             //             otherwise: p = 0 (which will be ignored)
          ) / q - r          //       compute p / q - r
        ) ** 2               //       and square the result (cheaper than absolute value)
      ) > m ?                //     if d is greater than m:
        m                    //       leave m unchanged
      : (                    //     else:
        o = p + ' in ' + q,  //       update the output string o
        d                    //       and update m to d
    ))                       //   end of recursive call
  :                          // else (all possible ratios have been tried out):
    r < .01 ? '<' + o :      //   if r is less than 0.01, prefix with '<'
    r > .99 ? '>' + o :      //   if r is greater than 0.99, prefix with '>'
    o                        //   otherwise, just return o
)(n = 11)                    // initial call to g() with m = n = 11

4

果冻,58字节

⁵R×⁵;12,15µ’,1,€)Ẏ;⁵Œc¤ð÷/ạ¥ÞḢj“ in ”
”<”>“”>.99$?<.01$?;Ç

在线尝试!

-16字节归功于Arnauld(可以在前面加上,<>不是重写整个短语)
-6字节归功于Jonathan Allan并修复了错误


@Arnauld哦,您是对的,没想过:P谢谢!
HyperNeutrino,

0.3应该3 in 10不会导致2 in 7
Jonathan Allan '18

您应该删除µµ,不是吗?编辑-然后打高尔夫球ÐṂṂÞḢ
乔纳森·艾伦,

更改9应该可以解决我认为的错误。
乔纳森·艾伦

@JonathanAllan哦,是的,我没有使用10作为有效分母。谢谢。而且不,删除双亩是行不通的,因为然后“最小”附加到二元链接-min函数的右侧,这绝对不是我想要的,但是只放一个亩似乎无法解决它。虽然感谢您的高尔夫:D
HyperNeutrino '18

3

Python 2中261个 278 261 237 177字节

lambda n:' <>'[(n<.01)-(n>.99)]+'%d in %d'%min([(a,b)for b in[[12,15]+r(10,110,10),r(1,11)][.1<n<.9]for a in r([1,b-1][n>.9],[b,2][n<.1])],key=lambda(a,b):abs(1.*a/b-n))
r=range

在线尝试!


1
Python不支持分号吗?除非我错'\n '';',否则您可以替换为...。
开发人员

@BradC固定:)
TF

3

干净224个 198 197字节

import StdEnv,Data.List,Text
t=toReal
$p=if(p<1.0)"<"if(p>99.0)">"""+snd(minimum[(abs(p-t n*1E2/t d),n<+" in "<+d)\\i<-[10,12,15:[20,30..100]],(n,d)<-[(1,i),(i-1,i):diag2[1..10][1..10]]|gcd n d<2])

在线尝试!

解释:

t = toReal                              // give `toReal` a shorter name
$ p
 = if(p < 1.0)                          // if the percentage is less than 1%
  "<"                                   // prepend "<"
 if(p > 99.0)                           // if the percentage is > 99%
  ">"                                   // prepend ">"
  ""                                    // otherwise prepend nothing
 + snd (                                // to the second element of
  minimum [                             // the smallest item in a list composed of
   (                                    // pairs of
    abs (                               // the absolute value of
     p -                                // the difference between the percentage
     t n*1E2 / t d                      // and the ratio
    ) 
   ,                                    // associated with
    n <+ " in " <+ d                    // the string representation of the ratio
   )                                    // in the form of a tuple
   \\ i <- [10, 12, 15: [20, 30..100]]  // for every special denominator `i`
   , (n, d) <- [(1, i), (i - 1, i): diag2 [1..10] [1..10]]
                                        // for every ratio `n` : `d`
   | gcd n d < 2                        // where `n` / `d` cannot be further simplified
  ]
 )

3

果冻 53  52 字节

_.01,.99Ṡµ<0ịØ<ḣE⁵Ż×⁵+12,5Ṡ,’Ɗż€$Ẏ;⁵Œc¤÷/ạ¥Þ³Ḣj“ in 

打印结果的完整程序。

在线尝试!

或查看测试套件

请注意,通过以下方式更改了测试套件以使代码成为单子链接:

  1. 使用寄存器跟踪当前的“程序输入”, ³®; 和
  2. 关闭“ in”中的字符代码列表,用 “ in “ in ”

怎么样?

从强制对<or进行必要打印的代码开始,>然后构建所有分子-分母对的代码(带有一些冗余的非简化形式的版本,所有形式都简化后),并使用稳定的字符打印出最小差异的除法运算项排序与 in

_.01,.99Ṡµ<0ịØ<ḣE⁵Ż×⁵+12,5Ṡ,’Ɗż€$Ẏ;⁵Œc¤÷/ạ¥Þ³Ḣj“ in  - Main Link: number in [0,1], n
 .01,.99                                             - literal pair = [0.01, 0.99]
_                                                    - subtract -> [n - 0.01, n - 0.99]
        Ṡ                                            - sign (vectorises) (-1 if <0; 1 if >0; else 0) 
         µ                                           - start a new monadic link
                                                     -   call that X
          <0                                         - less than zero? (vectorises)
             Ø<                                      - literal list of characters = "<>"
            ị                                        - index into (vectorises) ("<<" if n < 0.01; ">>" if n >= 0.99; else "><")
                E                                    - all (of X) equal? (1 if ((n < 0.01) OR (n > 0.99)) else 0
               ḣ                                     - head to index ("<" if n < 0.01; ">" if n > 0.99; else "")
                                                     -   (the following nilad forces a print of that)
                 ⁵                                   - literal 10
                  Ż                                  - zero-range -> [0,1,2,3,4,5,6,7,8,9,10]
                   ×⁵                                - multiply by 10 -> [0,10,20,30,40,50,60,70,80,90,100]
                      12,5                           - literal pair = [12,5]
                     +                               - add -> [12,15,20,30,40,50,60,70,80,90,100]
                                $                    - last two links as a monad
                             Ɗ                       -   last three links as a monad
                          Ṡ                          -     sign -> [1,1,1,1,1,1,1,1,1,1,1]
                            ’                        -     decrement -> [11,14,19,29,39,49,59,69,79,89,99]
                           ,                         -     pair -> [[1,1,1,1,1,1,1,1,1,1,1],[11,14,19,29,39,49,59,69,79,89,99]]
                              ż€                     -   zip with for €ach -> [[[1,12],[1,15],[1,20],[1,30],[1,40],[1,50],[1,60],[1,70],[1,80],[1,90],[1,100]],[[11,12],[14,15],[19,20],[29,30],[39,40],[49,50],[59,60],[69,70],[79,80],[89,90],[99,100]]]
                                 Ẏ                   - tighten -> [[1,12],[1,15],[1,20],[1,30],[1,40],[1,50],[1,60],[1,70],[1,80],[1,90],[1,100],[11,12],[14,15],[19,20],[29,30],[39,40],[49,50],[59,60],[69,70],[79,80],[89,90],[99,100]]
                                      ¤              - nilad followed by link(s) as a nilad:
                                   ⁵                 -   literal 10
                                    Œc               -   unordered pairs -> [[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[2,3],[2,4],[2,5],[2,6],[2,7],[2,8],[2,9],[2,10],[3,4],[3,5],[3,6],[3,7],[3,8],[3,9],[3,10],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],[5,6],[5,7],[5,8],[5,9],[5,10],[6,7],[6,8],[6,9],[6,10],[7,8],[7,9],[7,10],[8,9],[8,10],[9,10]]
                                  ;                  - concatenate -> [[1,12],[1,15],[1,20],[1,30],[1,40],[1,50],[1,60],[1,70],[1,80],[1,90],[1,100],[11,12],[14,15],[19,20],[29,30],[39,40],[49,50],[59,60],[69,70],[79,80],[89,90],[99,100],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[2,3],[2,4],[2,5],[2,6],[2,7],[2,8],[2,9],[2,10],[3,4],[3,5],[3,6],[3,7],[3,8],[3,9],[3,10],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],[5,6],[5,7],[5,8],[5,9],[5,10],[6,7],[6,8],[6,9],[6,10],[7,8],[7,9],[7,10],[8,9],[8,10],[9,10]]
                                           Þ         - sort by:
                                          ¥          -   last two links as a dyad:
                                                     -       ...(with right argument of
                                            ³        -           the program input, n)
                                        /            -     reduce by:
                                       ÷             -       division
                                         ạ           -     absolute difference
                                             Ḣ       - head
                                               “ in  - literal list of characters " in "
                                              ;      - concatenate
                                                     - implicit print

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.