Extract words containing diphthongs om and eu, solution atom and nucleus - Lisp
Формулировка задачи:
LISP PROBLEM
TEXT- The atom is a basic unit of matter that consists of a dense central nucleus surrounded by a cloud of negatively charged electrons. The atomic nucleus contains a mix of positively charged protons and electrically neutral neutrons.The electrons of an atom are bound to the nucleus by the electromagnetic force.
1) extracted repeating words in the table , solution atom-2 , nucleus-3, electrons-2
2) extract words containing diphthongs om and eu, solution atom and nucleus
Решение задачи: «Extract words containing diphthongs om and eu, solution atom and nucleus»
textual
Листинг программы
(defun list-of-rep (text &optional res) (let ((res nil)) (dolist (w (setof text) res) (let ((c (count w text))) (when (> c 1) (push (list w c) res)))))) ==> list-of-rep (list-of-rep '(The atom is a basic unit of matter that consists of a dense central nucleus surrounded by a cloud of negatively charged electrons. The atomic nucleus contains a mix of positively charged protons and electrically neutral neutrons.The electrons of an atom are bound to the nucleus by the electromagnetic force.)) ==> ((The 4) (by 2) (nucleus 3) (ATOM 2) (of 5) (charged 2) (a 4))
Объяснение кода листинга программы
В этом коде определенная функция list-of-rep
принимает два аргумента: текст и необязательный список результатов. Если список результатов не задан, он инициализируется как nil
.
Функция использует dolist
для перебора каждого слова в тексте. Для каждого слова выполняется внутренний блок let
, который считает количество повторений слова в тексте и проверяет, больше ли это количество 1. Если да, то слово и его количество добавляются в список результатов с помощью push
.
В данном примере список результатов будет содержать следующие элементы:
The 4
by 2
nucleus 3
ATOM 2
of 5
charged 2
a 4
Эти элементы представляют собой слова из исходного текста, в которых есть диптихиom
иeu
, а такжеatom
иnucleus
. Каждое слово сопровождается количеством его повторений в тексте.
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д