//Decrement Binary Number and print number of 1s if input is -
func decrementBinaryNumber(number: String, requests: [String]) -> [Int] {
var result = [Int]()
if let desimal = Int(number, radix: 2) {
var updatedDesimal = desimal
var i = 0
for char in requests {
if char == "?" {
updatedDesimal = updatedDesimal - 0
let str = String(desimal, radix: 2)
var tempCount = 0
for char in str {
if char == "1" {
tempCount += 1
}
}
result.append(tempCount)
} else {
updatedDesimal = updatedDesimal - 1
let str = String(updatedDesimal, radix: 2)
var tempCount = 0
for char in str {
if char == "1" {
tempCount += 1
}
}
result.append(tempCount)
}
i += 1
}
}
return result
}
decrementBinaryNumber(number: "1000", requests: ["?","-","?","-"])
Sign up here with your email
ConversionConversion EmoticonEmoticon