11
\$\begingroup\$

Given a long multi-sentence string, the goal is the count the occurrences of the following words: park, lake, Santa Fe.

The words can appear as substrings of other words, i.e, parked counts as an instance of park.

Santa Fe counts as one word (so the space matters). Both Santa and Fe appearing alone or out of order does not count as an instance of Santa Fe.

The output should be counts of the number of times the words park, lake, and Santa Fe appear in the input string, in any consistent and readable format and order.

For example, for an input of

I'm just parked out by the lake; the lake 80 miles from Santa Fe. That's where i'm parked, out by the lake.

the output would resemble

2 3 1

as there are \$2\$ occurrences of park, \$3\$ of lake and \$1\$ of Santa Fe.

Good luck! If you want a fun song to test this on you can use the lyrics to Parked by the lake - Dean Summerwind (below) and use it as the input; The output ought to be 20 24 14.

Winning submission is the least number of bytes.


Lyrics to Parked by the Lake

\$\endgroup\$
5
  • \$\begingroup\$ Also, for future reference, we suggest using the Sandbox first to get feedback :) \$\endgroup\$ Jul 26, 2021 at 1:38
  • \$\begingroup\$ I will definitely use that next time, my apologies! \$\endgroup\$
    – Taako
    Jul 26, 2021 at 1:40
  • 15
    \$\begingroup\$ Is input case sensitive? If input is "Park park PARK.", should I output 3 for park or 1? \$\endgroup\$
    – tsh
    Jul 26, 2021 at 2:49
  • 5
    \$\begingroup\$ Suggested example: Santa fed his reindeer and changed the spark plugs on his sleigh, then slaked his thirst with a nice glass of milk... \$\endgroup\$ Jul 26, 2021 at 16:40
  • 2
    \$\begingroup\$ Suggest testcase ppaparparkllalaklakeSSaSanSantSantaSanta Santa FSanta Fe with answer \$1,1,1\$. \$\endgroup\$
    – Noodle9
    Jul 27, 2021 at 8:49

22 Answers 22

7
\$\begingroup\$

J, 36 30 bytes

1#.('Santa Fe';park`lake)E.&><

Try it online!

-5 thanks to Bubbler!

Returns count in order "Santa Fe", "park", "lake".

  • E. Does all the matching work, returning three rows of ones and zeros, where ones represent the start of each match.
  • 1#. Sums row-wise.
\$\endgroup\$
1
  • 2
    \$\begingroup\$ 31 bytes 1#.<E.~&>park`lake,<@'Santa Fe' by avoiding rank fiddling, and then 30 bytes 1#.('Santa Fe';park`lake)E.&>< by reordering the output (I think it's allowed by the line "in any consistent and readable format and order") \$\endgroup\$
    – Bubbler
    Jul 26, 2021 at 3:26
6
\$\begingroup\$

Python 2, 48 bytes

lambda k:map(k.count,["park","lake","Santa Fe"])

Try it online!

-7 bytes thanks to tsh

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Python 2: lambda s:map(s.count,["park","lake","Santa Fe"]) or Python 3: lambda s:[*map(s.count,["park","lake","Santa Fe"])] \$\endgroup\$
    – tsh
    Jul 26, 2021 at 2:52
  • 1
    \$\begingroup\$ @tsh oh yeah, been a while since I've done this. thanks \$\endgroup\$
    – hyper-neutrino
    Jul 26, 2021 at 3:09
  • 4
    \$\begingroup\$ FYI, [*...] is not necessary in Python 3 since outputting a generator in place of a list is allowed. \$\endgroup\$
    – Bubbler
    Jul 26, 2021 at 3:38
5
\$\begingroup\$

05AB1E, 14 bytes

„†¹Š°#„š´º´™ª¢

Try it online!

„†¹Š°            -- compressed string  => "park lake"
     #           -- split on spaces    => ["park", "lake"]
      „š´º´      -- compressed string  => "santa fe"
           ™     -- title cased        => "Santa Fe"
            ª    -- append to the list => ["park", "lake", "Santa Fe"]
             ¢   -- for each word, count the number of occurences in the input
\$\endgroup\$
5
\$\begingroup\$

Factor, 58 bytes

[ { "park" "lake" "Santa Fe" } [ count-subseq ] with map ]

count-subseq:

  • Doesn't exist in build 1525 (the one TIO uses).
  • Has stack effect ( subseq seq -- n ) in every build that permits the omission of whitespace after strings.
  • Has stack effect ( seq subseq -- n ) in the current build, which means we don't need an extra swap.

Therefore, the code retains the whitespace after strings but it's still shorter than the alternatives. Also, have a picture.

enter image description here

\$\endgroup\$
5
\$\begingroup\$

Jelly, 19 17 bytes

ẆċⱮ“£Ḟœ“ċẈ“¡ç1Ḋı»

Try it online!

-2 bytes thanks to Nick Kennedy

How it works

ẆċⱮ“£Ḟœ“ċẈ“¡ç1Ḋı» - Main link. Takes a string S on the left
Ẇ                 - Yield all contiguous sublists of S
   “£Ḟœ“ċẈ“¡ç1Ḋı» - Compressed string list; ["park", "lake", "Santa Fe"]
  Ɱ               - Over each string:
 ċ                -   Count the occurrences in the sublists
\$\endgroup\$
0
5
\$\begingroup\$

Vyxal r, 16 bytes

`⟇æ
ȴ
ʁ∨ Fe`↵vO

Try it Online!

Explained

`...`↵vo
`...`      # the string "park\nlake\Santa Fe"
     ↵     # split on newlines to form a list
      vo   # for each in that list, get the count in the input
\$\endgroup\$
4
  • \$\begingroup\$ this doesnt seem to work on all inputs, i used the entire lyrics of the song i.imgur.com/TqbX07t.png \$\endgroup\$
    – Taako
    Jul 26, 2021 at 1:33
  • \$\begingroup\$ @Taako that's because you need to wrap it in " and replace the newlines with \n \$\endgroup\$
    – lyxal
    Jul 26, 2021 at 1:34
  • \$\begingroup\$ I don't think you need the S flag, as the output format has been loosened \$\endgroup\$ Jul 26, 2021 at 1:37
  • \$\begingroup\$ 16 with r \$\endgroup\$
    – emanresu A
    Jul 26, 2021 at 4:20
5
\$\begingroup\$

C (gcc), 124 \$\cdots\$ 106 103 bytes

*f(int*s){int c[3]={},i;for(;*s;++s)for(i=3;i--;)c[i]+=!wcsncmp(s,L"parklakeSanta Fe"+i*4,4<<i/2);s=c;}

Try it online!

Saved a 2 bytes and fixed a bug thanks to ceilingcat!!!

Inputs a string.
Return a pointer to an array of occurrence counts for park, lake, and Sante Fe.

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

JavaScript (ES6), 57 bytes

s=>["park","lake","Santa Fe"].map(w=>s.split(w).length-1)

Try it online!

s=>[(g=w=>s.split(w).length-1)`park`,g`lake`,g`Santa Fe`]

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Brachylog, 33 bytes

g;"park
lake
Santa Fe"ṇᵗz{{sᵈ}ᶜ}ᵐ

Try it online!

I'm not sure if this feels very golfable, or not even remotely golfable... ⟨s≡⟩ᶜ happens to tie {sᵈ}ᶜ, so maybe there's some shenanigan to pull there, but that seems doubtful.

g;                    Wrap the input in a list, and pair it with
  "..."               "park\nlake\nSanta Fe"
       ṇᵗ             split on newlines.
         z            Cycling zip.
          {     }ᵐ    For each pair [input, park/lake/Santa Fe]:
           {  }ᶜ      in how many ways
            sᵈ        is park/lake/Santa Fe a substring of the input?
\$\endgroup\$
4
\$\begingroup\$

Bash, 59 51 bytes

grep -Eo 'park|lake|Santa Fe'|sort|uniq -c|awk NF=1

Try it online!

-8 bytes thanks to user41805's nice awk trick!

Returns in order "lake", "park", "Santa Fe".

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The awk statement can be simplified to NF=1 \$\endgroup\$
    – user41805
    Sep 11, 2021 at 11:46
  • \$\begingroup\$ @user41805 TIL. Thanks! \$\endgroup\$
    – Jonah
    Sep 17, 2021 at 16:20
3
\$\begingroup\$

Japt, 21 bytes

`pk
lake
S Fe`·£èX

Try it

\$\endgroup\$
2
  • \$\begingroup\$ I'd love to know how you got around not writing the 'ar' in 'park' and the 'anta' in 'Santa'! \$\endgroup\$ Jul 26, 2021 at 11:58
  • 2
    \$\begingroup\$ String compression, @Pureferret. There are 2 unprintables in it, one after the p and the other after the Â; charcodes 135 & 28, respectively. \$\endgroup\$
    – Shaggy
    Jul 26, 2021 at 12:08
2
\$\begingroup\$

Ruby, 49 bytes

p %w[park lake Santa\ Fe].map{|r|$_.scan(r).size}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Clojure, 55 bytes

#(for[r[#"park"#"lake"#"Santa Fe"]](count(re-seq r %)))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

PHP, 73 bytes

foreach([park,lake,'Santa Fe']as$r)echo~-count(explode($r,$argv[1])).' ';

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ posted at the same time as my answer.. which could be improved by a foreach like in yours ;) \$\endgroup\$
    – Kaddath
    Jul 26, 2021 at 13:08
  • \$\begingroup\$ Great PHP minds think alike. Nice use of substr_count in your answer. \$\endgroup\$ Jul 26, 2021 at 13:14
2
\$\begingroup\$

PHP -F, 71 66 65 bytes

foreach([park,lake,"Santa Fe"]as$w)echo~_.substr_count($argn,$w);

Try it online!

Just using dedicated function and usual tricks..

EDIT: 5 bytes saved using a foreach like in Guillermo Phillips's answer

EDIT 2: another byte the dust thanks to Guillermo Phillips again!

\$\endgroup\$
1
  • \$\begingroup\$ Save a byte by echoing the space first. \$\endgroup\$ Jul 26, 2021 at 13:16
2
\$\begingroup\$

K (ngn/k), 39 bytes

{+/`park`lake`"Santa Fe"=/:`$(4'x),8'x}

Try it online!

  • (4'x),8'x build a list containing all 4-length sliding windows, and all 8-length sliding windows, of the input
  • `$ convert the slices from strings to symbols
  • `park`lake`"Santa Fe"=/: build a matrix with each of its three columns indicating whether or not each slice of the input is equal to one of the search terms
  • +/ sum column-wise, returning the number of times park, lake, and Santa Fe appear in the input (respectively)
\$\endgroup\$
2
\$\begingroup\$

Perl 5, 44 bytes

for$w("park","lake","Santa Fe"){say s/$w//g}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

WolframLanguage (Mathematica), 50 bytes

(f|->f~StringCount~#&/@{"park","lake","Santa Fe"})

The above won't work in tio.run because |-> is too new (introduced in MMA v. 12.2, while tio.run uses MMA 12.0).

Thus here's a 51-byte version that does:

(f=#;f~StringCount~#&/@{"park","lake","Santa Fe"})&

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Zsh, 45 bytes

for x (park lake Santa\ Fe)grep -o $x F|wc -l

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

Excel, 63 bytes

Cells A3:C3 = park, lake, Santa Fe (16 bytes)
=(LEN(A1)-LEN(SUBSTITUTE(A1,A3:C3,"")))/{4,4,8}  (47 bytes)

Link to Spreadsheet

The easiest way to count occurrences of a substring is to substitute the substring with "" and then divide the difference in length by the length of the substring.

Self contained formula, 68 bytes

=(LEN(A1)-LEN(SUBSTITUTE(A1,{"park","lake","Santa Fe"},"")))/{4,4,8}
\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 24 bytes

*M`park
*M`lake
Santa Fe

Try it online! Link is to verbose version of code. Explanation:

*M`park

Explicitly print the count of matches of the word park without changing the input buffer.

*M`lake

Explicitly print the count of matches of the word lake without changing the input buffer.

Santa Fe

Count the matches and implicitly print the result.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 24 bytes

IE⪪”&/‽∕“⎇⊙g…3CπΦP6”¶№θι

Try it online! Link is to verbose version of code. Explanation:

   ”...”        Compressed string `park\nlake\Santa Fe`
  ⪪     ¶       Split on newlines
 E              Map over each substring
         №      Count of
           ι    Current substring
          θ     In original input
I               Cast to string
                Implicitly print
\$\endgroup\$

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