Discussion
· May 24

Code Golf: Smiley Faces

It's Friday and a long weekend is upon us, so let's have a round of code golf!

Nowadays, it's rare to find a text message without at least one emoji. 😃😄😎🙂😊😀😁😆😂
But back in the day, people had to be creative to express their emotions in text. They would use emoticons, which are symbols made up of keyboard characters :^) =] ) B).
Our challenge for today is to create a function that receives a string as argument and returns the total number of smiling faces or happy faces.
Each smiley face must have one smiling mouth, which should be marked with ), ], }, D or >.
A smiley face can have a pair of eyes but it does not have to. Valid characters for eyes are :, ;, 8, B or =.
A nose is optional, and can be represented these characters: -, ^, c, o and ~.

Valid smiley face examples:

    :) :D ;-D :~) :‑) :-] =)    
    :] :-> :> 8-) :D 8‑D )
    8) :-} :} :o) :c) :^) =]
    :‑D 8D =D B^D

Input

"count how many smiley faces are here :)"

Output

1

Note

Rules

  1. The signature of the contest entry MUST be:

    Class codeGolf.SmileyFace Extends %RegisteredObject
    {
    
        ClassMethod Count(i As %String) As %Integer
        {
            q 0 // Your solution here
        }
    
    }
    
  2. It is forbidden to modify class/signature, including but not limited to:

    • Adding inheritance
    • Setting default argument values
    • Adding class elements (Parameters, Methods, Includes, etc).
  3. It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:

    ClassMethod Build(f As %Integer)
    {
      W ##class(myPackage.myClass).test(a)
    }
    
  4. The use of $ZWPACK and $ZWBPACK is also discouraged.

  5. You can use this test case:

    Class codeGolf.unittests.SmileyFace Extends %UnitTest.TestCase
    {
    
        Method TestSolutions()
        {
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count(")s).:D :~) ;~D :) xD"), 7)
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count(""), 0)
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count(":):(':D:O:;"), 2)
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count("let's put a smile on that face :o)"), 1)
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count("(smiley)"), 1)
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count("12 weirds :] :-> :> 8-) 8) :-} :} :o) :c) :^) =] =)"), 12)
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count(";] ;) ;-) winky"), 3)
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count("B^D 8D X‑D Laughing :D"), 4)
            Do $$$AssertEquals(##class(codeGolf.SmileyFace).Count("Sadness >:( :[ :{ :("), 0)
        }
    
    }
    

Discussion (16)2
Log in or sign up to continue

In the past we got challenges with a better definition... ;-((  ☹


In the original post, I'm pretty sure, there were
")s)).:D :~) ;~D :) xD ))" instead of
")s).:D :~) ;~D :) xD"

and
"(smiley) ))" instead of
"(smiley)"

Nevertheless,

"Sadness >:( :[ :{ :("  ---> you say: 0
         >                     I say: 1 (0 Eyses, 0 noses 1 mouth)
and there is no rule about interspace requirements

My quick solution:

ClassMethod Count(s)
{
	while $locate(s,"[:;8B=]?[co\^~-]?[\)\]\}D>]",$g(i),i),$i(n){} q +$g(n)
}

/// If one accepts "" as 0 then change +$g(n) to $g(n)

/// I would like a solution as (see the + char in regex)
/// but then the rules should be changed
///
ClassMethod Count(s)
{
	while $locate(s,"[:;8B=]?[co\^~-]?[\)\]\}D>]+",$g(i),i),$i(n){} q +$g(n)
}