| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | Vyxal | 240928T044450Z | emanresu |
| 021 | Retina 0.8.2 | 240927T143144Z | Unrelate |
| 068 | Python 3.12 | 240917T195931Z | Malo |
| 060 | R | 240917T062009Z | Dominic |
| 097 | TIBASIC | 240916T225815Z | absolute |
| 084 | R | 240916T123246Z | pajonk |
| 050 | JavaScript Node.js | 240916T115349Z | Fhuvi |
| 059 | Python | 240916T010600Z | STerliak |
| 012 | 05AB1E | 240916T085006Z | Kevin Cr |
| 088 | Python 3.12 | 240915T010906Z | David Ch |
| 064 | Haskell | 240914T191515Z | Unrelate |
| 093 | Haskell | 240914T143804Z | DPD- |
| 010 | Jelly | 240914T142247Z | Unrelate |
| 085 | Haskell | 240914T131444Z | DPD- |
| 005 | Charcoal | 240914T055735Z | Neil |
| 030 | Retina 0.8.2 | 240914T055131Z | Neil |
| nan | Vyxal | 240914T043133Z | lyxal |
Vyxal, 10 bytes
₌⌊LḞṘE₀*øḋ
Try it Online! Uses ideas from Fhuvi's JavaScript answer.
⌊ # Parse the result as a float
Ḟ # And stringify it
₌ L # to len(input) decimal places
Ṙ # Reverse the result
E # And cast it to float again
₀* # Multiply by 10
øḋ # And stringify as a decimal.
Retina 0.8.2, 22 21 bytes
$
.
D`\.
^
0
O$^`.\.?
-1 and 0.8.2 compatibility thanks to Neil!
Port of my Jelly solution. Also works in Retina 1 and ties Neil's Retina 1 solution.
$
.
D`\.
Append a ., then remove it if there already is one.
^
0
Prepend a 0.
O$^`.\.?
Group non-overlapping matches of .\.? (any one character, possibly followed by .), and concatenate them reversed.
Neil's trick to save a byte here is fiendish: using $ with an O (sort) stage makes it take a second source line as a sort key, and omitting that line defaults to an empty string, which always compares equal to itself and results in no sorting. The O$ effectively acts as Retina 1's L|"", and the ^ then reverses the non-sort.
Python 3.12, 75 68 bytes
lambda n:(r:=['']+n[::-1].split('.'))[-2]+r[-1][0]+'.'+r[-1][1:]+'0'
(old version 75 Bytes Try it online! )
Creates a list with 2 or 3 elements. First item is empty string. Reverse and split the given number. So second item is the reversed digits before the decimal point string, and the 3rd item is the reversed digit after the decimal point string if any.
Creates and returns a string by adding the penultimate item, add the first digit of the last element, add a decimal point, add the last element except 1st digit, add '0' so your number never ends ends by '.'
-7 bytes: lambda and walrus operator used, thanks to emanresu
R, 67 66 60 bytes
Edit: -5 bytes thanks to pajonk
\(x)el(intToUtf8(rev(c(y<-utf8ToInt(x),46[!46%in%y]))):1)*10
Reverse the digits (including the decimal point) and multiply by 10.
If there isn't a decimal point, add one at the end first.
R, 84 bytes
\(x){w=match(46,d<-c(b<-c(48,utf8ToInt(x)),46));b[w-1:0]=d[w-0:1];intToUtf8(rev(b))}
Alternative 'tinkering with the decimal point' approach, in response to pajonk's answer...
Prepend a zero, then find the dot and swap it with the previous character, and finally output the reversed string.
If there isn't a dot, add one at the end of the string before the swap.
TI-BASIC, 97 bytes
Prompt N
{0,1→L₁
NAns→L₂
LinReg(ax+b) Y₁
Equ►String(Y₁,Str1
sub(Str1,1,length(Str1)-3
If not(inString(Ans,".
Ans+".
For(I,1,length(Ans)-1
sub(Ans,2I,1)+Ans
End
sub(Ans,1,I
10expr(Ans
TI-BASIC does not have built-in methods for converting numbers to strings nor reversing strings, so that has to be done manually.
Explanation:
Prompt N ; Request an integer and store it in N
{0,1→L₁ ; [1] Convert N to a string
NAns→L₂ ;
LinReg(ax+b) Y₁ ;
Equ►String(Y₁,Str1 ;
sub(Str1,1,length(Str1)-3 ; [/1]
If not(inString(Ans,". ; If the number does not have a decimal, append one
Ans+". ;
For(I,1,length(Ans)-1 ; [2] Prepend reversed digits
sub(Ans,2I,1 ; E.g. "123.45" ⇒ "54.32123.45"
End ; [/2]
sub(Ans,1,I ; Get the reversed digits
10expr(Ans ; Convert the string to a number, then multiply it
; by 10
Examples:
prgmCDGF2B
N=?12.34
432.1
prgmCDGF2B
N=?3.0
3
prgmCDGF2B
N=?012
2.1
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
Program size is equal to \$MEM\: byte\: count - program\: name\: length - 9\: bytes\$.
[1] http://tibasicdev.wikidot.com/number-to-string
[2] http://tibasicdev.wikidot.com/reverse-a-string
R, 89 84 bytes
\(x,z=na.omit(strtoi(y<-el(strsplit(x,"")))))z%*%.1^(match(".",y,l<-sum(1|z)+1)-2:l)
Seems that base conversion is shorter than reversing and tinkering with the decimal point.
JavaScript (Node.js), 50 51 bytes
-1 byte thanks to l4m2's tip
String and number manipulation instead of base conversion.
n=>[...(+n).toFixed(n.length)].reverse().join``*10
This challenge has been surprisingly harder than initially thought, because :
012can be interpreted asdecimal 10by some languages in some circumstances. This has been handled by passing a string as parameter and converting it to a number with+- The absence of
.in some cases, whether directly in the input or after some calculation, doesn't allow to simply reverse the string. This is fixed with thetoFixed - if we choose to
divide by 10 before, this would give some pretty bad floating point errors, especially for the test case2.1. So it's simpler tomultiply by 10 afterwards
Python, 61 59 bytes
lambda x:re.sub('(.)(\.|$)',r'.\1',x,1)[::-1]+'0'
import re
-2 thanks to emanresuA
Original version: Attempt This Online!
Alternative regex-based approach: replace a first occurrence of a digit followed by decimal point or line end with a dot followed by that digit, then reverse and pad with zero.
Usually JS is better at regex codegolfs (at least since you don't have to import anything), but it lacks advanced slicing, so I can't make it shorter than 60 59 bytes (ATO):
s=>[...s.replace(/(.)(\.|$)/,'.$1')].reduce((p,q)=>q+p,'0')
05AB1E, 12 bytes
þRT*I'.«¤k°/
Try it online or verify all test cases.
Explanation:
þ # Remove a potential "." from the (implicit) input by only keeping digits
R # Reverse it
T* # Multiply by 10 to add a trailing 0
I # Push the input again
'.« '# Append a trailing "."
¤ # Push its tail (without popping): the "."
k # Pop both, and push the first (0-based) index of "."
° # Take 10 to the power this index
/ # Divide the earlier number by this
# (after which the result is output implicitly)
Python 3.12, 100 92 88 bytes
def f(x):*x,=*x,*{d:='.'}-{*x};t=x.index(d)-1;x[t:t+2]=d,x[t];return''.join(x)[::-1]+'0'
(Old version: Try it online!)
-8 bytes thanks to UnrelatedString: Try it online!
-4 bytes thanks to STerliakov: Try it online!
Inputs and outputs valid strings. First, checks if "." is in the string, if not it is added to the end First, converts the string to a list, adding "." to the end if it isn't already present. Then, switches the "." with whatever character is before it. Then returns the string reversed, adding a "0" to the end for cases like "2.1".
Haskell, 68 67 64 bytes
g.reverse.('0':)
g s|(a,d:h:b)<-span(>'.')s=a++h:d:b|0<1=g$'.':s
Just prepends a leading 0, reverses the whole thing, then surgically inverts the decimal point back ahead of the ones place with a fallible pattern guard--trying again with one prepended if needed. This is done after reversing so that the pattern match can extract the ones place as the second element of the suffix containing the decimal point instead of having to use functions to extract it as the last element of the prefix before it.
Haskell, 93 bytes
f=show.fst.foldl(#)(0,0)
x#'.'=x$>1
(x,0)#y=(x/10+g y,0)
(x,n)#y=(x+g y*10^n,n+1)
g=read.pure
greads a character into a number- the aggregating function
(#)is called for each character of the string; It first argument is the accumulator, the second the current character. The accumulator is itself a tuple: the first element is the current result and the second starts at zero(x,0)#y=(x/10+g y,0)when we encounter a new digit we multiply by 0.1 the current result and add to it the new digitx#'.'=x$>1when we encounter the decimal point we retain the current result, but we set the second argument to 1, switching to decimal part mode- in decimal part mode (
(x,n)#y=(x+g y*10^n,n+1)) when we encounter a new digit we have to multiply it by (0.1)^-n (i.e. 10^n) where n is the number of digits after the decimal mark
Jelly, 10 bytes
nkŻœ|¥ɗ”.Ṛ
This feels like it should be so much simpler than it is!
ɗ”. With the literal character '.' on the right:
Ż Prepend 0 to the input
œ| and append '.' if it doesn't contain a '.'.
k ¥ Split that after indices where
n the original input is not equal to '.'.
Ṛ Reverse the partitions (which are implicitly smash-printed).
Jelly, 11 bytes
pḊi¥¦Ṛḟɗ”.0
Full program only (may need quotes around input; test footer replicates the needed full program behavior).
ɗ”. With the literal character '.' on the right:
p Pair element(s) of the input with '.'
i ¦ at the 1-index of '.' (or 0 if not found)
Ḋ ¥ in the input without its first element.
Ṛ Reverse.
ḟ Filter out any non-paired '.'.
0 Print all that and return 0.
Haskell, 85 bytes
f x|elem '.'x=g x"0"|True=g(x++".")"0"
g(x:'.':y)z=g y$x:'.':z
g(x:y)z=g y$x:z
g[]z=z
since 0.1^n = 10^-n we simply reverse the string, taking care of some special cases
g(x:y)z=g y$x:zandg[]z=zisreverse(wherezis the accumulator)g(x:'.':y)z=g y$x:'.':zswaps the position of the decimal point while reversing (in facts the unit remains the unit since 10^0 = 0.1^0 = 1)f x=g x"0"calls our special reverse using an initial accumulator of['0']. This initial accumulator adds a trailing zero to the result, which ensures there is at least one digit after the decimal point- with the guard
elem '.'xwe can add the decimal point to the input if it is missing
Charcoal, 5 bytes
I⍘S·¹
Try it online! Link is to verbose version of code. Explanation:
S Input string
⍘ Interpret as base
·¹ Literal number `0.1`
I Cast to string
Implicitly print
When converting a string "from" a numeric base, Charcoal interprets the characters 0-9a-zA-Z as integers from 0 to 61 and then computes the polynomial with the given input base.
Retina 0.8.2, 30 bytes
$
.
D`\.
O$^`.
\.(.)
$1.
\.$
Try it online! Link includes test cases. Explanation:
$
.
Append a ..
D`\.
Remove it again if there was already a . in the input.
O$^`.
Reverse the input.
\.(.)
$1.
Move the . forward one digit.
\.$
Delete the . if it's now at the end of the string.
8 bytes can be saved by switching to Retina 1 since the reversal and . movement can be written using V stages:
$
.
D`\.
V`
V`\..
\.$
Try it online! Link is to test suite that converts any match of /\d+(\.\d+)?/ in the input.
Vyxal, (04.41) 144 bitsv2, (08.1) 18 bytes
\./0J2Ẏ÷$ṫ$\0∴WRIṠ\.j
Bitstring:
011010001110010011101111011111010001110011101100100100011100000001110010111000000000001110000100010101011110010010000111100001010101101101010100
A silly little answer