Tugas Anda adalah menulis sebuah fungsi atau program yang mengambil dua bilangan bulat non-negatif i
dan k
( i
≤ k
), dan mencari tahu berapa banyak nol yang akan Anda tulis jika Anda menulis semua bilangan bulat dari i
hingga k
(inklusif) dalam basis pilihan Anda pada sebuah karya dari kertas. Keluarkan bilangan bulat ini, jumlah nol, ke stdout atau serupa.
-30%
jika Anda juga menerima argumen ketiga b
, basis integer untuk menuliskan angka-angka. Setidaknya dua basis harus ditangani untuk mencapai bonus ini.
- Anda dapat menerima input di pangkalan apa pun yang Anda suka, dan Anda dapat mengubah pangkalan di antara kotak uji.
- Anda dapat menerima argumen
i
,k
dan secara opsionalb
dalam urutan yang Anda suka. - Jawaban harus menangani setidaknya satu pangkalan yang bukan unary.
Kasus uji (dalam basis 10):
i k -> output
10 10 -> 1
0 27 -> 3
100 200 -> 22
0 500 -> 92
Ini adalah kode-golf; byte paling sedikit menang.
code-golf
number
base-conversion
counting
Filip Haglund
sumber
sumber
Jawaban:
Jelly, 1 byte
Ini menggunakan basis
k+2
, dalam hal ini ada 0 iff tunggali
adalah 0. Dibutuhkan dua argumen, tetapi menerapkan logika TIDAK untuk hanya yang pertama.Jika kami tidak ingin menipu:
7 byte - 30% = 4,9
-1.1 poin oleh @Dennis
Ini mendapat bonus.
sumber
Python 2, 36 byte
Kredit untuk ikan lumpur untuk trik ``.
sumber
05AB1E,
31 byteUses base
k+2
like the Jelly answer, Code:Explanation:
3 byte non-cheating version:
Code:
Explanation:
The bonus gives me 3.5 bytes due to a bug:
Explanation:
Uses CP-1252 encoding.
sumber
Japt, 3 bytes
Uses base
k+2
, as the Jelly answer. There is a zero iffi==0
. Test it online!Better version,
108 bytesThis one uses base 10. Test it online!
Bonus version,
1412 bytes - 30% = 8.4Sadly, with the golfing I did, the bonus is no longer worth it... Test it online!
How it works
sumber
ES6,
9186 - 30% = 60.2 bytesOr save 3 (2.1) bytes if b doesn't need to default to 10.
Best non-bonus version I could do was 65 bytes:
Edit: Saved 5 bytes by using @edc65's zero-counting trick.
sumber
Seriously, 10 bytes
Explanation:
Try it online!
With bonus: 11.9 bytes
Try it online!
Explanation:
sumber
CJam,
12103 bytesThis uses the shortcut @ThomasKwa does.
If this is not allowed, then here is a 10 byte answer.
Nice and short! Works like @Mego's Seriously answer.
Thanks @Dennis!
Had fun writing my first CJam answer!
Try it here!
sumber
T-SQL, 394 Bytes (No bonus)
I figure 'why not', right?
And the friendly one:
sumber
Ruby, 46 - 30% = 32.2 bytes
You could probably golf this more, but at least I get the 30% bonus!
...or without the bonus (27 bytes.)
Tips are welcome, still learning this whole "Ruby" thing.
sumber
(i..k)
is as good as[*i..k]
in the first case.Brachylog, 26 bytes
Takes input as a list
[i,k]
.Explanation
sumber
Julia, 48 bytes - 30% = 33.6
This is a function that accepts three integers and returns an integer. One of the arguments specifies the base, so this qualifies for the bonus.
Ungolfed:
Implementing the bonus yields a score just barely better than the not implementing it (34 bytes):
sumber
Seriously, 2 bytes
This might be taking the Jelly answer trick to the limit, but here is a simple 2 byte Seriously answer.
Try it online!
sumber
Pyth, 6.3 bytes, with bonus (9 bytes - 30%)
Explanation:
Try it here
Or 7 bytes without the bonus:
Explanation:
Try it here
Or use a test suite
sumber
/sjRQ}EE0
PHP, 50 Bytes
supports decimal only
supports decimal and binary with Bonus 63
supports decimal,hexadecimal, octal and binary with Bonus 77.7
supports base 2 - 36 with Bonus 78.4
sumber
JavaScript (ES6), 50 (71 - 30%)
No bonus, base k+2 is 10 bytes
(i,k)=>+!i
No bonus, unary is 8 bytes
(i,k)=>0
TEST
sumber
o='0'
before the loop your code continues to work even whenk<i
.(i ≤ k)
. Update I tried this but in fact it does not work for k<iJolf, 7 bytes
Replace
♂
with\x11
. Try it here!sumber
Lua 74 bytes
There's gotta be a more effective way to do this...
I thought I was really onto something here:
But alas... It keeps getting longer and longer as I realize there's more and more zeroes I forgot about...
sumber
APL, 22 bytes
This is a monadic function that accepts the range boundaries on the left and right and returns an integer.
Ungolfed:
Try it here
sumber
Haskell, 29 bytes
I'm using base 10.
Usage example:
100 # 200
->22
How it works: turn each element in the list from
i
tok
into it's string representation, concatenate into a single string, take a1
for every char'0'
and sum those1
s.sumber
MATL, 7 (10 bytes − 30% bonus)
Try it online!
This works in release 11.0.2, which is earlier than this challenge.
Explanation
sumber
Matlab: 27 bytes
creates a vector from lower number to larger one, then converts all numbers to string and counts all the '0' symbols.
sumber
Python 3, 52.
Tried to implement the bonus, but it doesn't seem to be worth it.
With test cases:
sumber
Perl 6, 23 bytes
$^i..$^k
).comb
is a Str method ).comb(/0/)
)+
)Usage:
sumber
Mathematica, 39 bytes, 27.3 with bonus
sumber
C# 112 Bytes
sumber
PHP, 84 bytes *.7=58.8 (bases 2 to 36)
or
takes decimal input from command line arguments; run with
-r
.sumber
<?=0
supports unary and alphabetic. ;)PowerShell,
5654514842 bytesTakes input, creates a range with
$i..$k
then-join
s that together into a string, followed by a regex-split
command that separates the string into an array by slicing at the0
s. We encapsulate that with().count-1
to measure how many zeros. That's left on the pipeline, and output is implicit.Saved 6 bytes thanks to @ConnorLSW
Try it online!
Base-handling in PowerShell is limited and doesn't support arbitrary bases, so I'm not going for the bonus.
sumber
param($i,$k)(-join($i..$k)-split'0').Length-1
works for me, -3, or use.Count-1
to save even more, haven't tested that yet though.'0'
, so that trimmed off a few more.Java 8, 102 bytes - 30% = 71.4
Why not.
Without the bonus, 96 bytes (so the bonus actually improves my score!):
This implements the following:
sumber
Clojure,
5049 bytesOh regex is shorter than filtering. Original:
Very basic, uses the set of character
\0
to remove others and counts how many were found.sumber