อธิบายทศนิยม


12

รับทศนิยมในรูปแบบของ<float>, <precision>คุณวาดการแสดงกราฟิกของส่วนทศนิยม (เช่นเศษส่วน) ของการลอย ตัวอย่าง:

  1. อินพุต: 6.75, 4, เอาต์พุต:

    6||| 7
     ---- 
    

    6.75(หมายเลขแรกจากอินพุต) คือหมายเลขที่จะอธิบาย4(หมายเลขที่สองจากอินพุต) คือจำนวนเครื่องหมายขีดกลางด้านล่างไพพ์ 6เป็นพื้น 6.75, 7เป็น 6.75ceiled decimal part of first input number * second input numberจำนวนของท่อเป็น

  2. อินพุต: 10.5, 6, เอาต์พุต:

    10|||   11
      ------
    
  3. อินพุต: 20.16, 12, เอาต์พุต

    20||          21
      ------------
    

    .16 อันที่จริงใช้เวลา 1.92 ท่อ แต่เนื่องจากฉันไม่สามารถวาดท่อ 1.92 ได้ฉันจึงถึง 2

  4. อินพุต: 1.1, 12, เอาต์พุต:

    1|           2
     ------------
    

    .1 ในกรณีนี้คือ 1.2 ท่อดังนั้นจึงปูพื้นเป็น 1 ท่อ

  5. นอกจากนี้กรณีขอบ อินพุต: 5, 4(คือตัวเลขเป็นจำนวนเต็ม) เอาต์พุต:

    5    6
     ----
    

  • จำนวนที่จะอธิบายคือโฟลตเชิงบวกถูก จำกัด ด้วยความสามารถทางภาษาของคุณเท่านั้น
  • จำนวนความแม่นยำเป็นเลขจำนวนเต็มคู่ซึ่งมากกว่า 2 (เช่นความแม่นยำขั้นต่ำที่ต้องการคือ 4) มันอาจมีขนาดใหญ่ตามอำเภอใจเช่นกัน
  • > = n.5ท่อถูกปัดขึ้นเป็นn + 1 (เช่น 1.5 ถูกปัดเศษเป็น 2 และ 2.5 ถูกปัดเป็น 3) <n.5ท่อถูกปัดเศษเป็นn (เช่น 1.4 ถูกปัดเศษเป็น 1 และ 2.4 ถูกปัดเศษเป็น 2)
  • [6.75, 4]ถ้ามันจะสะดวกมากขึ้นสำหรับภาษาของคุณคุณสามารถใช้การป้อนข้อมูลที่เป็นอาร์เรย์เช่น หากคุณป้อนข้อมูลในลำดับที่กลับกันคือ[4, 6.75]โปรดระบุในคำตอบของคุณ

คุณจะเจาะจงมากขึ้นเกี่ยวกับรูปแบบผลลัพธ์ที่ต้องการได้หรือไม่
isaacg

@isaacg ฉันได้แสดงตัวอย่างผลลัพธ์สี่ตัวอย่าง ไม่มีความชัดเจนอะไร
nicael

ดูเหมือนจะมีบางกรณีที่มุมเปิด การป้อนข้อมูลเช่น5.0 4: ไม่ได้วาดจาก5ไป6หรือจาก4ไป5หรือเป็นทั้งที่ยอมรับ? อินพุต1.25 2: มี 0 หรือ 1 |วินาทีและทำไม (เช่นกฎการปัดเศษ) คืออะไร ตัวเลขแรกในอินพุตต้องเป็นค่าบวกหรือไม่ ความแม่นยำและขนาดสูงสุดคืออะไร ตัวเลขที่สองในอินพุตจะต้องเป็นค่าบวกหรือไม่ ถ้ามันเป็นลบเราจะวาดไปข้างหลังหรือไม่?
Peter Taylor

@Peter ชี้แจง
nicael

ฉันไม่คิดว่าคุณครอบคลุมกฏการปัดเศษ
Peter Taylor

คำตอบ:


6

CJam, 32 ไบต์

l~1md@:X*mo'|*XSe]1$)NW$s,S*'-X*

ใช้ความแม่นยำก่อนและทศนิยมวินาทีคั่นด้วยช่องว่าง

เรียกใช้กรณีทดสอบทั้งหมด

คำอธิบาย

l~   e# Read input and evaluate, pushing precision and decimal on the stack.
1md  e# Divmod 1, separating the decimal into integer and fractional part.
@:X  e# Pull up precision, store in X.
*mo  e# Multiply precision by fractional part and round.
'|*  e# Push that many vertical bars.
XSe] e# Pad with length X with spaces.
1$)  e# Copy integer part and increment.
N    e# Push linefeed.
W$   e# Copy integer part.
s,   e# Get number of digits as length of string representation.
S*   e# Push that many spaces, to indent the hyphens correctly.
'-X* e# Push X hyphens.

ดูเหมือนว่าจะทำงานได้ดี
nicael

4

Mathematica, 119 ไบต์

a=ToString;b=Array;a[c=Floor@#]<>{b["|"&,d=Round[#2#~Mod~1]],b[" "&,#2-d],a[c+1],"
"," "&~b~IntegerLength@c,"-"&~b~#2}&

ฉันพยายาม ... การทดสอบ:

In[1]:= a=ToString;b=Array;f=a[c=Floor@#]<>{b["|"&,d=Round[#2#~Mod~1]],b[" "&,#2-d],a[c+1],"\n"," "&~b~IntegerLength@c,"-"&~b~#2}&;

In[2]:= f[6.75, 4]

Out[2]= 6||| 7
         ----

In[3]:= f[10.5, 6]

Out[3]= 10|||   11
          ------

In[4]:= f[20.16, 12]

Out[4]= 20||          21
          ------------

In[5]:= f[1.1, 12]

Out[5]= 1|           2
         ------------

In[6]:= f[5, 4]

Out[6]= 5    6
         ----

คุณอาจจะให้ตัวอย่างที่ใช้งานได้หรือเป็นไปไม่ได้?
nicael


3

Java, 253 206 181 ไบต์

บันทึกแล้ว 47 ไบต์ขอบคุณ @Kenney ด้วยเงื่อนไขและตัวแปรที่ใช้เพียงครั้งเดียวและคัดแยกตัวแปรที่ซ้ำซ้อน

บันทึก 25 ไบต์อีกครั้งด้วย @Kenney โดยการใส่ 2 ลูปกับผู้ประกอบการที่ประกอบไปด้วยสาม

การจัดการสตริงบริสุทธิ์:

เวอร์ชั่นอินไลน์ลูป (181 ไบต์):

String m(float f,int p){int g=(int)f,i=0;String h="",q=""+g;int c=q.length();for(;i<c+p;)h+=i++<c?" ":"-";for(i=c;i<p+c;)q+=i++<c+Math.round((f-g)*p)?"|":" ";return q+(g+1)+"\n"+h;}

รุ่น 4 ลูป (206 ไบต์):

String m(float f,int p){int g=(int)f,i=0;String h="",q=""+g;int c=q.length();for(;i++<c;)h+=" ";for(;i<=c+p;i++)h+="-";for(i=c;i<c+Math.round((f-g)*p);i++)q+="|";for(;i++<p+c;)q+=" ";return q+(g+1)+"\n"+h;}

เวอร์ชันที่ไม่ถูกปรับแต่ง:

String m(float f,int p){
//initialize some useful values, d is the number of pipes needed
int g=(int)f,d=Math.round((f-g)*p),i=0;
String h="",q=""+g;//append the floored value to the pipe string first
int c=q.length();
for(;i<c;i++)h+=" ";//pad hyphen string with spaces for alignment
for(++i;i<=c+p;i++)h+="-";//append hyphens
for(i=c;i<c+d;i++)q+="|";//append pipes
for(;i<p+c;i++)q+=" ";//append spaces for padding
return q+(g+1)+"\n"+h;}//concatenate the strings in order, separating the strings with a UNIX newline, and return it.

การทำงานตัวอย่างเช่นที่นี่ที่ideone.com โปรแกรมเต็มรูปแบบรับข้อมูล STDIN <float>,<precision>เป็น

หมายเหตุ: Math.round(float)รอบของ Java ใช้RoundingMode.HALF_UPเป็นค่าเริ่มต้นซึ่งเป็นพฤติกรรมที่จำเป็นของ OP

ผลลัพธ์ของกรณีทดสอบที่ให้มานั้นต่างกันตรงกับที่ OP ระบุไว้


ฉันหวังว่าคุณจะไม่รังเกียจ! คุณลืมที่จะลบa(ไม่เคยใช้) ตั้งค่าที่คุณที่ 233. คุณสามารถประหยัดอีก 23 ที่จะได้รับที่ 210 ไบต์: แทนที่q.length()ด้วยbช่วยประหยัด int g=(int)f, b=(""+g).length(), c=b, i=0;13: Incrementing iterator อยู่ในสภาพของforบันทึก 6 และอินไลน์d(ใช้ครั้งเดียว) บันทึกที่ int c = b; for(;i++<b;)h+=" "; for(;i++<=b+p;)h+="-"; for(i=c;i<c+Math.round((f-g)*p);i++)q+="|"; for(;i++<p+b;)q+=" ";4:
Kenney

นอกจากนี้มีคนแนะนำให้ใช้ newline จริงแทนลำดับ escape แต่เนื่องจากฉันใช้ Windows นั่นคือ CRLF, 2 ไบต์ต่อไปที่ได้รับ\n
Tamoghna Chowdhury

นีซ - ใช่bกลายเป็นล้าสมัยเช่นกัน ;-) คุณยังสามารถบันทึก 1 ไบต์ในครั้งที่ 2 for(;i++<=c+p;)สำหรับ: คุณสามารถบันทึกไฟล์กับยูนิกซ์ตอนจบบรรทัดบน Windows แต่โชคร้ายJava ไม่อนุญาตให้สายหลาย ..
เคนนีย์

@ เคนนีย์ไม่ ฉันลองมัน มันนำไปสู่ยัติภังค์ที่ไม่ตรงแนว Java ไม่ใช่คนที่เหมาะสมสำหรับงานอยู่แล้ว
Tamoghna Chowdhury

ผมได้รับมันลงไป 181 ไบต์โดยใช้เพียง 2 สำหรับลูป:for(;i<c+p;)h+=i++<c?" ":"-";for(i=c;i<p+c;)q+=i++<c+Math.round((f-g)*p)?"|":" ";
เคนนีย์

3

Javascript ES6, 105 104 ไบต์

(f,p)=>(i=f|0)+("|".repeat(j=(f-i)*p+.5|0)+" ".repeat(p-j))+(i+1)+(`
`+i).replace(/\d/g," ")+"-".repeat(p)

บันทึกไว้ 1 ไบต์ต้องขอบคุณคุณพิมพ์ՊՓԼՃՐՊՃՈԲՍԼอย่างไร


ขออภัยฉันไม่ทราบว่าเครื่องหมายขีดคั่นเป็นส่วนหนึ่งของผลลัพธ์ฉันคิดว่าพวกเขาอยู่ที่นั่นเพื่อให้เห็นภาพของพื้นที่
Neil

(f,p)=>(i=f|0)+("|"[r="repeat"](j=(f-i)*p+.5|0)+" "[r](p-j))+(i+1)+("\n"+i).replace(/\d/g," ")+"-"[r](p)
Mama Fun Roll

โอ้ใช่แทนที่\nด้วยขึ้นบรรทัดใหม่จริง และตรวจสอบให้แน่ใจว่าได้ห่อไว้ในสตริงแม่แบบ
Mama Fun Roll

2

Haskell, 113 ไบต์

(%)=replicate.round
s=show
x!y|(n,m)<-properFraction x=[s n,(y*m)%'|',(y-y*m)%' ',s$n+1,"\n",s n>>" ",y%'-']>>=id

ตัวอย่างการใช้งาน:

*Main> putStrLn $ 20.16 ! 12
20||          21
  ------------

properFractionแยกทศนิยมออกเป็นส่วนจำนวนเต็มและเศษของมัน เอาท์พุทเป็นรายการของชิ้นส่วน (หมายเลขเริ่มต้น, บาร์, ช่องว่าง, ... ) ซึ่งถูกตัดแบ่งเป็นสตริงเดียว (ผ่าน>>=id)


เป็นไปได้ไหมที่จะเห็นตัวอย่างออนไลน์นี้
nicael

@nicael: สาธิต (พร้อมโปรแกรมเสริมmainสำหรับโปรแกรมเต็มรูปแบบ)
nimi

ดูเหมือนว่าทุกอย่างโอเค (btw: ทดสอบที่นั่นคิดว่ามันเป็นคอมไพเลอร์ที่สะดวกกว่า)
nicael

2

MATL , 49 ไบต์

2#1\tYUbiXK*Yo'|'1bX"tnKw-Z"hb1+YUhht4Y2m13*_45+c

ใช้รีลีสภาษา / คอมไพเลอร์6.0.0 ทำงานบน Matlab หรือ Octave

ใช้ตัวเลขในลำดับเดียวกันกับในการท้าทาย

ตัวอย่าง

>> matl
 > 2#1\tYUbiXK*Yo'|'1bX"tnKw-Z"hb1+YUhht4Y2m13*_45+c
 >
> 20.16
> 12
20||          21
  ------------

>> matl
 > 2#1\tYUbiXK*Yo'|'1bX"tnKw-Z"hb1+YUhht4Y2m13*_45+c
 >
> 5
> 4
5    6
 ----

คำอธิบาย

2#1\       % implicit input 1st number. Separate decimal and integer part
tYU        % duplicate integer part and convert to string
biXK*Yo    % input 2nd number. Copy it. Multiply by decimal part of 1st number and round
'|'1bX"    % row vector of as many '|' as needed
tnKw-Z"    % row vector of as many spaces as needed
h          % concat horiontally
b1+YUhh    % integer part of 1st number plus 1. Convert to string. Concat twice
t4Y2m      % detect numbers in this string
13*_45+c   % transform numbers into spaces, and non-numbers into '|'
           % implicitly display both strings

คุณมีล่ามออนไลน์หรือไม่
nicael

ยัง :-( ทำงานบน Matlab หรือ Octave
Luis Mendo

2

Perl, 90 ไบต์

print$f,"|"x($d=.5+($b=pop)*(($a=pop)-($f=0|$a))),$"x(1+$b-$d),$f+1,$/,$"x length$f,"-"x$b

ต้องการอินพุตเป็นอาร์กิวเมนต์บรรทัดคำสั่ง บันทึกในไฟล์ (พูด90.pl) และเรียกใช้เป็นperl 90.pl 6.75 4

ด้วยความเห็น

print $f,                        # floored input (initialized below due to expr nesting)
      "|" x ($d=.5+              # rounded pipe count (`x` operator casts to int)
             +($b=pop)           # second argument  (executed first)
             *( ($a=pop)         # first argument   (executed second)
               -($f=0|$a) )      # minus floored first argument = fractional part
            ),
      $"x(1+$b-$d),              # spaces
      $f+1,                      # floored + 1
      $/,                        # newline
      $"  x length $f,           # 2nd line alignment
      "-" x $b                   # the 'ruler'

1

Stackgoat , 31 27 ไบต์

CFv1%C*D'|^w1P-Y^vHXNY^w'-^

คล้ายกับคำตอบอื่น ๆ ส่วนใหญ่ ฉันจะดูว่าฉันสามารถเล่นกอล์ฟได้มากขึ้นหรือไม่ อินพุตสามารถคั่นด้วยเครื่องหมายจุลภาคคั่นด้วยช่องว่างหรือคั่นเกือบทุกอย่าง

ไม่ใช่การแข่งขันเนื่องจาก Stackgoat เกิดขึ้นหลังจากการท้าทายนี้

คำอธิบาย

CF   // Input, floored, push to stack
v1%  // Decimal part
C*   // Times second part
D    // Duplicate that result
'|^  // Repeat | by previous number
w    // Second input
1P   // Move # of |'s to the top of stack
-    // Subtract
Y^   // Repeat " " by above number
vH   // Ceil first input
X    // Newline
Z+   // Add to 
N    // Get length of first #
Y^   // Repeat by spaces
w'-  // Repeat - second input times

1

Lua, 157 ไบต์

ยาว แต่ไม่พบวิธีแก้ปัญหาที่สั้นกว่านี้

function f(d,n)r=""a=math.floor(d)d,s=d-a,a..r for i=1,#s do r=r.." "end for i=1,n do s,r=s..(i-.5>n*d and" "or"|"),r.."-"end s=s..a+1 return s.."\n"..r end

Ungolfed

function g(d,n)
  r=""
  a=math.floor(d)
  d,s=d-a,a..r                         -- d now contains its decimal part
  for i=1,#s do r=r.." "end            -- padding the hyphens
  for i=1,n
  do
    s,r=s..(i-.5>n*d and" "or"|"),r.."-"
    -- s is concatenated with a "|" if i-.5>n*d, a space otherwise
  end
  s=s..a+1
  return s.."\n"..r
end

คุณสามารถทดสอบ lua ออนไลน์กรณีทดสอบต่อไปนี้อาจมีประโยชน์ :)

function f(d,n)r=""a=math.floor(d)d,s=d-a,a..r for i=1,#s do r=r.." "end for i=1,n do s,r=s..(i-.5>n*d and" "or"|"),r.."-"end s=s..a+1 return s.."\n"..r end
print(f(16.75,4))
print(f(5,4))
print(f(20.16,12))

1

ค, 233 231 ไบต์

#include <stdlib.h>
#include <math.h>
i,n,l;main(c,v)char**v;{double m;l=atol(v[2]);n=(int)(modf(atof(v[1]),&m)*l+0.5);c=printf("%.f",m);for(;i++<l;)putchar(i>n?32:'|');printf("%.f\n",m+1);printf("%*s",c,"");for(;--i;)putchar(45);}

Ungolfed:

#include <stdlib.h>
#include <math.h>
i,n,l;

main(c,v)
char**v;
{
    double m;
    l=atol(v[2]); /* Get length from command line */
    n=(int)(modf(atof(v[1]),&m)*l+0.5); /* Get number of pipes and lower limit */
    c=printf("%.f",m); /* print lower limit */

    /* print pipes and spaces */
    for(;i++<l;)
            putchar(i>n?32:'|');

    /* print upper limit */
    printf("%.f\n",m+1);

    /* print spaces before dashes */
    printf("%*s",c,"");

    /* print dashes */
    for(;--i;)
            putchar(45);
}

1

Python 3, 116 108 ไบต์

def f(F,P):l=int(F);h,p=str(l+1),int((F-l)*P+.5);l=str(l);print(l+"|"*p+" "*(P-p)+h);print(" "*len(l)+"-"*P)

ลิงค์ trinket.io

ขอบคุณ Seeq สำหรับการบันทึกอักขระสองสามตัว

รุ่นแรก:

def f(F,P):
 l=int(F)
 h,s,p=str(l+1)," ",int((F-l)*P+.5)
 l=str(l)
 print(l+"|"*p+s*(P-p)+h)
 print(s*len(l)+"-"*P)

เวอร์ชันที่ไม่ถูกปรับแต่ง:

def frac(F,P):
        low = int(F)
        high = low+1
        pipes = int((F-low)*P+.5)
        print(str(low)+"|"*pipes+" "*(P-pipes)+str(high))
        print(" "*len(str(low))+"-"*P)

คุณช่วยสาธิตการทำงานได้ไหม
nicael

ลิงค์ trinket.io นี้ควรใช้งานได้: trinket.io/python/409b1488f8
Jack Brounstein

อันที่จริงมันใช้อักขระน้อยกว่าเพื่อใช้พื้นที่ตามตัวอักษรมากกว่าการจัดเก็บ ;นอกจากนี้คุณยังสามารถเข้าร่วมสายทั้งหมดที่มี คุณใช้เพียงhครั้งเดียวดังนั้นคุณควรอินไลน์ด้วย ควรบันทึกไว้บ้าง
Seequ

@Seeq ดีจับตัวอักษรในอวกาศ ก่อนหน้านี้ฉันกำลังพิมพ์ช่องว่างภายในช่องว่างที่ท้ายบรรทัดที่สอง หลังจากที่ฉันรู้ว่าไม่จำเป็นฉันไม่ได้ตรวจสอบรหัสเพื่อการประหยัดอีกครั้ง hเป็น trickier สำหรับการเรียงต่อกันและlenฟังก์ชั่นในสองแถวสุดท้ายที่จะทำงานlจะต้องมีสตริงดังนั้นจะต้องถูกแทนที่ด้วยh str(int(l)+1)การตั้งค่าhก่อนการแปลงlจะบันทึกอักขระสองสามตัว
Jack Brounstein
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.