Sampai desimalisasi pada tahun 1971 , uang Inggris didasarkan pada pembagian pound menjadi 240 sen. Shilling adalah 12 sen jadi 20 shilling menghasilkan satu pound. Denominasi terkecil adalah kentut di seperempat sen. Ada banyak denominasi dan nama panggilan lain untuk koin, yang bisa sangat membingungkan jika Anda tidak terbiasa dengan sistem.
Tantangan
Tulis program atau fungsi yang dapat mengkonversi (hampir) denominasi uang Inggris lama ke yang lain. Untuk membuatnya lebih mudah bagi pengguna Anda perlu mendukung bentuk jamak dan nama panggilan.
Ini adalah denominasi dan istilah sinonim yang harus Anda dukung. Demi kenyamanan, nilai mereka dalam bidak mengarah pada setiap baris.
1: farthing, farthings
2: halfpence, halfpenny, halfpennies
4: penny, pennies, pence, copper, coppers
8: twopenny, twopennies, twopence, tuppence, half groat, half groats
12: threepence, threepenny, threepennies, threepenny bit, threepenny bits, thruppence, thrupenny, thrupennies, thrupenny bit, thrupenny bits
16: groat, groats
24: sixpence, sixpenny, sixpennies, sixpenny bit, sixpenny bits, tanner, tanners
48: shilling, shillings, bob
96: florin, florins, two bob bit, two bob bits
120: half crown, half crowns
240: crown, crowns
480: half sovereign, half sovereigns
504: half guinea, half guineas
960: pound, pounds, pounds sterling, sovereign, sovereigns, quid, quids
1008: guinea, guineas
(Saya bukan orang Inggris, daftar ini sama sekali tidak berwibawa tetapi cukup untuk tantangan.)
Melalui stdin atau argumen fungsi, Anda harus mengambil string dari formulir
[value to convert] [denomination 1] in [denomination 2]
dan kembali atau cetak
[value to convert] [denomination 1] is [converted value] [denomination 2]
di mana [converted value]
adalah [value to convert]
unit denominasi 1 dikonversi ke denominasi 2.
The [value to convert]
dan [converted value]
yang mengapung positif. Dalam output keduanya harus dibulatkan atau dipotong ke 4 tempat desimal. Jika diinginkan, Anda dapat menganggap [value to convert]
selalu memiliki titik desimal dan nol saat input (misalnya, 1.0
bukan 1
).
Denominasi 1 dan 2 dapat berupa dua istilah dari daftar di atas. Jangan khawatir tentang apakah mereka jamak atau tidak, perlakukan semua denominasi dan sinonim yang sama. Anda dapat menganggap format input dan denominasi selalu valid.
Contohnya
1 pounds in shilling
→ 1 pounds is 20 shilling
( 1.0000 pounds is 20.0000 shilling
akan baik-baik saja)
0.6 tuppence in tanner
→ 0.6 tuppence is 0.2 tanner
24 two bob bits in pounds sterling
→ 24 two bob bits is 2.4 pounds sterling
144 threepennies in guineas
→ 144 threepennies is 1.7143 guineas
Mencetak gol
Kode terpendek dalam byte menang.
sumber
quid
adalahquid
. Kemungkinan besar ini akan sama dengan uang lama. Contoh:Five quid a pint! Cor blimey guvnor
. Pengecualian: quids-inJawaban:
Pyth,
146145More readable (newlines and indents must be removed to run):
Update: Turns out, it's 1 character shorter (no space needed) to chop up the string into a list of 2 character strings before running the string index operation.
/x"string"<b2 2
->xc"string"2<b2
. Nothing else needs to be changed.How it works:
This uses @xnor's approach of looking up the value of currency using its first two letters, as well as the trick of detecting the initial
half
ortwo
, removing it and calling the function again.To lookup the value of the first two characters, it finds the location of the first two letters of the currency in a string, then divides by 2 and takes the value at that index in the list. This is much shorter than an dict in pyth.
Uses the fact that
x
(find within string) returns -1 on failure to avoid puttingpo
(pounds)qu
(quid) orso
(sovereigns) in the string, and simply return the last element of the list, 960, by default.By rearranging the order of currencies in the lookup system and initializing carefully, with
K4
andJ24
, all spaces that would have been needed to separate numbers in the list were removed.Uses pyth's dual assignment operator,
A
, on the input split onin
to get the beginning and end of the input in separate variables.Does essentially the same lookup at the end, though pyth has no
.split(_,1)
, so it is somewhat more cumbersome.Examples:
sumber
<
and>
worked as string/list slice operators; that's much, much better than taking the head or end of a chop :)Ruby,
345306302288287278273253252242232221202190 bytesTakes input from STDIN and prints to STDOUT.
I'm using short regular expressions to match only the desired denominations for each value. There are two arrays, one with regexes and one with values, at corresponding indices. The regex array is a space delimited array literal, and the value array is packed into a string of UTF-8 characters.
I'm selecting the index into the values by searching for a regex that matches each denomination. I'm also defaulting to the tuppence/half-groat case (value 8), because that required the longest regex. Likewise, some of the patterns assume that other values have already been matched by earlier patterns, so each regex only distinguishes the desired value only from the remaining ones. Using this, I could probably shave off another couple of bytes by rearranging the order of the denominations.
Thanks to Ventero for helping me
beat Pythmaking it shorter!sumber
s[k]
) that overwrites$1
etc. You can save a few characters by moving the map block into a lambda and calling that directly in the last line (which also allows you to drop the assignments for$1
and$2
). Also.index
is shorter than.find_index
.Regexp.new k
→/#{k}/
and$><<gets.sub(/foo/){a=$3;...}
→gets[/foo/];a=$3;puts...
for a total of 221. And you can of course use the old trick of packing the int array into a string (using.pack("U*")
) and then indexing into that string. Should get you down to 195 chars/200 bytes.a=gets[/foo/,3]
Python 3:
264239 charsThe function
f
gets the shilling value of the currency-stringc
by fingerprinting the first two lettersusing the dictionaryby finding them in a string. The prefixes "half" and "two" are detected and accounted for by chopping the prefix and the space and applying a multiplier. Since "halfpenny" lacks a space after "half", this results in "enny", but that's handled with a fictional "en" entry.Thanks to @isaacg and @grc for lots of improvement on the dictionary lookup.
sumber
2/4**(c<'t')
part..get(c[:2],960)
to lookup the value from the dictionary and omitting thepo=960,so=960,qu=960,
entries from the dictionary.Python 2 - 345
358Requires input number to be a float in python i.e.
144.1
I think this could be shortened in python 3...
...Confirmed thanks to @xnor. Also confirmed that having a better algorithm matters a lot ;)
sumber
q=raw_input().split(' in ')
byq,b=raw_input().split(' in ')
h+' gr':8
andh+' g':504
depending on who is evaluated first for half groatsu
to the guinea one...Haskell - 315 bytes
sumber
JavaScript (ES5), 344
I went with a hash function approach... I think I underestimated (relatively) how complex the input processing would be (over the regex approach, that wouldn't care about the number).
sumber
Based on @FryAmTheEggMan's answer, with a different way of testing
str.startwith
:Python 2: 317
sumber
print
and the formatted string. You can also rewrite the lambda ass=lambda x:x and C.get(x,s(x[:-1]))or 0
to save a character (along with the spaces). This is a pretty neat idea, btw :)and/or
thing.u.split(' ')
sayu.split(' ',1)
for currencies that have spaces, like "half sovereign"., 1
!x and y or 0
can be shortened in general tox and y
, since both evaluate to0
or equivalentlyFalse
whenx
is Falsey.JavaScript ES6, 264
273Show code snippet
This gets the value of each currency by checking it against various regexes, starting with the broadest
/t/
; the value is overwritten if another match is encountered. There might be a way to shave off a couple bytes by reordering the regex string. You can test it using the snippet above (It is formatted only to use dialog boxes and remove ES6 arrow functions so everyone can test the code easily). Thanks to Alconja for the suggestions.sumber
't0sh|bo0^p....'.split(0)
, 4 more by using.map
instead of.forEach
and 3 more by callingc(0)
andc(1)
and doings[d].match