Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Asked
Modified 23 days ago
Viewed 943 times
5
\$\begingroup\$

Your goal is to write a program which will take an input and multiply it by 2. However, there are some catches.

You will get an input in one of a few forms:

  1. A number (3, 15.6, etc)
  2. A number which is written in english (five, sixty, one hundred fifty three, etc)
  3. A roman numeral (V, IX, MXXV, etc)

You must multiply it by two and output the result in the same format as the input.

You can take input in whatever format you want, but it must be consistent. E.g.: if you choose a string, all your inputs must be a string. If you choose an array of characters, all your inputs must be an array of characters. Etc.

When constructing your numbers, follow these rules:

  1. Numerals should reflect the input. I.e.: if there is not a decimal point in the input, don't include one in the output. If the input is not an integer but the result is an integer it's up to you if you include a decimal point or not.
  2. Construct your written numbers without and (i.e.: one thousand one hundred fifty three instead of one thousand one hundred and fifty three)
  3. Roman numerals should follow this form: https://en.wikipedia.org/wiki/Roman_numerals#Standard_form

Notes

  • The only input which could contain a non-integer will be the numeral (2.5 etc). You will never get a number written in English or roman numerals which is not an integer
  • You only have to handle numbers between 1 and 1000 (inclusive) as an input, regardless of the format. This means your output will be between 2 and 2000.
  • English written numbers will always be lower case
  • Roman numerals will always be upper case

Test Cases

2 -> 4
1.5 -> 3.0 or 3
1.6 -> 3.2
1000 -> 2000
1000.0 -> 2000.0 or 2000
one thousand -> two thousand
five hundred sixty eight -> one thousand one hundred thirty six
one -> two
eight -> sixteen
I -> II
IV -> VIII
CCXLV -> CDXC
M -> MM
\$\endgroup\$
9
  • 9
    \$\begingroup\$ So it seems that Stack Overflow adds comments directly, so I'm going to explain myself. This challenge is just decoding and encoding English numbers and roman numerals. We already have done that in multiple challenges. Unless you are able to prove that this provides a different spin on the topic, it should probably be closed as a duplicate. And no, multiplying by 2 in between is not enough in my opinion. \$\endgroup\$
    Weird Glyphs
    –  Weird Glyphs
    2025-09-17 01:55:58 +00:00
    Commented Sep 17 at 1:55
  • 4
    \$\begingroup\$ I think this isn't enough to be its own challenge. Maybe it's interesting because you have to guess which one it is? But this is pretty weak, just check if the string contains digits or IVXLCDM. If this challenge is allowed, nothing would prevent someone else from making the same challenge, just with one more format added. So my point stands as it is. Other users will probably disagree with that opinion, but I guess we'll see soon enough. \$\endgroup\$
    Weird Glyphs
    –  Weird Glyphs
    2025-09-17 02:02:16 +00:00
    Commented Sep 17 at 2:02
  • 5
    \$\begingroup\$ I think the unique portion of this challenge is remembering which of the three formats the input was in and applying the matching reverse transformation accordingly after the multiplication. Yes, the ×2 is a trivial computation, but the challenge is not in performing the computation but rather in persisting which format was used across a computation. (Certainly that is the portion I'm most struggling with in writing my answer, at least!) \$\endgroup\$
    thecommexokid
    –  thecommexokid
    2025-09-17 13:26:45 +00:00
    Commented Sep 17 at 13:26
  • 1
    \$\begingroup\$ This question is similar to: Match Roman Numerals. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. \$\endgroup\$
    Xcali
    –  Xcali
    2025-09-17 16:28:19 +00:00
    Commented Sep 17 at 16:28
  • 1
    \$\begingroup\$ @Xcali please could you explain specifically what leads you to believe this is a duplicate of the question you linked. Thank you 😊 \$\endgroup\$
    Ted
    –  Ted
    2025-09-18 00:39:18 +00:00
    Commented Sep 18 at 0:39

3 Answers 3

4
\$\begingroup\$

Vyxal, 304 bitsv2, 38 bytes

∆ċ` λ¬ `ðV\-ðV\,-)£E±[Ed|ȧæ[øṘdøṘ|륆?=;ṅd¥†

Try it Online!

Bitstring:

0001010011000011011001111010101011000101111100011110000000110101000001100010100101101100101001111010001100000011011110000100011000101000111010001100110110101101111000010101100110101001001111100100101010100001010010110110011100101010110000011011101000010011001110000011101100010100011001110001110111111000

Honestly, the most annoying thing about this is the lack of punctuation in the english number format and that everything must be a string. I've range coded to compensate for how annoying it is :p.

Explained

∆ċ` λ¬ `ðV\-ðV\,-)£E±[Ed|ȧæ[øṘdøṘ|륆?=;ṅd¥†­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏⁠‎⁡⁠⁢⁡⁢‏⁠‎⁡⁠⁢⁡⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁡⁣‏‏​⁡⁠⁡‌⁢⁣​‎‏​⁢⁠⁡‌⁢⁤​‎‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏⁠‎⁡⁠⁢⁢⁢‏‏​⁡⁠⁡‌⁣⁡​‎‎⁡⁠⁢⁢⁣‏‏​⁡⁠⁡‌⁣⁢​‎‎⁡⁠⁢⁢⁤‏⁠‎⁡⁠⁢⁣⁡‏‏​⁡⁠⁡‌⁣⁣​‎‎⁡⁠⁢⁣⁢‏‏​⁡⁠⁡‌⁣⁤​‎‎⁡⁠⁢⁣⁣‏⁠‎⁡⁠⁢⁣⁤‏⁠‎⁡⁠⁢⁤⁡‏⁠‎⁡⁠⁢⁤⁢‏⁠‎⁡⁠⁢⁤⁣‏‏​⁡⁠⁡‌⁤⁡​‎‎⁡⁠⁢⁢⁣‏‏​⁡⁠⁡‌⁤⁢​‎‎⁡⁠⁣⁡⁡‏⁠‎⁡⁠⁣⁡⁢‏⁠‎⁡⁠⁣⁡⁣‏⁠‎⁡⁠⁣⁡⁤‏⁠‎⁡⁠⁣⁢⁡‏⁠‎⁡⁠⁣⁢⁢‏⁠‎⁡⁠⁣⁢⁣‏‏​⁡⁠⁡‌⁤⁣​‎‎⁡⁠⁣⁡⁢‏⁠‎⁡⁠⁣⁡⁣‏‏​⁡⁠⁡‌⁤⁤​‎‎⁡⁠⁣⁡⁤‏⁠‎⁡⁠⁣⁢⁡‏‏​⁡⁠⁡‌⁢⁡⁡​‎‎⁡⁠⁣⁢⁤‏⁠‎⁡⁠⁣⁣⁡‏⁠‎⁡⁠⁣⁣⁢‏‏​⁡⁠⁡‌­
∆ċ` λ¬ `ðV\-ðV\,-)£                           # ‎⁡Helper function to convert numbers to words that match the challenge criteria:
∆ċ                                            # ‎⁢  Convert the number to words. Normally, this'd be where it ends, but we have to also:
  ` λ¬ `ðV                                    # ‎⁣    Replace " and " with a space
          \-ðV                                # ‎⁤    Replace "-" with a space
              \,-                             # ‎⁢⁡    And remove any commas. The price I have to pay for having grammatically correct numbers.
                  £                           # ‎⁢⁢Store the helper in the register for later.
# ‎⁢⁣Alright, now for the juicy stuff :p
                   E±[Ed                      # ‎⁢⁤If the input is already a number, simply double it and call it a day. No special handling needed.
                      |                       # ‎⁣⁡Otherwise:
                       ȧæ                     # ‎⁣⁢Get the case of the input without spaces. Returns 1 if all uppercase, or 0 if all lowercase. I have to remove spaces otherwise all lowercase is considered mixed-case and returns -1. I want a simple 0/1.
                         [                    # ‎⁣⁣If it's all uppercase (i.e. Roman Numeral):
                          øṘdøṘ               # ‎⁣⁤  Convert from roman numerals, double, and convert back to roman numerals.
                      |                       # ‎⁤⁡Otherwise (i.e. english number):
                                륆?=;ṅ       # ‎⁤⁢  Find the first number where:
                                 ¥†           # ‎⁤⁣    Converting it to words with the helper function
                                   ?=         # ‎⁤⁤    equals the input
                                       d¥†    # ‎⁢⁡⁡  Double the result and convert it back to words with the helper function.
💎

Created with the help of Luminespire.

\$\endgroup\$
0
4
\$\begingroup\$

05AB1E, 123 bytes

н.li“€µ‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿áÓÁÏ“#D9£U.•L¬M%*æsËCA…Å¡Ω‡íÖγ±γk·Θ•#D¦'…§«s„ty«DXâðý9ô.ι)˜„°¡±°#vD¯šXð«y«s‘ðý«}DIk>·<èë.v·Iai.X

Try it online or verify all test cases.

Explanation:

н.li          # If the first letter/digit of the (implicit) input is lowercase:
    “€µ...ý«} #  Push a list in the range ["one","nine thousand nine hundred ninty nine"]
              #  (explained separately below)
     D        #  Duplicate this list
      Ik      #  Pop one copy, and get the 0-based index of the input in it
        >·<   #  Increase it to a 1-based index; double; decrease back
           è  #  Index into the list again
              #  (after which it's output implicitly):
   ë          # Else:
    .v        #  Convert the (implicit) input from a Roman number to an integer
              #  (no-op for numbers)
      ·       #  Double it
       Iai    #  If the input only contains letters:
          .X  #   Convert it back to a Roman number
              #   (after which this Roman number is output implicitly)
              #  (implicit else: output the doubled number implicitly)

Explanation of creating the list in the range ["one","nine thousand nine hundred ninty nine"]:

“€µ‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿áÓÁÏ“
              # Push dictionary string "one two three four five six seven eight nine ten eleven twelve"
 #            # Split it on spaces to a list
  D           # Duplicate this list
   9£         # Only keep the first 9 items, removing ["ten","eleven","twelve"]
     U        # Pop and store this list in variable `X`
.•L¬M%*æsËCA…Å¡Ω‡íÖγ±γk·Θ•
              # Push compressed string "twen thir four fif six seven eigh nine"
  #           # Split it on spaces to a list
   D          # Duplicate
    ¦         # Remove the first item ("twen") from this copy
     '…§«    '# Append dictionary string "teen" to each
   s          # Swap to get the list again
    „ty«      # Append "ty" to each
D             # Duplicate this list as well
 Xâ           # Cartesian product with list `X` of range ["one","nine"]
   ðý         # Join each inner pair with space-delimiter
     9ô       # Split it into sublists of size 9
       .ι     # Interleave it with the ["twenty","ninety"] list
)             # Wrap everything on the stack into a list
 ˜            # Flatten this to a single list of strings
„°¡±°         # Push dictionary string "hundred thousand"
     #        # Split on the space
      v       # Loop over this pair:
       D      #  Duplicate the current list of strings
        ¯š    #  Prepend an empty list [] to this list of strings
        X     #  Push list `X` of range ["one","nine"]
         ð«   #  Append a space to each
           y« #  Append the current "hundred" or "thousand" to each
        s     #  Swap to get the list of strings we've duplicated
         â    #  Cartesian product
          €˜  #  Flatten each inner pair, to get rid of the [] again
          ðý  #  Join each inner pair with space-delimiter
            « #  Merge the two lists together
      }       # Close the loop

See this 05AB1E tip of mine (sections How to use the dictionary? and How to compress strings not part of the dictionary?) to understand why “€µ‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿áÓÁÏ“ is "one two three four five six seven eight nine ten eleven twelve"; .•L¬M%*æsËCA…Å¡Ω‡íÖγ±γk·Θ• is "twen thir four fif six seven eigh nine"; '…§« is "teen"; and „°¡±° is "hundred thousand".

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 269 bytes

¿ΣθI⊗θ«¿‹⌈θaF⁴F⪪”{⊞¶σ?~⧴θπ⪪λpAz” F⪪”{⊞‴▶Lc⧴d~dφW¶″” F⪪”{⊞‴ΣAº⧴θπ⪪λpAz” ⊞υ⁺⁺⁺×Mικλμ«≔⁺⟦⟦⟧⟧⪪”↶⌈ü?↑»lη+:,x&⪫⁻↘ξⅈ↔⟲▶M↖S₂υüXα◨…≡⧴~→*MZv℅№ⅈSU{+ⅉ◨·c↨Xcⅉ×|№O_U⁼züN÷5\δHDKθZ↖×” ηF⪪”↶±!κ⊖⍘~vd⎇¿Σ¦#¬J§c✳“εWWs⊞G⧴lºμ¤TCX” F…ηχ⊞η⪫Φ⟦ικ⟧λ F…ηχF…ηχFη⊞υ⪫Φ⟦⁺ι thousand⁺κ hundredλ⟧μ »§υ⊗⌕υθ

Try it online! Link is to verbose version of code. Handles Roman numerals up to MCMXCIX and English up to four thousand nine hundred ninety nine. Explanation:

¿Σθ

Are there any non-zero digits in the input?

I⊗θ«

If so, then treat it as a decimal number and double it. Otherwise:

¿‹⌈θa

If the input contains no lower case letters, then...

F⁴F⪪... F⪪... F⪪... ⊞υ⁺⁺⁺×Mικλμ«

Generate all the Roman numerals from (0) to 3999 by concatenating entries from compressed strings. Otherwise:

≔⁺⟦⟦⟧⟧⪪... η

Start with the words from one to nineteen, plus the empty list for zero.

F⪪... F…ηχ⊞η⪫Φ⟦ικ⟧λ 

Using the words from twenty to ninety, generate the numbers for twenty to ninety nine, and add those to the list.

F…ηχF…ηχFη⊞υ⪫Φ⟦⁺ι thousand⁺κ hundredλ⟧μ 

Taking the first ten numbers twice and then the numbers from (0) to 99, generate the numbers from (0) to 9999. Note that since 0 is represented by an empty list, appending a string to it vectorises and therefore does nothing, resulting in a falsy value, which is excluded from the final join.

»§υ⊗⌕υθ

Look up the position of the input and output the string that represents double that value.

\$\endgroup\$

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.