How to resolve the algorithm Determine sentence type step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Determine sentence type step by step in the Wren programming language
Table of Contents
Problem Statement
Use these sentences: "hi there, how are you today? I'd like to present to you the washing machine 9001. You have been nominated to win one of these! Just make sure you don't break it."
Don't leave any errors!
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Determine sentence type step by step in the Wren programming language
Source code in the wren programming language
var sentenceType = Fn.new { |s|
if (s.count == 0) return ""
var types = []
for (c in s) {
if (c == "?") {
types.add("Q")
} else if (c == "!") {
types.add("E")
} else if (c == ".") {
types.add("S")
}
}
if (!"?!.".contains(s[-1])) types.add("N")
return types.join("|")
}
var s = "hi there, how are you today? I'd like to present to you the washing machine 9001. You have been nominated to win one of these! Just make sure you don't break it"
System.print(sentenceType.call(s))
import "./pattern" for Pattern
import "./iterate" for Indexed
var map = { "?": "Q", "!": "E", ".": "S", "": "N" }
var p = Pattern.new("[? |! |. ]")
var paras = [
"hi there, how are you today? I'd like to present to you the washing machine 9001. You have been nominated to win one of these! Just make sure you don't break it",
"hi there, how are you on St.David's day (isn't it a holiday yet?), Mr.Smith? I'd like to present to you (well someone has to win one!) the washing machine 900.1. You have been nominated by Capt.Johnson('?') to win one of these! Just make sure you (or Mrs.Smith) don't break it. By the way, what the heck is an exclamatory question!?"
]
for (para in paras) {
para = para.trim()
var sentences = p.splitAll(para)
var endings = p.findAll(para).map { |m| m.text[0] }.toList
var lastChar = sentences[-1][-1]
if ("?!.".contains(lastChar)) {
endings.add(lastChar)
sentences[-1] = sentences[-1][0...-1]
} else {
endings.add("")
}
for (se in Indexed.new(sentences)) {
var ix = se.index
var sentence = se.value
System.print("%(map[endings[ix]]) <- %(sentence + endings[ix])")
}
System.print()
}
You may also check:How to resolve the algorithm Compare a list of strings step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Floyd's triangle step by step in the Python programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Rust programming language
You may also check:How to resolve the algorithm Polynomial regression step by step in the TI-83 BASIC programming language