You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

open System
let countRussianCharacters (str: string) =
str.ToCharArray()
|> Array.filter (fun c -> (c >= 'А' && c <= 'Я') || (c >= 'а' && c <= 'я') || c = 'Ё' || c = 'ё')
|> Array.length
let isLowercaseLatinSorted (str: string) =
let lowercaseChars =
str.ToCharArray()
|> Array.filter (fun c -> c >= 'a' && c <= 'z')
if lowercaseChars.Length <= 1 then true
else
lowercaseChars
|> Array.pairwise
|> Array.forall (fun (a, b) -> a <= b)
let shuffleWords (str: string) =
let random = Random()
str.Split(' ')
|> Array.sortBy (fun _ -> random.Next())
|> String.concat " "
let isPalindrome (str: string) =
let cleanStr = str.Replace(" ", "").ToLower()
let chars = cleanStr.ToCharArray()
chars = Array.rev chars
let shuffleCharacters (str: string) =
let random = Random()
str.ToCharArray()
|> Array.sortBy (fun _ -> random.Next())
|> String
let shuffleMiddleCharacters (str: string) =
let words = str.Split(' ')
let random = Random()
words
|> Array.map (fun word ->
if word.Length <= 2 then word
else
let first = word.[0]
let last = word.[word.Length - 1]
let middle = word.[1..word.Length-2].ToCharArray()
let shuffledMiddle =
middle
|> Array.sortBy (fun _ -> random.Next())
|> String
string first + shuffledMiddle + string last)
|> String.concat " "
let doUppercaseLatinFormPalindrome (str: string) =
let uppercaseChars =
str.ToCharArray()
|> Array.filter (fun c -> c >= 'A' && c <= 'Z')
|> Array.map string
|> String.concat ""
isPalindrome uppercaseChars
let countWordsWithEvenLength (str: string) =
str.Split(' ')
|> Array.filter (fun word -> word.Length % 2 = 0)
|> Array.length
let doLowercaseLatinFormPalindrome (str: string) =
let lowercaseChars =
str.ToCharArray()
|> Array.filter (fun c -> c >= 'a' && c <= 'z')
|> Array.map string
|> String.concat ""
isPalindrome lowercaseChars
let countLetterA (str: string) =
str.ToCharArray()
|> Array.filter (fun c -> c = 'А' || c = 'A' || c = 'а' || c = 'a')
|> Array.length
[<EntryPoint>]
let main argv =
printfn "Задание 19: Работа со строками"
printfn "================================\n"
printfn "1. Подсчет русских символов:"
let testStr1 = "Привет Hello Мир World"
let russianCount = countRussianCharacters testStr1
printfn " Строка: \"%s\"" testStr1
printfn " Количество русских символов: %d\n" russianCount
printfn "2. Проверка упорядоченности строчных латинских символов:"
let testStr2a = "abc def ghi"
let testStr2b = "acb def ghi"
printfn " Строка: \"%s\" - упорядочена: %b" testStr2a (isLowercaseLatinSorted testStr2a)
printfn " Строка: \"%s\" - упорядочена: %b\n" testStr2b (isLowercaseLatinSorted testStr2b)
printfn "3. Перемешивание слов:"
let testStr3 = "первое второе третье четвертое"
printfn " Исходная строка: \"%s\"" testStr3
printfn " Перемешанная: \"%s\"\n" (shuffleWords testStr3)
printfn "4. Проверка палиндрома:"
let testStr4a = "А роза упала на лапу Азора"
let testStr4b = "Это не палиндром"
printfn " \"%s\" - палиндром: %b" testStr4a (isPalindrome testStr4a)
printfn " \"%s\" - палиндром: %b\n" testStr4b (isPalindrome testStr4b)
printfn "5. Перемешивание символов:"
let testStr5 = "Hello"
printfn " Исходная строка: \"%s\"" testStr5
printfn " Перемешанная: \"%s\"\n" (shuffleCharacters testStr5)
printfn "6. Перемешивание средних символов в словах:"
let testStr6 = "Hello World Testing"
printfn " Исходная строка: \"%s\"" testStr6
printfn " С перемешанными средними: \"%s\"\n" (shuffleMiddleCharacters testStr6)
printfn "7. Проверка палиндрома из прописных символов:"
let testStr7a = "AaaBbbAaa"
let testStr7b = "AaaBbbCaa"
printfn " \"%s\" - прописные образуют палиндром: %b" testStr7a (doUppercaseLatinFormPalindrome testStr7a)
printfn " \"%s\" - прописные образуют палиндром: %b\n" testStr7b (doUppercaseLatinFormPalindrome testStr7b)
printfn "8. Подсчет слов с четным количеством символов:"
let testStr8 = "один два три четыре пять"
let evenWordsCount = countWordsWithEvenLength testStr8
printfn " Строка: \"%s\"" testStr8
printfn " Слов с четным количеством символов: %d\n" evenWordsCount
printfn "9. Проверка палиндрома из строчных латинских символов:"
let testStr9a = "AabccbaA"
let testStr9b = "AabcdbaA"
printfn " \"%s\" - строчные образуют палиндром: %b" testStr9a (doLowercaseLatinFormPalindrome testStr9a)
printfn " \"%s\" - строчные образуют палиндром: %b\n" testStr9b (doLowercaseLatinFormPalindrome testStr9b)
printfn "10. Подсчет букв 'А':"
let testStr10 = "АААaaa Мама мыла раму AAA"
let letterACount = countLetterA testStr10
printfn " Строка: \"%s\"" testStr10
printfn " Количество букв 'А' (включая 'a', 'а'): %d" letterACount
0