Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Say... I have an array [ 1, 2, 4, 5, 6, 9]. I like to make another array from it. like ["1-2", "2-4", "4-5", "5-6", "6-9"] which is based on one previous item and the other item followed by. What would be the coolest way to achieve this in Swift 3? Yes, I know how to do this old fashion way. But I am wondering, if there is a cool or simple way to do this by using such as map, reduce or others.
Thanks,
You can use zip and map along with dropLast and dropFirst to generate the result:
let arr = [ 1, 2, 4, 5, 6, 9]
let result = zip(arr.dropLast(), arr.dropFirst()).map { "\($0)-\($1)" }
print(result)
Output:
["1-2", "2-4", "4-5", "5-6", "6-9"]
zip works by creating a sequence of tuple pairs from the two sequences. map then takes these pairs and combines them using String interpolation.
As #MartinR pointed out, since zip works with different length sequences, you can skip the dropLast():
let result = zip(arr, arr.dropFirst()).map { "\($0)-\($1)" }
From the documentation seen when you option-click on zip:
If the two sequences passed to zip(::) are different lengths, the
resulting sequence is the same length as the shorter sequence.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
For example I have
[
["A",1]
["B",5]
["C",3]
]
How do i sort it so that it returns in Highest to lowest value B, C, A
You can do in following way:
a.sort {
return $0.last as! Int > $1.last as! Int
}
Don't forget to add additional checks while using this code, case where the last item is not an integer or there is an array not in the expected format. Otherwise, it will lead to a crash.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
You are given a dictionary crypt of type [String:String] which has values for all lowercase letters. The crypt dictionary represents a way to encode a message. For example, if crypt["h"] = "#" and crypt["i"] = "!" the encoded version of the message "hi" will be "#!".
The thing is that i have to Write code that would take any string containing only lower case letters and spaces and encode it using the crypt dictionary. I have successfully failed trying to write the code so i ended up just using a single print statement
//print(crypt["h"]!,crypt["i"]!).
If you have any idea you would like to share, please do so.
Thank you
Does this do what you're looking for:
let message = "hi"
let encryptedMessage = message.map { crypt[String($0)]! }.joined()
If you're unfamiliar with it, mapping a string iterates through each character, doing something to it, and returning that string. $0 refers to the first parameter (in this case #1 of 1, but 0-indexed).
As Dopapp suggests, map is the most elegant solution. If you want to see the steps broken out a bit, you can do it the long way.
var message = "hi"
var crytpedMessage = ""
for char in message {
let newChar = crypt[String(char)]
cryptedMessage.append(newChar)
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
var nsarray:[NSMutableDictionary] = [["object":["uid":["age":"26","gender":"male"]]]]
print(nsarray[0]["object"])
That is how it looks. I want to get the value "uid", so when it prints it is just "uid". Currently it is printing:
"uid":["age":"26","gender":"male"]
I want to get the "uid" value. Meaning when it prints it is just "uid". "uid" is a placeholder for a unique ID so I won't know what the uid is.
It looks like "object" key contains another dictionary, which has exactly one element. To get the first key, call allKeys to get keys, convert them to Array, and pick the the initial element:
let d = nsarray[0]["object"] as! NSDictionary
print(Array(d.allKeys)[0])
This question already has answers here:
How to check if an element is in an array
(18 answers)
Closed 7 years ago.
Swift 2 - so, I have an array and a uitextfield that a user inputs a string, I want to check whether the textfield.text is equal to ANY of the values in the array, can I do this with one line of code rather than lots of if's and else if's?!?
This is a generic code that will do what you are looking for. The if statement checks to see if a given value is equal to something that is located in the array. Simply replace the arr.contains() with the output you have given for your UITextfield.text Try to do a little research before you post. I can see that you are new here, so here is a little bit of help.
var arr = [1,2,3,4]
if arr.contains(3) {
//do something
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want remove common element from array. For example:
array1 =
[
{'id'=>78597,'data'=>'great'}
];
array2=
[
{'id'=>78345,'data'=>'first'},{'id'=>78597,'data'=>'great'},
{'id'=>78355,'data'=>'second'}
]
Now key Id '78597' is common in both array
Now i to want remove that element from array2 based on the key 'id'. The examples I referred where all single dimension.
You can build %seen hash lookup and filter #$array2,
my %seen;
#seen{ map $_->{id}, #$array1 } = ();
#$array2 = grep { !exists $seen{$_->{id}} } #$array2;