Author Topic: [challenge] A37 : Ice Cream Sandwich  (Read 1064 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10158
[challenge] A37 : Ice Cream Sandwich
« on: March 23, 2022, 09:49:14 AM »
An ice cream sandwich is a string that is formed by two identical ends and a different middle.

Some examples of ice cream sandwiches:
"AABBBAA"

"3&&3"

"yyyyymmmmmmmmyyyyy"

"hhhhhhhhmhhhhhhhh"

Notice how left and right ends of the sandwich are identical in both length and in repeating character. The middle section is distinctly different.

NOT ice cream sandwiches:
"BBBBB"
;; You can't have just plain "icecream".

"AAACCCAA"
;; You can't have unequal sandwich ends.

"AACDCAA"
;; You can't have more than one filling.

"A"
;; You can't have fewer than 3 characters.

Write a function that returns true (T) if a string is an ice cream sandwich and false (NIL) otherwise.

Examples:
(isIcecreamSandwich '("CDC"))
> T

(isIcecreamSandwich '("AAABB"))
> nil

(isIcecreamSandwich '("AA"))
> nil

Notes
- An ice cream sandwich must have a minimum length of 3 characters, and at least two of these characters must be distinct (you can't only have the filling!).
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1068
  • 40 + years of using Autocad
Re: [challenge] A37 : Ice Cream Sandwich
« Reply #1 on: March 23, 2022, 07:31:52 PM »
This is a bit similar to what I am trying to solve. If we take "AAABBBAAA" and make it ("A" "A"  "A"  "B" "B" "B" "A" "A" "A" ) maybe becomes
(("AAA")("BBB")("AAA")) so if (= (nth 0 lst)(nth 2 lst)) it is icecream. As I said before I get lost in mapcar and lambda's
A man who never made a mistake never made anything

ronjonp

  • Needs a day job
  • Posts: 7446
Re: [challenge] A37 : Ice Cream Sandwich
« Reply #2 on: March 23, 2022, 08:01:20 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun isicecreamsandwich-rjp (str / a)
  2.   (and (equal (setq a (vl-string->list str)) (reverse a)) (= 2 (length (vl-sort a '<))))
  3. )
  4. (mapcar 'isicecreamsandwich-rjp
  5.         '("AABBBAA"       "3&&3"          "yyyyymmmmmmmmyyyyy"            "hhhhhhhhmhhhhhhhh"
  6.           "BBBBB"         "AAACCCAA"      "AACDCAA"       "A"             "CDC"
  7.           "AAABB"         "AA"
  8.          )
  9. )
  10. ;; (T T T T nil nil nil nil T nil nil)

Windows 11 x64 - AutoCAD /C3D 2022

Custom Build PC

VovKa

  • Swamp Rat
  • Posts: 1488
  • Ukraine
Re: [challenge] A37 : Ice Cream Sandwich
« Reply #3 on: March 23, 2022, 08:49:29 PM »
"AACACAA"

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1470
  • class keyThumper<T>:ILazy<T>
Re: [challenge] A37 : Ice Cream Sandwich
« Reply #4 on: March 24, 2022, 05:55:55 AM »
"AACACAA"

:)  Double decker sandwich !!


;;-----------

Is it time to allow the VL-xxx functions ??
called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober

JohnK

  • Administrator
  • Seagull
  • Posts: 10158
Re: [challenge] A37 : Ice Cream Sandwich
« Reply #5 on: March 24, 2022, 08:41:15 AM »
--->%
Is it time to allow the VL-xxx functions ??
Sure.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: [challenge] A37 : Ice Cream Sandwich
« Reply #6 on: March 24, 2022, 08:53:57 AM »
Should have known better!  I came here thinking I would find photos of awesome looking Ice Cream Sandwiches.

I now know how Penny feels from Big Bang feels
« Last Edit: March 25, 2022, 03:51:37 PM by Krushert »
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

JohnK

  • Administrator
  • Seagull
  • Posts: 10158
Re: [challenge] A37 : Ice Cream Sandwich
« Reply #7 on: March 24, 2022, 09:00:25 AM »
lol
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12711
  • London, England
Re: [challenge] A37 : Ice Cream Sandwich
« Reply #8 on: August 29, 2022, 09:53:49 AM »
Taking a slightly different approach:
Code - Auto/Visual Lisp: [Select]
  1. (defun isicecreamsandwich-lm ( s / n p )
  2.     (and (< (setq p (1+ (vl-string-mismatch s (substr s 2)))) (setq n (strlen s)))
  3.          (wcmatch (substr s 1 p) (strcat "~*[~" (substr s 1 1) "]*"))
  4.          (wcmatch (substr s (- n p -1)) (strcat "~*[~" (substr s 1 1) "]*"))
  5.          (wcmatch (substr s (1+ p) (- n p p)) (strcat "~*[~" (substr s (1+ p) 1) "]*"))
  6.     )
  7. )