This question already has answers here:
Need to check that braces in given array are balanced or not
(6 answers)
Closed 8 months ago.
I am trying to solve the problem of whether the parentheses in the array are closed correctly or not. For example, if two pairs of parentheses are closed correctly, there are two cases: "()()" "(())". I want to first find the number of all cases of 2 pairs of parentheses, and then verify that it is closed correctly or not.
What I'm thinking is that when I try to find 2 pairs, first of all, "(", "(", ")", ")" is to make all cases first with 2 open and 2 closed parentheses. How can I get all the cases with 2 open parentheses + 2 closed parentheses? Please help me.
In pseudo code:
unClosedCounter = 0
For char in string{
if char == "("
{
unClosedCounter++
}
if char == ")"{
if unClosedCounter == 0
{
return false
}
unClosedCounter--
}
}
Return unClosedCounter == 0
Related
This question already has answers here:
How to convert an Int to a Character in Swift
(9 answers)
Closed 6 years ago.
I'm rewriting a library from java which uses something like
public int myFunc(char c){
return c+200
}
because in java 'a' + 1 will be 'b'
in Swift I cannot simply add an Int to a Character. Google search doesn't know anything about that.
Swift is a Strong Type Language. So the typecast is strict.
If you get ASCII value, you can like this:
let asciiValues = string.utf8.map{ Int($0) }
I hope that helps.
This question already has answers here:
Finding out whether a string is numeric or not
(18 answers)
Closed 7 years ago.
How can I check, if searchView contains just numbers?
I found this:
if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
but it checks if text contains any number. How can I do, that if all text contains just numbers in Swift?
Here is the solution you can get all digits from String.
Swift 3.0 :
let testString = "asdfsdsds12345gdssdsasdf"
let phone = testString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
print(phone)
you can use "^[0-9]+$" instade "^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"
This will accept one or more digits, if you want to accept only one digit then remove +
This question already has an answer here:
Regular expression for password complexity
(1 answer)
Closed 8 years ago.
I'm trying to have the following rules for NSString validation using regular expression:
8 characters minimum length
at least 1 digit
at least 1 uppercase
at least 1 lowercase
I'm only able to do the following to get the first rule like this:
^[a-zA-Z0-9]{8,}$
Which if i understand correctly check for minimum 8 characters length with lower/uppercase and digit
Thank you
Use a lookahead for each assertion:
(?=.*\d)(?=.*[A-Z])(?=.*[a-z])^.{8,}$
This question already has answers here:
Convert erlang terms to string, or decode erlang binary
(2 answers)
Closed 9 years ago.
Is there a way how to convert a tuple to a string ?
Consider I have the following list :
[{atom,5,program},{atom,5,receiving},{nil,5}]
I wish to convert this into the following string:
"{atom,5,program},{atom,5,receiving},{nil,5}"
I have tried using erlang:tuple_to_list on each element in the list, which returns
A = [atom,5,program]
Eventually, I can't concatenate that with "{" ++ A ++ "}"
Any ideas how I can turn that to a string ?
Term = [{atom,5,program},{atom,5,receiving},{nil,5}].
lists:flatten(io_lib:format("~p", [Term])).
This question already has answers here:
Array.join("\n") not the way to join with a newline?
(7 answers)
Closed 5 years ago.
I'm having an issue which I can't seem to solve. I have an array which I need to convert to an single string. The elements need to be put underneath each other.
sample_array = ['a','b','c','d','e']
desired output:
sample_array = "a
b
c
d
e"
I thought I could do this with a 'heredoc', but I can only get the elements behind each other inline. This is unfortunately not what I need. Anyone who can help me?
edit for edit question
In a single line, you can use inject:
sample_array = ['a','b','c','d','e']
puts sample_array.inject(""){|conc,x| conc + "\n" + x }
=> "a b c d e"
that will fold the array recursively and adding a line between chars