New answers tagged code-golf
0
votes
Find the weight of five apples
Husk, 6 bytes
m≠/4Σ¹
Try it online!
The same logic as in other answers as well as my Pyth answer.
Commented:
...
0
votes
Find the weight of five apples
Pyth, 6 bytes
m-/sQ4
Try it online!
Takes a list of partial sums and outputs the list of the individual apples' weights.
The solution is pretty basic: sum up the ...
0
votes
Sum every second digit in a number
Red, 57 bytes
func[n][s: 0 foreach[_ e]form n[s: s - 48 + any[e#"0"]]s]
Try it online!
0
votes
Print the ASCII Code page
Zsh +coreutils, 25 bytes
jot -c 95 32 \~|rs -eg0 6
Try it online!
Beats the other Zsh solution by 1 byte!
1
vote
Sum every second digit in a number
bc, 40 bytes
for(i=length(a);i>=0;i-=2)b+=a/10^i%10;b
Try it online!
42b
Based on my similar bc solution, but iterating from ...
1
vote
Add two numbers
Qdeql, 37 bytes
Input in bytes rather than ASCII. Although Qdeql is limited to bytes, this would work with any (arbitrarily large) finite cell size.
...
0
votes
0
votes
Expand a C array
Uiua, 69 bytes
⊂⊃(□$"_ _[_];"|⍚$"_[_] = _;"¤⋅⊙⊓⇡∘)⊙⊙(⊸⧻⊜□≥@0.)°$"_ _[] = {_};"
Try it!
0
votes
Help Sam understand chess
Uiua, 45 bytes
⊡⊗:"QRBNK":⍜≡⊡⊸↥:+@.[↥,,⊃(/↥=0|/=|=2/×|=1/↥)]⌵-°⍉⇡8_8⟜¤
Try it!
...
1
vote
Sum every second digit in a number
Husk, 6 bytes
Σ→TC2d
Try it online!
A function that takes in a number and returns the sum of its even digits.
Commented:
...
1
vote
Sing Happy Birthday to your favourite programming language
Setanta, 68 bytes
le i idir(-2,2)scriobh("Happy Birthday",(i&"to You")|"Dear Setanta")
try-setanta.ie link
0
votes
0
votes
Divmod continuously until the remainder is 1 or 0, then get the remainder
Setanta, 43 bytes
gniomh f(a,b){ma b>1 b=f(a//b,a%b)toradh b}
try-setanta.ie link
Non-recursive solution, 51 bytes
...
0
votes
Summing the Digits of a Number
Setanta, 57 bytes
gniomh(n){s:=0le i idir(0,fad@n)s+=go_uimh(n[i])toradh s}
try-setanta.ie link
Version for native number type (up to \$2^{53}\$), 35 bytes
...
0
votes
Prime counting function
Ruby -rprime, 33 bytes
->n{Prime.take_while{_1<=n}.size}
Attempt This Online!
2
votes
0
votes
Prime counting function
Husk, 3 bytes
#ṗḣ
Try it online!
A very simple solution.
Commented:
...
1
vote
Tips for golfing in x86/x64 machine code
LOOP according to ECX
This is the famously slow instruction that decrements CX/ECX/RCX as a counter, then short jumps if not zero, all in 2 bytes! ...
0
votes
Filter out and add up
Swift 6, 68 bytes
let p={($0+"").split(separator:#/[^\d]/#).map{Int($0)!}.reduce(0,+)}
0
votes
Print the ASCII Code page
Ruby, 34 bytes
puts [*" "..?~].join.scan /.{,16}/
Attempt This Online!
0
votes
Print the ASCII Code page
Swift 6, 84 bytes
(2...7).map{n in(0...15).map{n*16+$0}}.map{print(String($0.map{.init(.init($0)!)}))}
0
votes
Is my triangle right?
Swift 6, 65 bytes
let r={{a in a[0]*a[0]+0+a[1]*a[1]==a[2]*a[2]}(($0+[]).sorted())}
r takes an ...
0
votes
Implement the hyperfactorial
Swift 6, 69 bytes
import Foundation
let h={(1...$0).map{pow(.init($0),$0)}.reduce(1,*)}
1
vote
Is my triangle right?
Husk, 7 bytes
Ẋo=+m□O
Try it online!
Takes a list of side lengths and outputs a 1-element list: [1] if true, ...
0
votes
0
votes
4
votes
1
vote
Tips for golfing in x86/x64 machine code
Use 32-bit x86 instead of 64-bit x86-64, if you can
This is a bit silly, but many code golf challenges only require 32-bit inputs, and 32-bit programs execute just fine on x86-64 processors (almost ...
0
votes
Divmod continuously until the remainder is 1 or 0, then get the remainder
Ruby, 25 bytes
F=->a,b{b>1?F[a/b,a%b]:b}
Attempt This Online!
0
votes
Parse Minecraft's VarInt
x86, 24 23 22 bytes
Couldn't figure out any tricks to get rid of shift, so it's a straight-forward do-while loop, checking the byte's sign bit. Input in esi, output ...
1
vote
Draw a big slash X
K (ngn/k), 19 18 bytes
-1 byte from @att
"X\\/"@2/~|:\=1+2*
Try it online!
Thanks to @tubular.
1+2* multiply the (...
2
votes
Minimum excluded number
Husk, 6 bytes
▼-⁰ŀ21
Try it online!
This is my first solution in Husk. I have picked up an easy challenge for starters.
Commented:
...
1
vote
1
vote
Sums of X*Y chunks of the nonnegative integers
Python, 131 bytes
import numpy
lambda a,b,c,n:numpy.arange(b*c*n).reshape((-1,b,c)).transpose((0,2,1)).reshape((-1,a,b)).sum(axis=(1,2)).flatten()[n]
TIO. Unlike ...
0
votes
Filter out and add up
JavaScript (Node.js), 34 bytes
s=>eval(s.replace(/\D|$/g,' +')+0)
Try it online!
Like ETHproductions's answer but extra push is possible
Not work as intended ...
1
vote
Sums of X*Y chunks of the nonnegative integers
Clojure, 87 bytes
#(for[P[partition]p(P %2(P(quot %3 %1)(P %1(range))))r(apply map concat p)](apply + r))
TIO. Yay, Clojure is quite good with partitioning and &...
1
vote
Filter out and add up
Python 3.12, 53 bytes
import re;lambda s:sum(map(int,re.findall(r'\d+',s)))
Apologies if anything is incorrect with my submission, I've been lurking for a while ...
0
votes
Bloons TD 6 Ordered Upgrade Paths
Vyxal M, 143 bitsv2, 17.875 bytes
502fʀΠvṖÞfUµ₍G≬GḟN;Ṙ
Try it Online!
Bitstring:
...
1
vote
Sing Happy Birthday to your favourite programming language
Chicken, 20550 bytes
Program here
The language I chose is ridiculously unsuitable for code golfing, you will know why when you see the program.
However, there is an interesting feature in this ...
0
votes
Shortest Error Message
Vyxal (online), 4 bytes
1ƛød
Try it Online!
Outputs ⟨ .
Vyxal's online mode has a weird quirk where, if an error occurs inside ...
0
votes
Visually Explain the Pythagorean Theorem
Vyxal, 17 bytes
∆/J:\#*ε‛+=Y4IYf§
Try it Online!
I thought I'd answer this with modern Vyxal.
...
0
votes
Generate this number table
Clojure, 49 bytes
#(for[R[(range 1(inc %))]r R](for[c R](max r c)))
TIO. IMO the only interesting trick is to re-use the range value ...
0
votes
Calculate sum of self-exponentation
Clojure, 34 bytes
#(apply +(for[i %](Math/pow i i)))
The input must be a list.
0
votes
0
votes
0
votes
2
votes
How much code would a codegolf golf if a codegolf could golf code?
Uiua, 52 50 bytes
$"How much _ would a __ _ if a _ could _ _?"⊃∩.⊃⊂:
Try it: Uiua pad
Explanation: Each _ in the <...
0
votes
Recreate the ASCII-table as an ASCII-table
Uiua, 95 92 bytes
J←$"_ | _"
≡&p⊂/J↯3"Dec Chr "⊂@-/≡J≡≡(⊂⬚@ ↙5°⋕)+32°△↙2⟜↯3_32_5↻1⬚@ (⊂"DEL"⊂"Space")+@!⇡¤94
Try it: ...
1
vote
Sing Happy Birthday to your favourite programming language
///, 51 bytes
/@/Happy Birthday //#/to You
/
@#@#@Dear Slashes
@#
Try it online!
I'm back! Slashes is a great language for these very repetitive tasks.
0
votes
Top 50 recent answers are included
Related Tags
code-golf × 12069string × 2126
math × 1645
number × 1226
ascii-art × 1166
sequence × 871
array × 780
decision-problem × 746
kolmogorov-complexity × 643
arithmetic × 508
number-theory × 436
integer × 414
geometry × 350
grid × 347
random × 327
tips × 326
combinatorics × 312
primes × 300
matrix × 283
date × 273
graphical-output × 271
parsing × 247
game × 237
binary × 223
restricted-source × 204