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
Related
This question already has answers here:
How to get the least common element in a list?
(12 answers)
Closed 3 years ago.
string='hhelloo'
output must be 'e' because it is least occurred character in string.
lst=[1,1,2,3,4,5,5]
moc=min([(lst.count(chr),chr) for chr in (lst)])
print(moc)
You could use the last element from Counter.most_common()
from collections import Counter
print(Counter('hhelloo').most_common()[-1][0])
This wouldn't be very efficient however. For a more efficient method, take a look at this answer: https://stackoverflow.com/a/4743286/5946921
It implements a least_common function in a similar way to how Counter.most_common is implemented
This question already has answers here:
How to search for a string in cell array in MATLAB?
(8 answers)
Closed 6 years ago.
I'm a newbie to Matlab and have a question about str locating:
A = ['abc','de','fghij','something','another'];
Then how can I get 3 if I use strfind(A,'fghij')?
Thanks.
I'm not exactly sure I understand your question, but if you are wondering why the value is 3 instead of 2 it is because matlab (unlike most languages you may be used to) indexes arrays starting at 1 instead of 0.
Thanks #TroyHaskin ! I find the answer in another post~
idx=find(ismemeber(A,'fghij')) is what I want.
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)
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