I have this problem and all possible solutions I'm looking for online hasn't helped me. Is there a way to make a table like in a string format?
What I'm expecting the output will be like this for a string:
Category
ITEM1 QTY PRICE TOTAL
ITEM2 QTY PRICE TOTAL
but my output code is like this:
Category
ITEM1 QTY PRICE TOTAL
ITEM2 QTY PRICE TOTAL
Category
ITEM1 QTY PRICE TOTAL
ITEM2 QTY PRICE TOTAL
I figured out something that the item string length is the cost why they are not aligned formally in my format, but is there a way on how to solve this issue? I'm totally new in swift.
here is my code:
for category in self.categoryList {
sentence += "Category: \(category)\n"
for items in self.productList {
if category == items.category {
grandTotal += Double(items.qty)! * Double(items.price)!
let total: Double = Double(items.qty)! * Double(items.price)!
let item = (items.item as NSString).utf8String
let qty = (items.qty as NSString).utf8String
let price = (items.price as NSString).utf8String
sentence += String(format: "%-10s%20s%15s%15.02f\n", item!, qty!, price!, total)
}
}
sentence += "\n\n"
grandTotal = 0
}
lblData.text = sentence
You could pad your strings to a defined length:
let elements = ["ITEM1", "QTY", "PRICE", "TOTAL"]
// without padding
var sentence = ""
for e in elements {
if !sentence.isEmpty {
sentence += " "
}
sentence += e
}
print(sentence)
// prints "ITEM1 QTY PRICE TOTAL"
// with padding
sentence = ""
for e in elements {
sentence += e.padding(toLength: 10, withPad: " ", startingAt: 0)
}
print(sentence)
// prints "ITEM1 QTY PRICE TOTAL "
Or in a shorter form:
// without padding
var sentence = elements.joined(separator: " ")
print(sentence)
// with padding
sentence = elements.map({$0.padding(toLength: 10, withPad: " ", startingAt: 0)}).joined(separator: " ")
print(sentence)
Related
How to remove decimal point value if the number is more than 10.0. Below is the code what i have tried. At below code i am getting the value and i put the condition that if value is less than 1km then show number in meter, if value is more than 1 then show the number in km and if the value is greater than 10.0 then i am not able to remove the decimal point
let resultDelivery = String(format: "%.1f", Obj.distance)// Here getting value from api either i get 0.5 or 6.5 or 11.5
if (resultDelivery.starts(with: "0")){
let resultDelivery2 = String(format: "%.f", Obj.distance/1 * 1000)
cell.lblDeliverykm?.text = resultDelivery2.description + " " + "m".Localized() + " " + "" // result is 900 m
}
else if (resultDelivery.starts(with: "10.0")){
let resultDelivery2 = String(format: "%.0f", Obj.distance)
cell.lblDeliverykm?.text = resultDelivery2.description + " " + "km".Localized() + " " + "" // couldn’t able to remove decimal point
}
else {
cell.lblDeliverykm?.text = resultDelivery.description + " " + "km".Localized() + " " + "" // result is 8.6 km
}
Ah the joys of C-style formatting strings.
I present this as an alternative approach:
extension String.StringInterpolation
{
public mutating func appendInterpolation<F: BinaryFloatingPoint>(distance: F)
{
if distance < 1 {
appendLiteral("\(Int(distance * 1000))m")
}
else if distance >= 10 {
appendLiteral("\(Int(distance))km")
}
else
{
let d = (distance * 10).rounded(.toNearestOrEven) / 10
appendLiteral("\(d)km")
}
}
}
print("\(distance: 0.1)")
print("\(distance: 1)")
print("\(distance: 10)")
print("\(distance: 100)")
The output is
100m
1.0km
10km
100km
This will accept Double, Float, Float80, Float16 and any other type conforming to BinaryFloatingPoint.
If you want localizable formats, look into NumberFormatter.
[EDIT] as noted by #flanker in comments, LengthFormatter with its method, string(from: String, unit: LengthFormatter.Unit) -> String would be the way to go rather than NumberFormatter
I have 2 datasets included in one chart using ggplot. I am using ggplotly to create a tooltip but the information in the tooltips for the 2 points is showing twice. The following code is a little lengthy but will recreate the chart:
AreaName <- c("A", "B", "C", "A", "B", "C")
Timeperiod <- c("2018", "2018", "2018", "2019", "2019", "2019")
Value <- c(11.5, 39.3, 9.4, 14.2, 40.7, 19.1)
df <- data.frame(cbind(AreaName, Timeperiod, Value), stringsAsFactors = F)
df$Value <- as.numeric(df$Value)
AreaName <- c("A", "A")
Timeperiod <- c("2019", "2020")
qtr <- c("Q1-Q2", "Q1-Q2")
Value <- c(15.6, 10.2)
df2 <- data.frame(cbind(Timeperiod, qtr, AreaName, Value), stringsAsFactors = F)
df2$Value <- as.numeric(df2$Value)
ggp <- ggplotly(ggplot(data = df, aes(x=Timeperiod, y=Value, group = AreaName, colour = AreaName, text = paste("Area name: ", AreaName, "<br>Time period: ", Timeperiod, "<br>Rate: ", round(Value,1), "per 100,000"))) +
geom_line() +
geom_point() +
geom_point(data = df2, aes(shape = c(paste(AreaName, qtr, Timeperiod)),text = paste("Area name: ", AreaName, "<br>Quarter: ", qtr, "<br>Time period: ", Timeperiod, "<br>Rate: ", round(Value,1), "per 100,000"))) +
scale_shape_manual(values = c(18, 17)) +
theme(axis.text.x = element_text(vjust = 0.5), axis.title.x = element_blank()) +
labs(y = "Crude rate per 100,000 persons all ages", colour = "Area", shape = "") +
guides(shape = guide_legend(order = 2),colour = guide_legend(order = 1)) +
expand_limits(y=0), tooltip = "text")
ggpNames <- unique(df$AreaName)
legs <- paste(df2$AreaName, df2$qtr, df2$Timeperiod)
ggpNames <- c(ggpNames,legs)
for (i in 1:length(ggp$x$data)) { # this goes over all places where legend values are stored
n1 <- ggp$x$data[[i]]$name # and this is how the value is stored in plotly
n2 <- " "
for (j in 1:length(ggpNames)) {
if (grepl(x = n1, pattern = ggpNames[j])) {n2 = ggpNames[j]} # if the plotly legend name contains the original value, replace it with the original value
}
ggp$x$data[[i]]$name <- n2 # now is the time for actual replacement
if (n2 == " ") {ggp$x$data[[i]]$showlegend = FALSE} # sometimes plotly adds to the legend values that we don't want, this is how to get rid of them, too
}
ggp %>% config(displaylogo = FALSE, modeBarButtonsToRemove = list("autoScale2d", "resetScale2d","select2d", "lasso2d", "zoomIn2d", "zoomOut2d", "toggleSpikelines", "zoom2d", "pan2d"))
ggp
Does anyone have an elegant solution to this?
Thanks
Do not define text in geom_point for the second dataframe df2. Then you will get only one tooltip for those two points.
ggp <- ggplotly(ggplot(data = df, aes(x=Timeperiod, y=Value, group = AreaName, colour = AreaName, text = paste("Area name: ", AreaName, "<br>Time period: ", Timeperiod, "<br>Rate: ", round(Value,1), "per 100,000"))) +
geom_line() +
geom_point() +
geom_point(data = df2, aes(shape = c(paste(AreaName, qtr, Timeperiod)) #,
#text = paste("Area name: ", AreaName, "<br>Quarter: ", qtr, "<br>Time period: ", Timeperiod, "<br>Rate: ", round(Value,1), "per 100,000")
)) +
scale_shape_manual(values = c(18, 17)) +
theme(axis.text.x = element_text(vjust = 0.5), axis.title.x = element_blank()) +
labs(y = "Crude rate per 100,000 persons all ages", colour = "Area", shape = "") +
guides(shape = guide_legend(order = 2),colour = guide_legend(order = 1)) +
expand_limits(y=0), tooltip = "text")
ggpNames <- unique(df$AreaName)
legs <- paste(df2$AreaName, df2$qtr, df2$Timeperiod)
ggpNames <- c(ggpNames,legs)
for (i in 1:length(ggp$x$data)) { # this goes over all places where legend values are stored
n1 <- ggp$x$data[[i]]$name # and this is how the value is stored in plotly
n2 <- " "
for (j in 1:length(ggpNames)) {
if (grepl(x = n1, pattern = ggpNames[j])) {n2 = ggpNames[j]} # if the plotly legend name contains the original value, replace it with the original value
}
ggp$x$data[[i]]$name <- n2 # now is the time for actual replacement
if (n2 == " ") {ggp$x$data[[i]]$showlegend = FALSE} # sometimes plotly adds to the legend values that we don't want, this is how to get rid of them, too
}
ggp %>% config(displaylogo = FALSE, modeBarButtonsToRemove = list("autoScale2d", "resetScale2d","select2d", "lasso2d", "zoomIn2d", "zoomOut2d", "toggleSpikelines", "zoom2d", "pan2d"))
ggp
I want to take input from user in binary, What I want is something like:
10101
11110
Then I need to perform bitwise OR on this. I know how to take input and how to perform bitwise OR, only I want to know is how to convert because what I am currently using is not giving right result. What I tried is as below:
let aBits: Int16 = Int16(a)! //a is String "10101"
let bBits: Int16 = Int16(b)! //b is String "11110"
let combinedbits = aBits | bBits
Edit: I don't need decimal to binary conversion with radix, as my string already have only 0 and 1
String can have upto 500 characters like:
1001101111101011011100101100100110111011111011000100111100111110111101011011011100111001100011111010
this is beyond Int limit, how to handle that in Swift?
Edit2 : As per vacawama 's answer, below code works great:
let maxAB = max(a.count, b.count)
let paddedA = String(repeating: "0", count: maxAB - a.count) + a
let paddedB = String(repeating: "0", count: maxAB - b.count) + b
let Str = String(zip(paddedA, paddedB).map({ $0 == ("0", "0") ? "0" : "1" }))
I can have array of upto 500 string and each string can have upto 500 characters. Then I have to get all possible pair and perform bitwise OR and count maximum number of 1's. Any idea to make above solution more efficient? Thank you
Since you need arbitrarily long binary numbers, do everything with strings.
This function first pads the two inputs to the same length, and then uses zip to pair the digits and map to compute the OR for each pair of characters. The resulting array of characters is converted back into a String with String().
func binaryOR(_ a: String, _ b: String) -> String {
let maxAB = max(a.count, b.count)
let paddedA = String(repeating: "0", count: maxAB - a.count) + a
let paddedB = String(repeating: "0", count: maxAB - b.count) + b
return String(zip(paddedA, paddedB).map({ $0 == ("0", "0") ? "0" : "1" }))
}
print(binaryOR("11", "1100")) // "1111"
print(binaryOR("1000", "0001")) // "1001"
I can have array of upto 500 string and each string can have upto 500
characters. Then I have to get all possible pair and perform bitwise
OR and count maximum number of 1's. Any idea to make above solution
more efficient?
You will have to do 500 * 499 / 2 (which is 124,750 comparisons). It is important to avoid unnecessary and/or repeated work.
I would recommend:
Do an initial pass to loop though your strings to find out the length of the largest one. Then pad all of your strings to this length. I would keep track of the original length of each string in a tiny stuct:
struct BinaryNumber {
var string: String // padded string
var length: Int // original length before padding
}
Modify the binaryOR function to take BinaryNumbers and return Int, the count of "1"s in the OR.
func binaryORcountOnes(_ a: BinaryNumber, _ b: BinaryNumber) -> Int {
let maxAB = max(a.length, b.length)
return zip(a.string.suffix(maxAB), b.string.suffix(maxAB)).reduce(0) { total, pair in return total + (pair == ("0", "0") ? 0 : 1) }
}
Note: The use of suffix helps the efficiency by only checking the digits that matter. If the original strings had length 2 and 3, then only the last 3 digits will be OR-ed even if they're padded to length 500.
Loop and compare all pairs of BinaryNumbers to find largest count of ones:
var numbers: [BinaryNumber] // This array was created in step 1
maxOnes = 0
for i in 0 ..< (numbers.count - 1) {
for j in (i + 1) ..< numbers.count {
let ones = binaryORcountOnes(numbers[i], numbers[j])
if ones > maxOnes {
maxOnes = ones
}
}
}
print("maxOnes = \(maxOnes)")
Additional idea for speedup
OR can't create more ones than were in the original two numbers, and the number of ones can't exceed the maximum length of either of the original two numbers. So, if you count the ones in each number when you are padding them and store that in your struct in a var ones: Int property, you can use that to see if you should even bother calling binaryORcountOnes:
maxOnes = 0
for i in 0 ..< (numbers.count - 1) {
for j in (i + 1) ..< numbers.count {
if maxOnes < min(numbers[i].ones + numbers[j].ones, numbers[i].length, numbers[j].length) {
let ones = binaryORcountOnes(numbers[i], numbers[j])
if ones > maxOnes {
maxOnes = ones
}
}
}
}
By the way, the length of the original string should really just be the minimum length that includes the highest order 1. So if the original string was "00101", then the length should be 3 because that is all you need to store "101".
let number = Int(a, radix: 2)
Radix helps using binary instead of decimical value
You can use radix for converting your string. Once converted, you can do a bitwise OR and then check the nonzeroBitCount to count the number of 1's
let a = Int("10101", radix: 2)!
let b = Int("11110", radix: 2)!
let bitwiseOR = a | b
let nonZero = bitwiseOR.nonzeroBitCount
As I already commented above "10101" is actually a String not a Binary so "10101" | "11110" will not calculate what you actually needed.
So what you need to do is convert both value in decimal then use bitwiseOR and convert the result back to in Binary String (in which format you have the data "11111" not 11111)
let a1 = Int("10101", radix: 2)!
let b1 = Int("11110", radix: 2)!
var result = 21 | 30
print(result)
Output: 31
Now convert it back to binary string
let binaryString = String(result, radix: 2)
print(binaryString)
Output: 11111
--: EDIT :--
I'm going to answer a basic example of how to calculate bitwiseOR as the question is specific for not use inbuilt function as string is very large to be converted into an Int.
Algorithm: 1|0 = 1, 1|1 = 1, 0|0 = 0, 0|1 = 1
So, What we do is to fetch all the characters from String one by one the will perform the | operation and append it to another String.
var str1 = "100101" // 37
var str2 = "10111" // 23
/// Result should be "110111" -> "55"
// #1. Make both string equal
let length1 = str1.characters.count
let length2 = str2.characters.count
if length1 != length2 {
let maxLength = max(length1, length2)
for index in 0..<maxLength {
if str1.characters.count < maxLength {
str1 = "0" + str1
}
if str2.characters.count < maxLength {
str2 = "0" + str2
}
}
}
// #2. Get the index and compare one by one in bitwise OR
// a) 1 - 0 = 1,
// b) 0 - 1 = 1,
// c) 1 - 1 = 1,
// d) 0 - 0 = 0
let length = max(str1.characters.count, str2.characters.count)
var newStr = ""
for index in 0..<length {
let charOf1 = Int(String(str1[str1.index(str1.startIndex, offsetBy: index)]))!
let charOf2 = Int(String(str2[str2.index(str2.startIndex, offsetBy: index)]))!
let orResult = charOf1 | charOf2
newStr.append("\(orResult)")
}
print(newStr)
Output: 110111 // 55
I would like to refer Understanding Bitwise Operators for more detail.
func addBinary(_ a: String, _ b: String) {
var result = ""
let arrA = Array(a)
let arrB = Array(b)
var lengthA = arrA.count - 1
var lengthB = arrB.count - 1
var sum = 0
while lengthA >= 0 || lengthB >= 0 || sum == 1 {
sum += (lengthA >= 0) ? Int(String(arrA[lengthA]))! : 0
sum += (lengthB >= 0) ? Int(String(arrB[lengthB]))! : 0
result = String((sum % 2)) + result
sum /= 2
lengthA -= 1
lengthB -= 1
}
print(result) }
addBinary("11", "1")
How to perform reversing of all words in a sentence.
Example
let str = "Hello playground"
Result should be like "olleH dnuorgyalp"
With minimum complexity.
Here's the typical functional programming approach (also posted by #Saranjith)
let result = str
.components(separatedBy: " ")
.map { $0.reversed() }
.joined()
Complexity
First of all lets define
n: the number of characters in the input string
k: the number of words in the input string (of course k<=n)
⏳ Time complexity
Now lets look at the time complexity of each piece of our code
.components(separatedBy: " ")
This instructions need to go through the entire string so O(n)
.map { $0.reversed() }
Here each word is reversed. So if we have k words we have a time complexity of
O(m0) + O(m1) + ... + O(mk-1)
where mi is the length of the i-th word.
However the sum of the length of all the words is <= n so we can say that
O(m0) + O(m1) + ... + O(mk-1) <= O(n)
.joined()
Finally we have k words which need to be joined togheter. This can be done in O(k) that, again, is <= O(n).
Wrap up
let result = str
.components(separatedBy: " ") // O(n)
.map { $0.reversed() } // O(m0) + O(m1) + ... + O(mk-1) <= O(n)
.joined() // O(k) <= O(n)
Time complexity = O(n) + O(n) + O(n) = O(n)
🚀 Space complexity
.components(separatedBy: " ")
Here we are building k Strings (m0, m1, ..., mk-1). The sum of the length of these k-1 words will be <= n so here space complexity is O(n).
.map { $0.reversed() }
For each (m0, m1, ..., mk-1) we are building a duplicate. So, again, O(n).
.joined()
Here we are building the result which is a string with n chars. So space complexity is O(n).
Space complexity: O(n) + O(n) + O(n) = O(n).
You can use String method enumerateSubstrings using .byWords options and replace the subrange of each word with the substring reversed. Note that this way the punctuation will remain in place:
import Foundation
Mutating approach:
var str = "Hello, playground!!!"
str.enumerateSubstrings(in: str.startIndex..., options: .byWords) { _, range, _, _ in
str.replaceSubrange(range, with: str[range].reversed())
}
print(str) // "olleH, dnuorgyalp!!!"
Non mutating:
let str = "Hello, playground!!!"
var result = ""
str.enumerateSubstrings(in: str.startIndex..., options: .byWords) { string, range, enclosingRange, _ in
result.append(contentsOf: string!.reversed())
result.append(contentsOf: str[range.upperBound..<enclosingRange.upperBound])
}
print(result) // "olleH, dnuorgyalp!!!"
Working code!
import UIKit
var str = "Hello playground"
let result = str.split(separator: " ").map { String($0.reversed())}.joined(separator: " ")
print(result) // olleH dnuorgyalp
let result = String(str.reversed())
.components(separatedBy: .whitespaces)
.reversed()
.joined(separator: " ")
Building on the OPs answer, if you wanted to follow English capitalization rules on the output:
var str = "Hello playground"
let result = str.split(separator: " ").enumerated().map {
let reversed = String($0.1.reversed())
return $0.0 == 0 ? reversed.capitalized : reversed.lowercased()
}
.joined(separator: " ")
print("Rerversing letters in each word = \"" + result + "\"") // Olleh dnuorgyalp
Also note that multiple spaces would mess this up, as would commas, periods, and other word/sentence delimiters.
func FirstReverse(_ str: String) -> String {
return String(str.reversed())
}
So I have a Double that gets calculated and varies in length each time (based on certain input). This Double is placed in to a String.
var doubleNumber = 30440.8734
var string = "€ \(round(100 * doubleNumber) / 100)"
var numberString: String = string.stringByReplacingOccurrencesOfString("€ ", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
var splitString = split(numberString) {$0 == "."}
println(splitString[0])
Result would be: "30440"
What I would like to do is to place spaces in this number for readability. For which the result would end in: "30 440"
Any suggestions?
Regardless of the issue of currency formats, you can use an NSNumberFormatter to add spaces as grouping separators in a number:
var doubleNumber = 30440.8734
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencySymbol = "€ "
numberFormatter.currencyGroupingSeparator = " "
numberFormatter.maximumFractionDigits = 0
if let formattedString = numberFormatter.stringFromNumber(doubleNumber) {
println(formattedString)
}
Output: € 30 441
I do think that NSNumberFormatter approach is better:
let f = NSNumberFormatter()
f.groupingSeparator = " "
f.groupingSize = 3
f.usesGroupingSeparator = true
f.stringFromNumber("30456".toInt()!)
but here's my solution if I would do this task without formatter:
var i: Int = count(number)
let a = "".join(map(number) {String($0) + (--i % 3 == 0 ? " " : "")})
a.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())