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

Hot answers tagged

23

C++ (2420,2243,2353,1860,1822*.9=1639.8) Ok, so this is probably the worst ever code golf submission, but it's my first and I had fun. I think it even works. :) #include <iostream> #include <list> #include <string> #include <time.h> #define D r=rand();d=((double)r/RAND_MAX) using namespace std;class P{int n[25];public:int S;P(int N){...


17

Charcoal, 18 16 13 bytes -3 bytes thanks to @Neil! F⊕N«←OM⊕ι↓»‖C Explanation F⊕N« » For ι (implicitly from 0) to (1 + input as number) ←O Print O, with print direction rotated 180 degrees M⊕ι↓ Move 1+ ι units down ‖C Reflect (in the default direction, right), leaving original intact Try it online! ...


16

Javascript ES6, 62 Bytes s=>Math.sign([...s].reduce((p,c,i)=>p+~~c*(i-s.indexOf`^`),0)) -1 if left is heavier 0 if balanced 1 if right is heavier Ungolfed: s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input p+~~c*(i-s.indexOf`^`),0)) // add weights of all ...


16

JavaScript (ES6), 78 bytes Takes input as 3 distinct variables (mass, radius, unit). (m,r,u)=>m*m/r/[8987e9,56,6276e6,,4184e5,1e-4,360][parseInt(u,30)%7]/2498e11+u Test cases The following snippet runs all test cases in the provided spreadsheet. let f= (m,r,u)=>m*m/r/[8987e9,56,6276e6,,4184e5,1e-4,360][parseInt(u,30)%7]/2498e11+u console....


16

Python 3, 67 66 bytes, 53 bytes def f(p,v,q,w):p-=q;d=((v-w)/p).real*p;return v-d,w+d Try it online! -1 byte thanks to @ngn -13 bytes thanks to @Neil This function f takes four complex numbers as input and returns two complex numbers. The ungolfed version is shown in the following. Ungolfed def elastic_collision_complex(p1, v1, p2, v2): p12 = p1 - ...


13

CJam, 40 39 38 bytes q~21Ue]e!{21,Af-Aest.*:+!}=0'_erNAS*'^ Try it online. How it works q~ e# Read and evaluate the input. 21Ue] e# Append zeroes to achieve a length of 21. e! e# Push all unique permutations. { }= e# ...


12

Python 3, 1,247 * 0.9 = 1,122.3 Well, this is my longest entry by a long shot, but at least I'm shorter than C++. Now with added bonus! It has to be called with a number as the first argument. My universe didn't work with decaying particles other than Higgs Boson, but now it does. Also, I didn't have pluralization or punctuation correct, but I actually do ...


12

Retina, 50 48 bytes = +`((.) (.+)\D*)\2(?!\w*]) $1$3 A-2` ](.). $1 Try it online! Explanation = Remove all equals signs together with the space in front of them. Who needs those anyway... +`((.) (.+)\D*)\2(?!\w*]) $1$3 This performs the substitutions of known quantities. It repeatedly matches a quantity definition (the quantity is any character in ...


12

JavaScript (ES6), 456 448 431 420 bytes This code is using only two small lookup tables: one to identify the quarks: 'CUBDScubds' another one to detect the 'anti' prefix of Mesons: 'SdUcBdBs' Everything else is deduced from the quarks. (s,[a,b]=[...s].map(c=>(i='CUBDScubds'.search(c)%5,m|=1<<i,j+=i<2,k+=i&1,p+=i?i-2?'':'bottom ':'charmed '...


12

05AB1E, 5 3 bytes zOz Try it online! Explanation z # compute 1/x for each x in input O # sum input z # compute 1/sum


11

Perl 6, (707 bytes -10%) Score 636.3 With some unneeded line breaks for a bit more readability: { my%p; %p=<H H2309.469bB64.8WW14.1gg8.82lL7.04cC3.27ZZ1.59pp0.223Zp0.111mM0.0244tT0.0216 W W3pn1Mn1Ln1 Z Z100nN20.6ep3.4mM3.4lL3.4dD15.2sS15.2bB15.2uU11.8cC11.8 t t7.722Wd1Ws1Wb1 T T7.722WD1WS1WB1>; my&f=*.comb[0]; my%h; %h{.&f}="$_ boson" ...


11

Mathematica, 17 bytes +##/(1+##/9*^16)& An unnamed function taking two integers and returning an exact fraction. Explanation This uses two nice tricks with the argument sequence ##, which allows me to avoid referencing the individual arguments u and v separately. ## expands to a sequence of all arguments, which is sort of an "unwrapped list". Here is ...


11

JavaScript (Node.js), 90 88 bytes (m,n,o,p,q,r,s,t,u=(q-=m)*q+(r-=n)*r,v=o*q+p*r-s*q-t*r)=>[o-(q*=v/u),p-(v*=r/u),s+q,t+v] Try it online! Link includes test suite. Explanation: q,r are repurposed as the difference vector between the centres, and u is the square of its length. v is the difference in the dot products of o,p and s,t with q,r, so v/u is the ...


9

Mathematica, 33 bytes So close to Pyth... l/Tr[l={#,#2(x=100Pi*I),1/x/#3}]& This is an unnamed function, which takes R, L and C as its three arguments and returns a list of complex numbers as the result (in the required order VR, VL, VC). Example usage: l/Tr[l={#,#2(x=100Pi*I),1/x/#3}]&[1, 1, 0.00001] (* {0.0548617 + 0.22771 I, -71.5372 + 17.2353 ...


9

CJam, 162 25 bytes • qi),0Xt_ff{*s4Se]}N*s0'xt 12 bytes • q_:i:mh\,mq/ (© Dennis) 18 bytes • q~P*90/ms\_**9.81/ 12 bytes • q$e`{0=~}$e~ (© Dennis) 16 bytes • 1_{_2$+}99*]qi#) 22 bytes • qS/(1+1$s{1$#Lt}/)@@** 18 bytes • q~{,}$_z,f{W$e]}p; 16 bytes • q2>i25A5]:md]W%p 23 bytes • q~${_0=2$1=>{+$3%}|}*]p Dennis agreed to join forces :)


9

Haskell, 18 16 bytes (1/).sum.map(1/) Try it online!


9

MATLAB, 14 bytes In MATLAB norm(...,p) computes the p-norm of a vector. This is usually defined for \$p \geqslant 1\$ as $$\Vert v \Vert_p = \left( \sum_i \vert v_i \vert^p \right)^{\frac{1}{p}}.$$ But luckily for us, it also happens to work for \$p=-1\$. (Note that it does not work in Octave.) @(x)norm(x,-1) Don't try it online!


8

Pyth, 155 153 149 142 141 131 130 bytes 4 bytes thanks to @FryAmTheEggman 1, 5 and 4 bytes thanks to @Jakube 24 bytes: J+1SQp\xtjmsm.[`*dk\ 4JJ Construct the multiplication table from the list [1, 1, 2, 3, ...], which is +1SQ, then print a x and remove its first character. 10 bytes: @.Om^Cd2z2 Straightforward. 18 bytes: c*.t.tyvw7Z*QQ9.81 Uses the ...


8

MATL, 31 30 27 25 bytes 95\16\5B#fG&X>1)b- 20*X^h Input is a 2D char array with ; as row separator: [' 1 5 2 ';' 3 ';' 4 ';' ';' ';'__________________________'] Try it online! Or include an initial t in the code to display the map for ...


8

JavaScript (ES6), 116 111 108 106 bytes -5 bytes by summing via eval(array.join`+`) instead of array.reduce(). -3 bytes by defaulting to 1 instead of 32 - 31, allowing parentheses to be removed. -2 bytes since pivot point is the length of the last line - 1 (s,[t,r,b]=s.split` `)=>Math.sign(eval([...r].map((_,i)=>(t.charCodeAt(i)-31||1)*(i-b.length+1))...


8

Perl 6, 75 64 63 61 bytes 11 bytes saved by switching from map to for, dispensing with the need to put things into intermediate variables for the map to see. 1 byte saved by changing ($^a-$^c)².&{$_/abs} to ($^a-$^c).&{$_/.conj}. 2 bytes saved thanks to @nwellnhof. {(.($^b+$^d,{$_/.conj}($^a-$^c)*($b-$d).conj)/2 for *-*,*+*)} Try it online! ...


7

Pyth, 30 29 28 bytes L*vw*100.l_1)K[Qy0c1y1)cRsKK Try it online.


7

Groovy, 1506 1454 - 10% = 1309 bytes Assumes the number of starting Higgs boson particles is given as the first argument on the command line: A='anti' B='bottom ' C='charmed ' D='downward' E='tau ' F='top ' L='lepton' M='muon' N='nutrino' O=' boson' P='upward ' Q='quark' T='strange ' a=[n:'gluon'] b=[n:B+Q] c=[n:B+A+Q] d=[n:D+Q] e=[n:D+A+Q] f=[n:P+Q] g=[n:...


7

QBasic 2161 * .9 = 1945 2028 * .9 = 1825 1854 * .9 = 1669 bytes Now that QBasic is the LOTM I thought I'd revise my very first answer on PPCG ever. Managed to knock off 140 bytes, not bad! Based on feedback by @TaylorScott and @DLosc I've done a complete redesign: Time keeping altered Formatting now conforms to spec Saved a ton of bytes by making an ...


7

PHP, 989 - 10% = 890.1 bytes Sub 1K baby! Thanks vsz, this was a really fun challenge. So many ways to go about it and very hard to verify your output is correct. The program can take a command line argument to specify the starting number of Higgs bosons, e.g. php universe_simulator.php 5 <?eval(gzinflate(base64_decode('bVNdb9Q6EH3nV4TVSEm0ozRpt/...


7

CJam, 184 105 98 96 bytes 00000000: 22 73 20 6d 20 6b 67 41 20 4e 20 4a 20 57 20 48 "s m kgA N J W H 00000010: 7a 50 61 43 20 56 20 46 20 ce a9 53 20 57 62 54 zPaC V F ..S WbT 00000020: 20 48 22 32 2f 53 66 2d 22 d6 9c 64 c6 a1 24 a4 H"2/Sf-"..d..$. 00000030: 4b f9 1c 4a 57 f4 61 79 31 ed 82 34 22 33 31 38 K..JW.ay1..4"318 00000040: ...


7

Python 2, 69 bytes lambda s:cmp(sum(ord(c)%16*(i-s.find('^'))for i,c in enumerate(s)),0) The modulus ord(c)%16 extracts the value of a digit character while getting 0 for space. For each character, its torque contribution is computed as its weight times the signed distance to the pivot i-s.find('^'), and these are summed and compared to 0, producing one of ...


7

C, 125 122 121 bytes b[99]={};main(l,c){for(;(c=getchar())<95u;)b[c]=(l+=c==10);for(c=47;++c<58;)b[c]&&printf("%c,%f\n",c,sqrt((l-b[c])*20));} Compile & run with gcc -w golf.c -lm && cat balls.txt | ./a.out.


7

Jelly, 78 bytes ²×⁽LÐ÷ȷ14÷⁴÷5÷⁵ị“¤Fðẏẏż“?A⁺ẏẏż“¬ŀż“8Ƙż“¡uþ³⁾ẉṿÆ“¡’¤;⁵ị“ÞṠlT“¡ṁæ-“;ØḲ“ȧ¹“ṫD“}»¤ Try it online! Stuff to do with strings isn't really Jelly's type of challenge... -7 bytes thanks to user202729 (thanks for helping me not get beaten by JavaScript :D)


7

Pyth, 12 10 bytes !%ys*VQUQs Try it online Saved 2 bytes thanks to Mr. Xcoder and Erik the Outgolfer. Explanation !%ys*VQUQs *VQUQ Multiply each input by its index. ys Take twice the sum (to handle half-integer positions). !% sQ Check if that's a multiple of the total weight.


Only top voted, non community-wiki answers of a minimum length are eligible