Skip to main content
added 213 characters in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 2, 57 bytes

f=lambda s,p="":s and f(s[:-3],p[1:]+"kMGT")+s[-3:]+p[:1]

Try it online!

Takes input as a string. If the output may have a trailing space, we can avoid some workarounds and save 3 bytes.

54 bytes

f=lambda s,p=" kMGT":s and f(s[:-3],p[1:])+s[-3:]+p[0]

Try it online!

One potentially-useful observation is that the string format '{:,}'.format puts commas in the places we want to insert letters:

'{:,}'.format(1234567) == "1,234,567"

However, I haven't found a short enough way to use this.

Python 2, 57 bytes

f=lambda s,p="":s and f(s[:-3],p[1:]+"kMGT")+s[-3:]+p[:1]

Try it online!

Takes input as a string. If the output may have a trailing space, we can avoid some workarounds and save 3 bytes.

54 bytes

f=lambda s,p=" kMGT":s and f(s[:-3],p[1:])+s[-3:]+p[0]

Try it online!

Python 2, 57 bytes

f=lambda s,p="":s and f(s[:-3],p[1:]+"kMGT")+s[-3:]+p[:1]

Try it online!

Takes input as a string. If the output may have a trailing space, we can avoid some workarounds and save 3 bytes.

54 bytes

f=lambda s,p=" kMGT":s and f(s[:-3],p[1:])+s[-3:]+p[0]

Try it online!

One potentially-useful observation is that the string format '{:,}'.format puts commas in the places we want to insert letters:

'{:,}'.format(1234567) == "1,234,567"

However, I haven't found a short enough way to use this.

Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 2, 57 bytes

f=lambda s,p="":s and f(s[:-3],p[1:]+"kMGT")+s[-3:]+p[:1]

Try it online!

Takes input as a string. If the output may have a trailing space, we can avoid some workarounds and save 3 bytes.

54 bytes

f=lambda s,p=" kMGT":s and f(s[:-3],p[1:])+s[-3:]+p[0]

Try it online!