Display only some digits of an Int [closed] - swift

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
Let's say you have an Int.
let int = 12345
I want it to only display some of the digits.
For example: print(firstTwoDigits) --> 12
How do I do this and thank you in advance.

It depends on your specific requirements.
This prints the first two digits of an integer number
let intVal = 12345
print(String(intVal).prefix(2))
Output: 12
Another way which only prints certain ones in the number:
let intVal = 12345
let acceptableValues = ["1", "2"]
let result = String(intVal).filter {
acceptableValues.contains(String($0))
}
print(result)
Output: 12

Related

How do I Print an Array of names as pairs [closed]

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 2 years ago.
Improve this question
let players = ["Greg", "Jenn", "Steve", "Anthony", "Krista", "Marti", "Erin", "Brandon",].shuffled()
I want to loop over the array and have it print out all pairs after being shuffled... so if the above was the outcome after being shuffled... it would print out
Greg, Jenn
Steve, Anthony
Krista, Marti
Erin, Brandon
you could use this:
if !players.isEmpty {
let arrTpl = stride(from: 1, to: players.count, by: 2).map { (players[$0-1], players[$0]) }
print("\(arrTpl)")
}

Print repetition in Swift while statement [closed]

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 2 years ago.
Improve this question
In my while statement, I cannot understand why my output is printed twice ?
I would like to print i only one time, where is my error ?
func fetch2(){
var i: Int = 0
while i <= (self.returned-1) {
let itemLookUp = "https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemLookup?appid=\(self.appId)&itemcode=\(self.arrayCodeProduct[i])&responsegroup=large"
print(i)
i = i+1
}
}
Here is the output that I obtain :
0
1
2
3
0
1
2
3
Thank you in advance.
It looks like fetch2() is called twice.
Add a print(#function) before you var i and check that fetch2() is not called several times.

Round Double value in swift with addition before decimal [closed]

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 3 years ago.
Improve this question
i have double value 70514.94971385633, now I want it to round the value and make it 70515.00. I have tried command rounded(), but it only rounds value after the decimal. How I can round value after the decimal and add the nearest value to a number before decimal? this is my code for rounding the value,
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.rounded()
but it shows result 70514.95, i want it to add it before decimal value like 70515.00
Just small change in your code
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.round()
or
let roundedValue = totalBidValue.rounded(.up)
Use ceil(_:) to get that working,
let value = 70514.94971385633
let result = ceil(value)
print(result) //70515.0
Use round()
let myDoubleValue = 70514.94971385633
let roundedOffValue = round(myDoubleValue)
print(roundedOffValue) // 70515.0
Your code is almost perfect...
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.rounded(.toNearestOrAwayFromZero)
just add .toNearestOrAwayFromZero in your code

Scala: sort comparing with adjacent elements [closed]

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 5 years ago.
Improve this question
Assuming I have the following Scala classes:
Human(id: String, task: Task)
Task(id: String, time: Duration)
And having a List[(Human, Task)] with the following elements:
("H2", Task("T3", 5 minute))
("H3", Task("T1", 10 minute))
("H1", Task("T1", 10 minute))
("H1", Task("T2", 5 minute))
Now I want to functionally check if close elements have the same duration, and if so, order them by the human id.
In this case, the final list would have the elements sorted like so:
("H2", Task("T3", 5 minute))
("H1", Task("T1", 10 minute))
("H3", Task("T1", 10 minute))
("H1", Task("T2", 5 minute))
I tried to use sortBy to do so, but the way I'm doing, the final list will be fully ordered by the Human ID, not comparing the times.
Does anyone have any idea how can I do this?
Your question is a bit confused. You say you have a List of (Human,Task) tuples, but then you describe a collection of (String,Task) tuples.
Here's a way to sort a List[Human] according to the rules you've described.
def sortHumans(hs: List[Human]): List[Human] =
if (hs.isEmpty) Nil
else {
val target = hs.head.task.time
hs.takeWhile(_.task.time == target).sortBy(_.id) ++
sortHumans(hs.dropWhile(_.task.time == target))
}

Swift 3 - matching user input with dictionary values [closed]

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 5 years ago.
Improve this question
The user of my app will be tested on their english. They will select different words from a list by placing a check mark next to the word. They will want to ONLY select the words that are nouns. If, for example, they choose 5 out of 10 correctly, how do I show them a score of 50%. I think that I need to filter dictionary values based on the user's input. The user's input being an array. What is the best way to code this?
Try the following code:
let myDictionary : [String : Any] =
[ "Nouns":["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"],
"Verbs":["Eat","play","dance","walk","run","sing","read","write","go","come"]]
var myUserSelectedArray:[String] = ["One","come","Three","go","Five","x","Six","x","x","Ten"];
let myNounArray = myDictionary["Nouns"] as? [String];
let myVerbArray = myDictionary["Verbs"] as? [String];
let set1:Set<String> = Set(myUserSelectedArray);
let set2:Set<String> = Set(myNounArray!);
let set3:Set<String> = Set(myVerbArray!);
let scroeInNoun = set1.intersection(set2).count;
let scroeInVerb = set1.intersection(set3).count;
print ("score in Verb \((scroeInVerb * 100)/set2.count) %")
print ("score in noun \((scroeInNoun * 10)/set3.count ) %")
let finalScore = (scroeInNoun + scroeInVerb) * 100 / ((myNounArray?.count)! + (myVerbArray?.count)!)
print ("final score in noun \(finalScore) %")