Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Arrange a Haft-Sin table

It's Nowruz and you want to set up a Haft-Sin table by words. This means finding seven words that start with letter s.

The Challenge

Write a program which its input is a list of words separated by space, and output at most first 7 words which starts by letter s. If the s is before letter h it doesn't count because it would not pronounce /s/.

Input

An arbitrary length string containing words separated by space. Words must not contain anything other than letters (uppercase or lowercase) and numbers and _.

These inputs are valid:

hello Puzzle code_golf 12
Start say_hello separating_by_space_is_right
I am a valid word list

And these inputs are invalid:

code-golf, #invalid_word, separating_by_comma_is_wrong
I'm an invalid word list

Output

The first 7 words which starts by letter S-s and not followed by letter H-h, in every acceptable way (comma separated, space separated, new-line etc) and in any order.

  • If two words are duplicate don't count them twice. Every single word is count once.
  • If the input contains less that 7 word starting with s output nothing. Don't output the words.
  • The output must contain the exact word which is in the input. So if the input contains SuPER, output should be SuPER and not SUPER or super or any other form of lower and upper case.
  • Words pronunciation matter. The word Speed and SPEED both count the same. You may want to lowercase all the input and unique the words and then check for words.

test-cases

input:

speed speed new car book seven sad sum power fun super sister silver silly start

output:

speed seven sad sum super sister silver

input:

speed SpEEd new book seven sad sum power fun super sister silver silly start

output:

speed seven sad sum super sister silver

input:

sheep speed new car book seven sad sum power fun super sister silver silly start

output:

speed seven sad sum super sister silver

input:

first second third

output:


Edited

This was my first question and I missed many special cases. I try to clarify them.

Answer*

Draft saved
Draft discarded
Cancel
6
  • \$\begingroup\$ -2 bytes by stealing regex from other answers. \$\endgroup\$ Commented Mar 23, 2022 at 5:53
  • \$\begingroup\$ @pajonk - Thanks! I even think I saw it, but didn't understand it, and didn't try it out... Now I need to figure-out how it works... \$\endgroup\$ Commented Mar 23, 2022 at 7:36
  • \$\begingroup\$ "Negative lookahead" is the search term to look for. Example link \$\endgroup\$ Commented Mar 23, 2022 at 7:40
  • \$\begingroup\$ SPEED sPeEd shopper SPEED new car book seven sad sum power fun super sister silver silly start should result in SPEED seven sad sum super sister silver, so a simply uniquify won't suffice in this case. \$\endgroup\$ Commented Mar 23, 2022 at 8:10
  • 1
    \$\begingroup\$ @KevinCruijssen - Fixed now, but it cost a lot... \$\endgroup\$ Commented Mar 23, 2022 at 8:45

AltStyle によって変換されたページ (->オリジナル) /