The Stack Overflow podcast is back! Listen to an interview with our new CEO.

New answers tagged

2

Poetic, 485 bytes honestly i lived my life i faced a boatload of brutal challenges i desire a day of bliss-i want badly to forget a day where things backfire i desire a day-i covet a week-and of course i want solitude around everyone i desire a day-i crave a year-finding no faults i seem to create everyday i desire a day-i plead,i pray-for an answer i need,...


0

Forth (gforth), 84 bytes : f s" kMGT" bounds <# do 1000 /mod tuck if 0 # # # d. i c@ hold then loop 0 #s #> ; Try it online! A function that takes a number from the stack, and returns a string as (address, length) pair, which is the standard representation for a string in Forth. Produces lots of garbage under the returned string, and a few zeros on ...


0

Haskell, 99 bytes f n=concatMap(\(a,b)->a++b)$reverse$zip(reverse<$>chunksOf 3(reverse$show n))(["","k","M","G","T"]) Try it online! The function f takes a number, converts it into a string and splits it backwards into chunks of 3 characters, e.g. reverse<$>chunksOf 3(reverse$show 1234) == ["234","1"] it then zips the list of chunks with ...


1

Forth (gforth), 99 bytes : f >r 0 begin 1+ dup begin dup i < while dup 20 for 10 /mod >r + r> next + repeat i = until r> . ; Try it online! Largely similar to reffu's submission (106 bytes). The golfed parts are: Digit sum calculation (-6) Final cleanup (-1) by printing some garbage to stdout. (No problem because the result is returned on ...


1

Perl 6, 39 bytes {flip [~] <<''k M G T>>Z~.flip.comb(3)} Try it online!


2

JavaScript (Node.js), 55 bytes f=(n,i=0)=>n.replace(/.+(?=...)/,d=>f(d,i+1)+'kMGT'[i]) Try it online!


1

Bash, 83 bytes a=('' k M G T) s= (($1>99))&&s=${1: -3} echo `$0 ${1:0:-3} $[$2+1]`${s:-$1}${a[$2]} Try it online! (link shows 82 bytes, but uses function recursion instead of $0-recursion) Nice little recursive solution. Bash doesn't like invalid string indexing, which both helps (gives us our exit condition for free) and hurts (we have to ...


0

Zsh, 53 bytes a=kMGT for z y x (${(Oas::)1})s=$x$y$z$a[i++]$s <<<$s Try it online! (s::) splits the stirng, Oa reverses the characters. Zsh arrays and strings are 1-indexed, so $a[0] on the first iteration substitutes nothing.


1

C (clang), 65 62 bytes f(char*n,z){for(;z--;z%3||printf(L" kMGT"+z/3))putchar(*n++);} Try it online! Taking a char array as input. Saved 3 thanks to @ceilingcat C (clang), 68 bytes f(n,s){!s|n&&f(n/10,s+1)+printf(s%3?"%d":"%d%c",n%10," kMGT"[s/3]);} Try it online! Taking an int as input Recursive function which first call itself and ...


4

Brachylog, 17 bytes ↔ġ₃;" kMGT"↔z₀cc↔ Input as a string, returns through output the result, with a trailing newline :) Explanation: More or less a translation of Jelly answer: ↔ Reverse input string ġ₃ Group into triples, excluding last group with length in range (0,3] ; Pair those groups with: " kMGT" A ...


4

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 '{:,}'....


0

Python 2, 72 bytes f=lambda n,p=' kMGT':(n>M and f(n/M,p[1:])or'')+`n%M+M`[-3:]+p[0] M=1000 Try it online!


0

Retina 0.8.2, 31 bytes r`\B... ,$.'$& T`9630,`TGMk_`,. Try it online! Explanation: r`\B... Match groups of three digits starting from the end but ensure that there is at least one more digit before the match. ,$.'$& Prefix each match with a marker comma and the number of digits after the match (0 for the last match, then 3, 6 and 9 respectively)...


3

Jelly, 14 bytes 13 if input formatted as a string is acceptable for languages which can handle large integers (remove Ṿ and quote the argument) ṾṚs3żFtṚʋ“kMGT A full program accepting the non-negative number as a command line argument which prints the result Try it online! How? ṾṚs3żFtṚʋ“kMGT - Main Link: integer e.g. 12345 Ṿ - un-...


1

Gema, 88 characters \A=@subst{?=\@push\{p\;?\};TGMk } *=@reverse{@subst{<d3>=\$p\$1\@pop\{p\};@reverse{$0}}} Boring double reverse approach, but looping in Gema would be painfully long. Sample run: bash-5.0$ echo -n '1234567890' | gema '\A=@subst{?=\@push\{p\;?\};TGMk };*=@reverse{@subst{<d3>=\$p\$1\@pop\{p\};@reverse{$0}}}' 1G234M567k890 ...


0

JavaScript (V8), 71 bytes Takes input as number b=(a,l=10e11,x=0,c=a/l|0)=>(l-1)?(c?c+'TGMk'[x]:'')+b(a%l,l/1000,x+1):c Try it online!


1

PowerShell, 81 73 71 69 64 bytes ($a=$args)|%{$r=$a[--$i]+' kMGT'[$j+5*!$k]+$r;$j+=$k=!($i%3)} $r Try it online! Takes argument as a splatted string.


0

Python 2, 84 74 bytes i=input();o="" for x in"kMGT ":o=(i,x+i[-3:])[len(i)>3]+o;i=i[:-3] print o Try it online! Previous version for 84 bytes i=input()[::-1];o="" for x in"kMGT ":o+=(i,i[0:3]+x)[len(i)>3];i=i[3:] print o[::-1] Try it online! Reverse the input Step through backwards in threes appending prepending the next letter Reverse and print ...


1

APL+WIN, 46 bytes Prompts for integer as a string ∊(n/m),¨(n←3>+/¨' '=¨m←((3/⍳5)⊂¯15↑⎕))/'TGMk ' Try it online! Courtesy of Dyalog Classic


2

Icon, 76 71 bytes procedure f(s) k:=0 s[i:=*s-2to 2by-3:i]:="kMGT"[k+:=1]&\z return s end Try it online! Takes input as a string.


3

Perl 5 (-p), 40 bytes y/ kMG/kMGT/while s/\d\K((\d{3})+$)/ $1/ Try it online!


2

PHP, 67 65 bytes for(;''<$d=$argn[-++$i];)$r=$d.' kMGT'[$i%3==1?$i/3:5].$r;echo$r; Try it online! Loops on input digits from right to left and concatenates each digit to $r variable in reversed order. When we are on 4th, 7th, etc... digit, the respective prefix is concatenated to $r as well. Output has a trailing space. for(;''<$d=$argn[-++$i];) // ...


9

JavaScript (ES6), 64 bytes Takes input as a string. f=(n,i=0,[d,t]=n.split(/(...)$/))=>t?f(d,i+1)+['kMGT'[d&&i]]+t:d Try it online! How? The regular expression /(...)/$ matches the last three digits of the integer, or nothing at all if there are less than 3 digits remaining. Because there's a capturing group, these digits -- if present -- ...


1

Red, 85 bytes func[n][p: copy"kMGT"parse reverse n[any[3 skip ahead skip insert(take p)]]reverse n] Try it online! Takes the input as a string.


4

05AB1E, 15 14 bytes R3ô"kMGTP"øSR¦ Try it online! Thanks to Kevin Cruijssen for -1 byte R reverse input 3ô split it into groups of 3 digits "kMGTP" push the prefixes ø zip the groups of numbers with them S split to a list of characters R reverse again ¦ drop the first element implicitly print


0

Python3, 89 83 bytes 65 chars (or 65 bytes, if you use just ASCII, but I fancy the Unicode superscript small letters) f=lambda n,p='ᵏᴹᴳᵀ':len(n)<4 and n or f(n[:-3],p[1:])+p[0]+n[-3:] Try it online! Python3, 89 bytes: import re;print(''.join(sum(zip(re.findall('..?.?',str(x)[::-1]),'kMGT.'),()))[::-1][1:]) Try it online! I didn't try to shorten ...


1

Whitespace, 101 bytes (Lots of whitespace) Try it online! readable version: lstl call readint sssttsl push 6 tssl mult lstl call readint tsss add lstl call readint sssttsl push 6 tssl mult lstl call readint tsss add tsst sub sssl push 0 slt swap lttsl jn otherteam ...


1

MathGolf, 7 6 bytes I am quite impressed that MathGolf has 8 block-starting builtins! 2É6*+> TIO Explanation 2 Constant 2 É Start code block of length 3 6* Take one input * 6 (with implicit input) + Take another input and add them > Take Greater than of the two constants in the stack There is an implicit output.


1

Add++, 58 bytes D,g,@,FL1+ D,k,@~,J L,R€gd"#"€*$dbM€_32C€*z£+bUBcB]bR€kbUn Try it online! How it works Our program consists of two helpers functions, g and k, and the main lambda function. g iterates over each integer, \$x\$, between \$1\$ and \$n\$ and returns \$d(x)\$ as defined in the challenge body, while k takes a list of characters and concatenates ...


1

Python 3, 44 bytes i=0;print(i) while 1:i+=1;print(i);print(-i) Try it online!


1

Cascade, 13 bytes (chunk size 13) ] &/2 # * 2& Try it online! ] &/2 # * 2&] &/2 # * 2& Try it doubled! This was quite difficult. The basic gist of this is to print the input multiplied by 2 for the first program, and replace the 2 by a copy of the input for the second. Explanation: The executed part of the first program ...


0

Zsh, 30 bytes (chunk size 10) m=$m[1]*2 x=$[$1$m] return $x Try it online! Try it doubled! Abuses the fact that $var in $[$var] gets expanded first, then evaluated in the arithmetic context. # each line is 9 characters long. when doubled, each is run twice. m=$m[1]*2 # first time: m='*2' second time: $m[1] is '*', so m='**2' x=$[$1$m] ...


1

Python 3, 60 bytes Chunk size 6. Not a great solution, but it works. This is such a unique challenge, it really makes you think from a different perspective. 0; i=input;0; p=print;n=i()# x=int(n)## z=2*x#z=x*x p(z)## Try it online! Doubled: 0; i=0; i=input;input;0; p=0; p=print;print;n=i()#n=i()# x=int x=int(n)## (n)## z=2*x#z=2*x#z=x*x z=x*x p(z)#...


1

Go, 44 bytes func f(a,b,x,y int)bool{return a*6+b>=x*6+y} Try it online!


2

PowerShell, 35 bytes param($g,$b,$h,$c)$g*6+$b-gt$h*6+$c Try it online!


1

SimpleTemplate, 84 bytes Just the simple "multiply by 6, sum and compare" aproach, except the math support is extremelly lacking. {@set*A argv.0,6}{@incbyargv.1 A}{@set*B argv.2,6}{@incbyargv.3 B}0{@ifB is lowerA}1 Outputs 0 for false and 01 for true. Ungolfed: {@// multiply the first argument by 6} {@set* teamA argv.0, 6} {@// add the 2nd argument to ...


2

Brain-Flak, 62 bytes ([((({})({}){}){}{}[(({})({}){}){}{}]<(())>)(<>)]){({}())<>}{} Outputs 1 if the first team lost, and 0 if they won (or tied). Try it online! # Input: G B g b ( <(())>) # Push 1 under... ({})({}){}){}{} # 6G + ...


3

brainfuck, 45 38 36 32 29 28 bytes ,[>,[<++++++>-],]-[[-<]>>]>. Try it online! Thanks to @Jo King for -8 bytes Input is b1, g1, b2, g2 (goals and behinds are exchanged) Prints þ, if team 1 won. Prints null, if team 2 won. code: [tape: p1, p2, print marker] [Get input and calculate scores] , input behinds of team 1 [ ...


1

Ruby, 21 bytes ->a,b,x,y{b-y>6*x-=a} Try it online! Worked around the boring solution, saved a byte and called it a day.


1

Japt, 6 bytes Input as a 2D-array. Outputs 1 for team 1, 0 for a draw or -1 for team 2. mì6 rg Try it mì6 rg :Implicit input of array m :Map ì6 : Convert from base-6 digit array r :Reduce by g : Sign of difference


3

Scala, 11 bytes _*6+_>_*6+_ Try it online! Takes 4 Integers in the order of g1 b1 g2 b2.


1

Whitespace, 115 bytes [S S S N _Push_0][S N S _Duplicate_0][T N T T _STDIN_as_integer][T T T _Retrieve_integer][S S S T T S N _Push_6][T S S N _Multiply][S N S _Duplicate][S N S _Duplicate][T N T T _STDIN_as_integer][T T T _Retrieve_integer][T S S S _Add][S N S _Duplicate][S N S _Duplicate][T N T T _STDIN_as_integer][T ...


3

33, 22 bytes 6OxcOasz6OxcOaclmzh1co Try it online! Takes the input as 4 delimited integers, and returns 0 for the first team winning, 1 for the second. Explanation: 6Oxc | Multiplies the first number by 6 Oa | Adds the second number 6Oxc | Multiplies the third number by 6 Oa | ...


1

Wolfram Language (Mathematica), 13 bytes 6#+#2>6#3+#4& Try it online! straightforward and boring


5

CP-1610 assembly (Intellivision), 9 DECLEs1 ≈ 12 bytes A routine taking input in R0 (\$g_1\$), R1 (\$b_1\$), R2 (\$g_2\$) and R3 (\$b_2\$) and setting the sign flag if the 2nd team wins, or clearing it otherwise. 275 PSHR R5 ; push return address 110 SUBR R2, R0 ; R0 -= R2 082 MOVR R0, R2 ; R2 = R0 04C SLL R0, 2 ; R0 <<= ...


2

05AB1E, 6 5 bytes 6δβZk Input as a nested list [[g1,b1],[g2,b2]]. Output 0 if team 1 wins and 1 if team 2 wins. -1 byte thanks to @Grimy for reminding me about δ. Try it online or verify all test cases. Explanation: Apparently arbitrary base conversion on nested lists doesn't work without an explicit map outer product. δβ # Apply arbitrary base-...


3

Scratch 3.0 17 16 blocks, 160 143 bytes Score comes from proposed scoring method here 1 block/17 bytes saved thanks to @A (or Uzer_A on scratch)_ Try it on Scratch As Scratchblocks: when gf clicked repeat(2 ask[]and wait set[m v]to((answer)*(6) ask[]and wait change[m v]by(answer add(m)to[x v end say<(item(1) of [x v]) > (m) Answer History Pretty ...


3

J, 12 bytes >&(1#.6 1*]) Try it online!


1

Perl 5, 18 bytes say<>*6+<>><>*6+<> Try it online! Input is line separated: g1 b1 g2 b2


2

Zsh, 19 bytes try it online!! ((6*$1+$2>6*$3+$4)) Input order is g1 b1 g2 b2. Exit codes 0==true and 1==false


Top 50 recent answers are included