Posts by celtschk
Python 3, 142 bytes def f(s): m=1;r="" for c in s.upper(): if'@'<c<'['or'-'==c:r+=c*m;m=0 if c in":; ":r+=c*(c!=' ');m=1 if c in".?!":r+=' ';m=1 return r Try it online!
Bash, 20 18 bytes Saved two bytes by removing the quotes; the code works well without them. echo Hello, `cat`! Try it online!
Given two strings, I define their product as follows: If any of the two strings is empty, the product is the empty string. If the second string consists of a single character, the result ...
Python 3, 76 71 bytes Saved 5 bytes thanks to user def f(p,n):l=[n*p//q for(p,q)in p if n%q<1];return f(p,l[0])if l else n Try it online! This code assumes that the fractions are given a...
C (gcc), 32 bytes f(a,b){return a>0?f(b,a-b)+1:0;} Try it online!
The task is to generate the text of Lewis Caroll's Jabberwocky. The text, quoted from Wikipedia, is as follows (I've replaced a non-ASCII character with ASCII, otherwise it's direct copy&paste...
Run-length encoding is a simple compression technique which compresses sequences of repeating identical bytes. The encoding rules for this task are as follows: Any sequence of $n$ identical bytes...
What about the following rule: You get points, but your score is not your own points, but the points of the best player below you, or 0 if you are the worst player. Obviously if you are the worst ...
Some additional cosiderations on using the sandbox: First, a migration mechanism might encourage good behaviour, for example by migration being only possible if the post has a minimal score, and/o...
Python 3, 48 bytes def f(n,d):return f"[{(50*n+d//2)//d*'|':-<50}]" Try it online!
Contestants should read from standard input and write to standard output. This is because almost all programming languages, including most esoteric programming languages, allow reading from standar...
C (gcc), 37 bytes This takes an integer as input. It uses the fact that the last digit is odd if and only if the number is odd, and that integer division by 10 removes the last digit. Testing if ...
Bash, 205 bytes for s in C{C{CC,D,M},DC,MC} {CM,DC,D}{D,M} I{C,D,I{II,V,X},L,M,VI,X{C,I,L,V,X}} L{C,D,L,M,XC,XL} MMMM V{C,D,IV,IX,L,M,V,X} X{C{C,D,L,M,X},D,LX,M,X{C,L,XX}} do echo $1|grep -q $s...
C (gcc), 64 62 bytes Saved 2 bytes thanks to m90 in the comments. i;f(char*s){i=atoi(s)>>4;return'A'+(i&8?i&4?i&2?i%2+3:2:1:0);} Try it online!
Use pointer arithmetic and \0 with string literals In C, strings are just zero-terminated character arrays, which means you can play tricks with pointer arithmetic. For example, the line char*s=a...
Rule 110 is a Turing complete cellular automaton. It is defined as follows: Take as initial value a sequence of symbols that's infinite to both sides, which consists only of two different symbols ...
C (gcc), 774 bytes #define S strcat(o, #define I(s)if(n)S s);else{S" ");return;} #define B);break;case n,N,s;char*i,o[999],*d[]={"zero ","one ","two ","three ","four ","five ","six ","seven "...
Python 3, 69 bytes i="))43,]1-::[i,43,73(%ci(tnirp;%c%s%c=i";print(i%(37,34,i[::-1],34)) Try it online!
Replace n+1 with -~n and n-1 with ~-n For integers, n+1 and ~-n have the same value, as have n-1 and -~n. While the expressions themselves have the same lengths, the replacements often allow to s...
Replace if/else expressions by array subscripts Consider the statement a=b if x<y else c You can get rid of the expensive keywords by using the implicit conversion of boolean values to inte...
Python 3, 99 bytes r=[*range(26)] a="ABCDEFGHIJKLMNOPQRSTUVWXYZ" for i in r+r[-2::-1]:print(" "*(26-i)+a[:i]+a[i::-1]) Try it online!
C (gcc), 46 bytes f(int*a){return *a<0|a[1]<0||*a==a[1]&f(a+1);} This takes an array that is terminated by a negative number (which is not part of the list content, just like the te...
Replace print loops by list unpacking If you want to print elements of a list one per line, the straightforward way to do it is a loop: for i in l:print(i) But this can be shortened using list...
Python 3, 78 bytes def t(n,*l):d="0+-"[n%3];return d*(not l or n*n) if n*n<2 else t((n+1)//3,1)+d Try it online!
If you have a loop or if, you can save two or more characters (depending on the current indentation level and the number of statements in the body) by putting it right after the colon: For example...
- ← Previous
- 1
- 2
- 3
- 4
- Next →
