12
\$\begingroup\$

Challenge

Given a string such as Hello World!, break it down into its character values: 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33.

Then calculate the difference between each consecutive pair of characters: 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67.

Finally, sum them and print the final result: -39.

Rules

  • Standard loopholes apply
  • No using pre-made functions that perform this exact task
  • Creative solutions encouraged
  • Have fun
  • This is marked as , the shortest answer in bytes wins but will not be selected.
\$\endgroup\$
  • 16
    \$\begingroup\$ Dennis's observation shows that this task is phrased in a more complicated way than necessary. \$\endgroup\$ – Greg Martin Sep 26 '16 at 2:47
  • \$\begingroup\$ Can a language accept input as a character array even if it supports string types? \$\endgroup\$ – Poke Sep 26 '16 at 15:13
  • \$\begingroup\$ @Poke sorry, has to be a string \$\endgroup\$ – dkudriavtsev Sep 27 '16 at 20:19
  • \$\begingroup\$ @GregMartin I actually did not realize that until later. The challenge should stay this way though. \$\endgroup\$ – dkudriavtsev Sep 27 '16 at 20:19
  • \$\begingroup\$ @DJMcMayhem Good to know, all other forms of output are hereby allowed. \$\endgroup\$ – dkudriavtsev Sep 27 '16 at 20:58

42 Answers 42

0
\$\begingroup\$

PHP, 39 Bytes

<?=ord(substr($s=$argv[1],-1))-ord($s);
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 30 bytes

Tr@Differences@ToCharacterCode
\$\endgroup\$
0
\$\begingroup\$

Befunge-93, 16 bytes

~:v_$-.@
~\<^`0:

Befunge-98, 14 bytes

~v >-.@
\>#^~

Reads (~) the first character, and then loops reading (~) a character and swaping (\) the top 2 items in the stack (so the first character keeps being raised to the top of the stack) until EOF, then substracts (-) the top 2 items, outputs (.) the result and ends (@).

\$\endgroup\$
0
\$\begingroup\$

C, 69 bytes

main(i,e,z){while(EOF!=(i =getc(stdin))){z+=e-i;e=i;}putc(z,stdout);}
\$\endgroup\$
  • \$\begingroup\$ Useless whitespace at i =getc. \$\endgroup\$ – Yytsi Sep 28 '16 at 13:41
0
\$\begingroup\$

Pyth (fork), 5 bytes

s.+CM

Does not work on the online interpreter, as current online interpreters are based only on the main repo.

Explanation:

   CM  Map ordinals over input
 .+    Deltas, calculates differences between consecutive numbers
s      Sums the result
\$\endgroup\$
0
\$\begingroup\$

Haskell, 27 bytes

\x->(ord.last)x-(ord.head)x

Unfortunately, Haskell has no knowledge of character codes until you import Data.Char. Fortunately, Haskell already treats strings as character arrays.

\x->                             --Syntax for anonymous function of 'x'
    (ord                         --Character code of...
        .last)                     --last element in the array
              x                  --Apply to argument
               -
                (ord             --Character code of...
                    .head)         --first element in the array
                          x      --Apply to argument
\$\endgroup\$
  • 1
    \$\begingroup\$ Don't you have to import ord? \$\endgroup\$ – Jo King Aug 20 at 1:05
0
\$\begingroup\$

Pyke, 5 bytes

m.o$s

Try it here!

m.o   -   map(ord, input)
   $  -  delta(^)
    s - sum(^)
\$\endgroup\$
0
\$\begingroup\$

Racket 121 bytes

(λ(s)(let((l(map char->integer(string->list s))))(for/sum((i(range 1(length l))))(-(list-ref l i)(list-ref l(- i 1))))))

Ungolfed version

(define f
  (λ(s)
    (let ((l (map char->integer
                   (string->list s))))
      (for/sum ((i (range 1 (length l))))
        (- (list-ref l i)
           (list-ref l (- i 1)))))))

Testing

(f "Hello World!")

-39
\$\endgroup\$
0
\$\begingroup\$

Groovy (80 Bytes)

f={n->s=[];n.eachWithIndex{x,i->s<<(i+1<n.size()?(int)n[i+1]-(int)x:0)};s.sum()}
\$\endgroup\$
0
\$\begingroup\$

brainfuck, 31 bytes

Probably easy to outgolf, any ideas about this code will be appreciated.

>+[>,>,[-<->]<]<<[>[-<+>]<<]>-.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ How does this print negative numbers? Also, I don't think this works? \$\endgroup\$ – Jo King Aug 20 at 0:31
0
\$\begingroup\$

Seed, 6014 bytes

17 47142938850356537668025719950342285215695875470457212766920093958965599014291682211474803930206233718243166021324700185443274842742744297183042397910454208210130742260519105651032450664856693714718759816232968955289663444878636053035645231246428917992493766505872029989544065894418166685284738859999912471659057565000392419689361271972485444731284404987238803115210428572945108908191097214978382112218242627719651728712577306652405452922782307249485947045215982784790396555726144526602778473387279897334709142853151393812930171163300904436548870603006549005515709074682843356844201220668045057407146751793470423217099467145255174198241606814578351260769359571053755888106818197239116973006365593563235361647743411341624822052103816968153274122434280200888824954875622811325064255818154979564925710534165572852442761249176778416688044630942040966271963723430245979221181930857841829694362184653939393940015797332978459794253176110314873994228261888801228999293570329618551223457182420746927212801550646743152754821640064626761542582557138452651970009253770914346130172884305622027370793496993281847017017643506435562229916984107083951938286577012273222191422054315198157936674247934699496471202544270325061352014830137178245082445717253260177560449757186762445707057028987371278573629077370632470496186218574320801798046510846708620502139560277546345198686675095078255875594169064796673074708822106659920187882062247609587560174781170641367430722951002242213604709887062481149928551745163110045572994991844223216663621203042075294195007458339984527333125093390189721042315604498435269143549420166732177200370228527273606218617171975362431824163269672003982537382982066136613799403024924018145511099557720492305303748099327810811511080314262364010281851651151072957475365629128068033597559560186625877942054704386175359499573139930378099420149452745731809033737756051947913924265484582800618244473333957173960222243311738522875022546610298627492222587971756897328087719407454153248557203886421828643453889090192355970705084245312184441674098515659253482621260617211786550204852895652236768886852209506535523414991099331857674826373947830587028494510697603296607361093480842935154672353288419699354739650168309017848485131553416956405911683526896232046773861961911767319373432460217755874481607587604361758089936007730253450733375831228127106295259261723611771334468553746160739548375950046831923765023329346333968732796413192682936767133122325481273354810124729664400173367781325488656859581438769940474229394692089519981810909719628263357284973442177568041416363386891516725592952892168077523560584005586276794967492051823290615767599202657060820223928678900774601616908031321346819422162123048834532926372862962159255934240435694566497798544870186550219886342379298214007368081326725550763589917206162393892085506551547475259270513853987294911388226039365971184089828739349642347312302559286882065147953715607221387657413593069535573044067517274676745306396611760657091792151803798859781616126637075577936361782593546481811651450365118155866449850474140044293772144065341051900055416408240857348697564252386403719942197789892382627153382011984996644288495699209129097948405810551134169739499539470610790009272281731894550593600643079188663110695127017324336488487580799309995227054576681630676222848231145106058050452439356753552872060820230589152143268436210090733908507724084889788244157692417246691477400856716677564609725979550100138132944851304473466485128295568194188600543661131650115157260312071377345528896088825140611689855398183561760791314285051031265520689685711014805539543231880745343434703336135460092978233226869465481514867102356014796452769427144878161981604598284226724364115327232429251648806008491831667849783134258050318352705228015592040283819580427654502685183767187264525468057644591817805077799577374180646351741390409612391460346723479963058814536210233414837391674888122618077194836922671914065618441647043153132083998473534427301829870250489601442408268679696053841229993528867954466850948420575595935246933986705544865791748152250860891691689550405507636051241221193981460705452034472581219219131492199338074507051525608727607753872348347510045928104973092730607238725575621161530652078644079719913360273376285341372904769462063481044112178129302484205828990243798270446582298702462804481665731925271856576016183221640793857602133368192713012660584670729232620780030562375903926774141506962613831669490567906301132856157162867313939619561102306215358680938754366282457693877443057948719703797904712882087077090587783853798102152390947224144124105071446453239724581037164590701776798626325188627581359321782931929843635657783982195061329702192052726640085906290737880814887482893410437504205275570230930090834795876542838231267620898665679543525456703419900522994914159773392943043903186459127433183779018202183011184038085476805931394464684670489009922829358411550911905248719656009989360709676867351399598068266946139357582112926638401353993307339918106185805556120316564529026756467819003076545744254320222778049542753133527192649704482623663506356794213738428920928849672841431353190441722416692276901115889938513206017095803856117910254971161240799053346999051488769167404088861181374808024511916665064497767512426470207134173736740946015760679693254719991498773014507172860317142784288296333191476051757537119748829048030691437064939656766105398512319172423549946763727605010882763970121081142051597256133655915705933173565060765616683823033538356198220570566417229714432455364829470178695310405745225393222390495063348719712582275526537940768106257772996928962724609811699327814059261779499421288213191050232888701422465784983890609391833066104790500403345737145544209827453309885263187154808017568798298051331810458835354554547127662702469973394033949069059725266828660824789855081371335929552101868487934072495897313804620595847701970125002539551932701207295152245398863089760079740345450129279644488605227478724223384468254156317867623300352874555658914942314098345377792

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C# .NET, 156 bytes

public class P{public static void Main(string[]a){int q=0;for(int i=0;i<a[0].Length;i++){q+=i!=a[0].Length-1?a[0][i+1]-a[0][i]:0;}System.Console.Write(q);}}

Try online

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.