This question already has answers here:
NSExpression custom variables inside expression
(1 answer)
Swift - Resolving a math operation in a string
(4 answers)
Closed 7 years ago.
I have been wondering for far too long now: Is it possible in any way to convert a String containing an equation to Int?
For example, this is what I tried:
var equation: String? = "\(5) \(+) \(8)" // 5, +, and 8 would be generated Ints/operaters
if let answer = Int(equation) {
print("\(answer)")
}
I've been trying to find a way to do this across various languages with no luck. Any help is appreciated.
You're looking for an arithmetic expression evaluator, which isn't trivial to do and there doesn't exist a simple function that just does it. You can try to find an open-source implementation of a simple one.
EDIT: Have a look at the Arithmetic Evaluation Rosetta Code page, which is the simplest thing you probably want. There isn't a Swift version (yet), but you should be able to do it yourself by looking at the other languages.
Another EDIT: Check out this Code Golf Thread which features some of the shortest arithmetic expression evaluators in some languages (no Swift yet)
Last EDIT: Also have a look at the Shunting-yard algorithm and generalised Operator-precedence parsing. I'm actually very attempted now to write a simple one in Swift...
Post last EDIT: There exists a Swift library for that :D. Also: Downvotes are for non-useful answers (see hover text), not for when there's a better answer (see comments)
Related
This question already has an answer here:
How to represent an attribute's data type as an array of objects on class diagram?
(1 answer)
Closed 2 years ago.
I am new to UML diagrams, so this may be a very ignorant question though I can't find the answer anywhere.
There is a class Classroom that holds an object of lecture times. Should lecture times be its own class or should it be an attribute of Classroom?
Feel free to critique notation.
Edit: I have already seen this post and it has not helped. I would like to know if LectureTime should be a separate class.
Option 1:
Option 2:
You create a new class if it has more than a single attribute and/or additional operations (which are not just a getter/setter). Or of you plan to add them in a later phase.
In your case lectureTime is obviously a simple type and the 2nd variant is to be preferred, except see above.
However, instead of the round braces you should use square brackets like validLectureTimes[] or validLectureTimes[0..*] which are equivalent.
If LectureTime is an Entity then yes. I guess it is, because of the relation you've added.
This question already has answers here:
How many objects are created
(4 answers)
Closed 9 years ago.
for this statement
String a="MAM"+"BCD"+"EFG"+"GFE";
How many objects will be created? (I am confused is it created 4 or 5 or 7)
Most smart compilers will realize that concatenated string constants can be concatenated at compile time. If your compiler chooses to make that optimization, the answer is one.
Otherwise, you have each of your literal strings, plus one for each concatenation. Without the optimization, the answer is 7 because you have 4 strings and 3 +es.
If you're talking about Java, the answer is one, at compile time, as specified in the JLS.
If you're not, the question is unanswerable as posed.
Only one object will be created. In this case because the plus sign is used to concatenate "string literals". :)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are there any better methods to do permutation of string?
I know recursion and can write programs like fibonacci number, tree traversals so I think I know it but when it comes to this question specifically I feel bad
Please guide me with how to calculate all possible permutations of a string
Here is good examples of different permutation algorithms, including recursive one: http://www.bearcave.com/random_hacks/permute.html
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
The History Behind the Definition of a 'String'
About the only thing every programming language I've seen is able to agree upon is that a variable that refers to a block of text is called a "string." Why? Where does the name come from, and how did it become idiomatic across programming in general?
Great question! This might be somewhat helpful:
Strings are called "strings" because they are made up of a sequence, or string, of characters.
Source: http://www.vias.org/cppcourse/chap07_03.html
Very interesting,
The very definition of a string (according to Princeton at least) is:
a linear sequence of symbols
So, since the String datatype is a sequence of characters/symbols, it rather fits the definition.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Open file in ML(SMLNJ)
I have a string value which has value like this:
"[(1,2,3),(2,3),(6,8)]" -> string
but I want to have these values in int type like this:
[(1,2,3),(2,3),(6,8)] -> (int list) list
what should I do?is there any function which can help me? or I have to do it myself?
This problem is perfectly suited to parsing combinators, which you can steal from my friend Greg Morrisett at Harvard.
If you want to understand the underlying ideas, read Graham Hutton's paper Higher-Order Functions for Parsing. If you want to know how to implement I/O in Standard ML, consult the TextIO module in the Standard Basis Library. If you want someone to write the code for you, you may have reached the wrong web site.
You can use stringint from the String module.
See for example http://wwwcgi.rdg.ac.uk:8081/cgi-bin/cgiwrap/wsi14/poplog/ml/help/string#Character%20Transformation%20Functions