Swift function returning wrong output - ios

I created a function that prints where the students are - lesson, long break or short break (and which short break)
Function parameters are:
shortBreak - duration of short break
longBreak - duration of long break
lesson - after which lesson is long break
time - the time where students are located
My parameters to test function are
currently(shortBreak: 5, longBreak: 15, lesson: 3, time: "10:37)
So it should be
8:00 - 8:45 - lesson
8:45 - 8:50 - short break
8:50 - 9:35 - lesson
9:35 - 9:40 - short break
9:40 - 10:25 - lesson
10:25 - 10:40 - long break (after 3.lesson is long break)
which means that students are currenty on long break, but my output says that they are on lesson.
I've reviewed the code again, but I can't find where I'm wrong.
Also, is there better way to manipulate with time paramater?
import UIKit
enum current {
case lesson
case shortBreak
case longBreak
}
func currently(shortBreak: Int, longBreak: Int, lesson: Int, time:String){
var shortBreakCounter: Int = 0
var currentLesson: Int = 1
var currentHours: Int = 8
var currentMinute: Int = 0
var currentCase: current = .lesson
let minute: Int = Int(time.suffix(2)) ?? 0
let hour: Int = Int(time.prefix(2)) ?? 0
if(checkValid(hour: hour, minute: minute)){
print("Invalid time")
}
else {
while isInRange(hour: currentHours, minute: currentMinute) {
currentCase = .lesson
currentMinute += 45
if currentMinute >= 60 {
currentHours += 1
currentMinute = 0
}
if currentLesson == lesson {
currentCase = .longBreak
currentMinute += longBreak
if currentMinute >= 60 {
currentHours += 1
currentMinute = 0
}
} else{
currentCase = .shortBreak
currentMinute += shortBreak
shortBreakCounter += 1
if currentMinute >= 60 {
currentHours += 1
currentMinute = 0
}
}
currentLesson += 1
}
switch currentCase {
case .shortBreak:
print("Students are on \(shortBreakCounter) short break")
case .longBreak:
print("Students are on long break")
default:
print("Students are on lesson")
}
}
}
func checkValid(hour: Int, minute:Int) -> Bool {
if hour >= 16 || hour < 8 {
return true
} else if minute > 59 || minute < 0 {
return true
}
return false
}
func isInRange(hour: Int, minute: Int) -> Bool{
if 8..<16 ~= hour && 0..<60 ~= minute {
return false
}
return true
}
currently(shortBreak: 5, longBreak: 15, lesson: 3, time: "10:37")

if currentMinute >= 60 {
currentHours += 1
currentMinute = 0
}
This is wrong, you should set currentMinute to the current value - 60.
Imagine if the value is 75, there are 15 minutes that is gone because you set it as 0.
it should be
if currentMinute >= 60 {
currentHours += 1
currentMinute = currentMinute - 60
}

isInRange always returns false if checkValid is true. You are always getting the default currentCase (lesson).
A better way to "edit the time parameter" is by using Date.

Related

Trying to create timeAgo function for news feed, but it just displays "0 seconds ago"

I am trying to create a newsfeed that shows something like "3 minutes ago" or whatever the current time is from when they posted. However, very time I post to the newsfeed it just says 0 seconds ago. Could someone please tell me where my code is messing up.
func timeAgoDisplay() -> String {
let secondsAgo = Int(Date().timeIntervalSinceNow)
if secondsAgo < 60 {
return "\(secondsAgo) seconds ago"
} else if secondsAgo < 60 * 60 {
return "\(secondsAgo / 60) minutes ago"
} else if secondsAgo < 60 * 60 * 24 {
return "\(secondsAgo / 60 / 60) hours ago"
}
return "\(secondsAgo / 60 / 60 / 24) days ago"
}
Since Date() returns the current date, you need to specify an input value with the type Date for the function and use the timeIntervalSinceNow property of that value.
Also, you need to multiply the value you're getting with -1, since the value will be negative if the input date is from the past.
func timeAgoDisplay(_ date: Date) -> String {
let secondsAgo = Int(date.timeIntervalSinceNow) * -1
[...]
}

Hackerrank New Year Chaos Swift

I am trying to solve Hackerrank's New Year Chaos problem in Swift. https://www.hackerrank.com/challenges/new-year-chaos/problem
It is about finding the number of bribes people made on a line waiting for a roller coaster ride. For example, there is a total of 3 bribes in this list [2, 1, 5, 3, 4].
Person 2 bribed person 1.
Person 5 bribed person 3 and 4.
If there are more than 2 bribes by a person, the line becomes "Too chaotic".
I was able to get an exponential solution. However, I want to make it linear.
func minimumBribes(q: [Int]) -> Void {
var bribeCount = 0
var chaotic = false
// for i in 0..<q.count {
// if q[i] - (i + 1) > 2 {
// chaotic = true
// break
// }
//
// for j in i + 1..<q.count {
// if q[i] > q[j] {
// bribeCount += 1
// }
// }
// }
var i = 0
while i < q.count - 1 {
if q[i] - (i + 1) > 2 {
chaotic = true
break
} else if q[i] > i + 1 {
bribeCount += (q[i] - (i + 1))
i += 1
} else if q[i] <= i + 1 && q[i] > q[i + 1] && q.indices.contains(i + 1) {
bribeCount += 1
i += 1
} else { // q[i] < q[i + 1]
i += 1
}
}
if chaotic {
print("Too chaotic")
} else {
print(bribeCount)
}
}
I commented out the exponential solution, which works. But the linear solution does not work and I cannot find out why. It works with the following arrays, [3,2,1,6,5,4], [2,5,1,3,4], [1,2,5,3,7,8,6,4], [1,3,4,2,7,6,5,9,8,11,10,14,13,12].
But there is a really long array in one of the test cases of the problem, which I do not get the correct answer with my linear solution.
For this long array, I get 966 with my exponential solution but the linear solution prints 905.
[2,1,5,6,3,4,9,8,11,7,10,14,13,12,17,16,15,19,18,22,20,24,23,21,27,28,25,26,30,29,33,32,31,35,36,34,39,38,37,42,40,44,41,43,47,46,48,45,50,52,49,51,54,56,55,53,59,58,57,61,63,60,65,64,67,68,62,69,66,72,70,74,73,71,77,75,79,78,81,82,80,76,85,84,83,86,89,90,88,87,92,91,95,94,93,98,97,100,96,102,99,104,101,105,103,108,106,109,107,112,111,110,113,116,114,118,119,117,115,122,121,120,124,123,127,125,126,130,129,128,131,133,135,136,132,134,139,140,138,137,143,141,144,146,145,142,148,150,147,149,153,152,155,151,157,154,158,159,156,161,160,164,165,163,167,166,162,170,171,172,168,169,175,173,174,177,176,180,181,178,179,183,182,184,187,188,185,190,189,186,191,194,192,196,197,195,199,193,198,202,200,204,205,203,207,206,201,210,209,211,208,214,215,216,212,218,217,220,213,222,219,224,221,223,227,226,225,230,231,229,228,234,235,233,237,232,239,236,241,238,240,243,242,246,245,248,249,250,247,244,253,252,251,256,255,258,254,257,259,261,262,263,265,264,260,268,266,267,271,270,273,269,274,272,275,278,276,279,277,282,283,280,281,286,284,288,287,290,289,285,293,291,292,296,294,298,297,299,295,302,301,304,303,306,300,305,309,308,307,312,311,314,315,313,310,316,319,318,321,320,317,324,325,322,323,328,327,330,326,332,331,329,335,334,333,336,338,337,341,340,339,344,343,342,347,345,349,346,351,350,348,353,355,352,357,358,354,356,359,361,360,364,362,366,365,363,368,370,367,371,372,369,374,373,376,375,378,379,377,382,381,383,380,386,387,384,385,390,388,392,391,389,393,396,397,394,398,395,401,400,403,402,399,405,407,406,409,408,411,410,404,413,412,415,417,416,414,420,419,422,421,418,424,426,423,425,428,427,431,430,429,434,435,436,437,432,433,440,438,439,443,441,445,442,447,444,448,446,449,452,451,450,455,453,454,457,456,460,459,458,463,462,464,461,467,465,466,470,469,472,468,474,471,475,473,477,476,480,479,478,483,482,485,481,487,484,489,490,491,488,492,486,494,495,496,498,493,500,499,497,502,504,501,503,507,506,505,509,511,508,513,510,512,514,516,518,519,515,521,522,520,524,517,523,525,526,529,527,531,528,533,532,534,530,537,536,539,535,541,538,540,543,544,542,547,548,545,549,546,552,550,551,554,553,557,555,556,560,559,558,563,562,564,561,567,568,566,565,569,572,571,570,575,574,577,576,579,573,580,578,583,581,584,582,587,586,585,590,589,588,593,594,592,595,591,598,599,596,597,602,603,604,605,600,601,608,609,607,611,612,606,610,615,616,614,613,619,618,617,622,620,624,621,626,625,623,628,627,631,630,633,629,635,632,637,636,634,638,640,642,639,641,645,644,647,643,646,650,648,652,653,654,649,651,656,658,657,655,661,659,660,663,664,666,662,668,667,670,665,671,673,669,672,676,677,674,679,675,680,678,681,684,682,686,685,683,689,690,688,687,693,692,691,696,695,698,694,700,701,702,697,704,699,706,703,705,709,707,711,712,710,708,713,716,715,714,718,720,721,719,723,717,722,726,725,724,729,728,727,730,733,732,735,734,736,731,738,737,741,739,740,744,743,742,747,746,745,750,748,752,749,753,751,756,754,758,755,757,761,760,759,764,763,762,767,765,768,766,771,770,769,774,773,776,772,778,777,779,775,781,780,783,784,782,786,788,789,787,790,785,793,791,792,796,795,794,798,797,801,799,803,800,805,802,804,808,806,807,811,809,810,814,812,813,817,816,819,818,815,820,821,823,822,824,826,827,825,828,831,829,830,834,833,836,832,837,839,838,841,835,840,844,842,846,845,843,849,847,851,850,852,848,855,854,853,857,856,858,861,862,860,859,863,866,865,864,867,870,869,868,872,874,875,871,873,877,878,876,880,881,879,884,883,885,882,888,886,890,891,889,893,887,895,892,896,898,894,899,897,902,901,903,905,900,904,908,907,910,909,906,912,911,915,913,916,918,914,919,921,917,923,920,924,922,927,925,929,928,926,932,931,934,930,933,935,937,939,940,938,936,943,944,942,941,947,946,948,945,951,950,949,953,952,956,954,958,957,955,961,962,963,959,964,966,960,965,969,968,971,967,970,974,972,976,973,975,979,977,981,982,978,980,983,986,984,985,989,988,987,990,993,991,995,994,997,992,999,1000,996,998]
Please help me figure out what is wrong with my solution. Thanks in advance!!
Here is my solution which passes all the test cases :)
func minimumBribes(q: [Int]) -> Void {
var bCount = 0
var isChaotic = false
for (key,value) in q.enumerated() {
if (value - 1) - key > 2 {
isChaotic = true
break
}
for index in stride(from: max(0, value - 2), to: key, by: 1){
if q[index] > value {
bCount += 1
}
}
}
isChaotic ? print("Too chaotic") : print("\(bCount)")
}
What you basically need to do is to first check if the element in each loop is on it's correct position. And if not you find out how much further is it from the right position if its greater than 2 you print "Too chaotic". Your solution is correct uptil this point. But if the difference is less than or equal to 2 then you need to increment the bribes and swap the indices to represent updated array. Furthermore if there are two swaps then you need to represent how the array would be effected by these 2 swaps and hence swap these values before the next iteration to ensure the array is in the condition it would be after these swaps.
Please refer to my solution below. It passes for all test cases:
func swapValues( arr:inout [Int],index:Int, times: Int, bribes:inout Int) -> Bool {
if times == 0 {
return false
}
if arr[index] > arr[index+1] {
let temp = arr[index+1]
arr[index+1] = arr[index]
arr[index] = temp
bribes = bribes + 1
return swapValues(arr: &arr, index: index+1, times: times-1,bribes: &bribes)
}else{
var diff = abs(arr[index+1] - (index+2))
if diff > 2 {
print("Too chaotic")
return true
}
var tooChaotic = swapValues(arr: &arr, index: index+1, times: diff,bribes:&bribes)
if tooChaotic {
return true
}
return swapValues(arr: &arr, index: index, times: times, bribes: &bribes)
}
}
func minimumBribes(q: [Int]) -> Void {
var qC = q
var bribes = 0
var i = 0
while i <= qC.count-1{
if i+1 == qC[i] {
i = i + 1
continue
}
let diff = abs(qC[i] - (i+1))
if diff > 2 {
print("Too chaotic")
return
}
var tooChaotic = swapValues(arr: &qC, index: i, times: diff, bribes: &bribes)
if tooChaotic {
return
}
}
print(bribes)
}
I found this short and easy solution.
func minimumBribes(q: [Int]) -> Void {
var ans = 0
var shouldShow = true
for i in stride(from: (q.count - 1), through: 0, by: -1) {
if (q[i] - (i+1) > 2) {
shouldShow = false
break;
}
for j in stride(from: max(0, q[i] - 2), to: i, by: 1){
if q[j] > q[i] {
ans += 1
}
}
}
if shouldShow {
print(ans)
} else {
print("Too chaotic")
} }
https://github.com/AnanthaKrish/example-ios-apps

Adding time Strings in swift

I have an array of timeStrings of the following format:
x Hr(s) xx min.
I need to add these up to give a total. I'm wondering what is the best way to do this?
My thoughts were to find the index of "Hr(s)". Then if i substring between index 0 and the index of "Hr(s)", I have my hrs var and then add 6 to index of "Hr(s)" and find the index of "min" - 1, to give me the min var.
Then I need to take into account if seconds is greater than 60. So if I divide my seconds var by 60 and the answer is great than 1, I add that answer to my hrs var?
Can anyone see any flaws in this logic?
Sample implementation:
JSON response:
{"status":"OK","hrs":[{"scheduleDate":"2015-11-09","userName":"John Doe","company":"Company X","hrsWorked":"0 Hr(s) 42 min"},{"scheduleDate":"2015-11-10","userName":"Jane Doe","company":"Company Y","hrsWorked":"0 Hr(s) 47 min"},{"scheduleDate":"2015-11-10","userName":"Bob loblaw","company":"Company X","hrsWorked":"0 Hr(s) 37 min"},{"scheduleDate":"2015-11-10","userName":"Joe Soap","company":"Company Y","hrsWorked":"0 Hr(s) 50 min"},{"scheduleDate":"2015-11-10","userName":"Test","company":"Company Y","hrsWorked":"0 Hr(s) 40 min"}],"queryStatus":"OK","message":null,"count":5}
var hrsVar = 0
var minsVar = 0
loop through hrsArray{
hrsMinStr = hrsWorkedInJson
if let endHrsIndex = hrsMinStr.lowercaseString.characters.indexOf("Hr(s)") {
print("Index: \(index)")
let hrsStr = Int(hrsMinStr.substringWithRange(Range<String.Index>(start: 0, end: endHrsIndex)))
hrsVar += hrsStr
let minStr = Int(hrsMinStr.substringWithRange(Range<String.Index>(start: endHrsIndex + 1, end: hrsMinStr.length - 3)))
minsVar += minStr
}
}
if minsVar/60 > 1 {
hrsVar = hrsVar + minsVar/60
minsVar = minsVar%60
}
Update
It seems as though I cannot pass in "Hr(s)" and instead only a single character "h". Because of this, I was trying to use the advancedBy(x) method to get the right endIndex. But I'm getting the error:
Cannot invoke initializer for type 'Range<Index>' with an argument list of type '(start: Int, end: String.CharacterView.Index)'
Updated code:
if let endHrsIndex = hrsMinStr.lowercaseString.characters.indexOf("h") {
print("Index: \(endHrsIndex)")
let hrsStr = Int(hrsMinStr.substringWithRange(Range<String.Index>(start: 0, end: endHrsIndex)))
hrsVar += hrsStr
let minStr = Int(hrsMinStr.substringWithRange(Range<String.Index>(start: endHrsIndex.advancedBy(5), end: hrsMinStr.length.characters.count.advancedBy(-3))))
minsVar += minStr
}
I'm really looking for the most efficient approach as possible, so please advise if there is a better way/if you see issues with this approach
import Foundation
let str = "0 Hr(s) 42 min"
let time = str
.stringByReplacingOccurrencesOfString("Hr(s)", withString:":")
.stringByReplacingOccurrencesOfString("min", withString: "")
.stringByReplacingOccurrencesOfString(" ", withString: "")
let t = time.characters.split(Character(":"))
let h = Int(String(t[0])) // 0
let m = Int(String(t[1])) // 1
and sum
import Foundation
var time: [(hours:Int,minutes:Int,seconds:Int)] = []
for i in 0...5 {
let h = Int(arc4random_uniform(24))
let m = Int(arc4random_uniform(60))
let s = Int(arc4random_uniform(60))
time.append((h,m,s))
}
let t = time.reduce(0) { (sum, time: (hours: Int, minutes: Int, seconds: Int)) -> Int in
sum + time.seconds + time.minutes * 60 + time.hours * 60 * 60
}
let seconds = t % 60
let minutes = ((t - seconds) / 60) % 60
let hours = ((t - seconds) / 3660)
time.forEach {
print(String(format: " %6d:%02d:%02d", arguments: [$0.hours,$0.minutes, $0.seconds]))
}
print("-------------------")
print(String(format: "Sum: %6d:%02d:%02d", arguments: [hours,minutes,seconds]))
/*
12:25:04
2:43:36
14:09:35
11:59:43
10:39:19
23:32:14
-------------------
Sum: 74:29:31
*/
You should most likely try using the scanners in your case.
let string = "12 Hr(s) 37 min"
let scanner = NSScanner(string: string)
var min: Int = 0
var hour : Int = 0
scanner.scanInteger(&hour)
scanner.scanString("Hr(s) ", intoString: nil)
scanner.scanInteger(&min)
Or even easier if you create this part in Objective-C:
int min, hour;
sscanf("12 Hr(s) 37 min", "%d Hr(s) %d min", &hour, &min);

Jquery UI date Picker disable enable Sunday, Thursday and disable specific dates

I'm trying to enable only Thursdays and Sundays but also disable some specific Sundays or Thursdays.
I'm trying with this function but it's not working yet:
<script>
var unavailableDates = ["2013-03-31", "2013-03-24"];
function disabledays(date) {
ymd = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
if ($.inArray(ymd, unavailableDates) == 0) {
return [false, "", "Unavailable"]
} else {
//Show only sundays and thuersdays
var day = date.getDay();
return [(day == 0 || day == 4)];
}
$('#txtDate').datepicker({
beforeShowDay: disabledays
})
</script>
Two problems:
The code that builds a date string does not add a 0 to the month portion. You could change your unavailableDates array.
You need to check the return value of $.indexOf to see if it's >= 0 instead of just equal to zero.
With both changes:
var unavailableDates = ["2013-3-31", "2013-3-24"];
function disabledays(date) {
var ymd = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
if ($.inArray(ymd, unavailableDates) >= 0) {
return [false, "", "Unavailable"];
} else {
//Show only sundays and thuersdays
var day = date.getDay();
return [(day == 0 || day == 4)];
}
}
$('#txtDate').datepicker({
beforeShowDay: disabledays
});
Example: http://jsfiddle.net/XJKbV/

Convert HH:MM:SS to seconds in xcode

Hi I have the code to separate hour,min,sec
Now i have to convert it in to seconds.and nsnumber
NSRange range = [string rangeOfString:#":"];
NSString *hour = [string substringToIndex:range.location];
NSLog(#"time %#",hour);
NSRange range1= NSMakeRange(2,2);
NSString *min = [string substringWithRange:range1];
NSLog(#"time %#",min);
NSRange range2 = NSMakeRange(5,2);
NSString *sec = [string substringWithRange:range2];
NSLog(#"time %#",sec);
If you want to find out how many seconds the hours, minutes and seconds total, you can do something like this:
- (NSNumber *)secondsForTimeString:(NSString *)string {
NSArray *components = [string componentsSeparatedByString:#":"];
NSInteger hours = [[components objectAtIndex:0] integerValue];
NSInteger minutes = [[components objectAtIndex:1] integerValue];
NSInteger seconds = [[components objectAtIndex:2] integerValue];
return [NSNumber numberWithInteger:(hours * 60 * 60) + (minutes * 60) + seconds];
}
Just an alternative if you have to handle both "HH:mm:ss" and "mm:ss"
extension String {
/**
Converts a string of format HH:mm:ss into seconds
### Expected string format ###
````
HH:mm:ss or mm:ss
````
### Usage ###
````
let string = "1:10:02"
let seconds = string.inSeconds // Output: 4202
````
- Returns: Seconds in Int or if conversion is impossible, 0
*/
var inSeconds : Int {
var total = 0
let secondRatio = [1, 60, 3600] // ss:mm:HH
for (i, item) in self.components(separatedBy: ":").reversed().enumerated() {
if i >= secondRatio.count { break }
total = total + (Int(item) ?? 0) * secondRatio[i]
}
return total
}
}
Swift 4 - improved from #Beslan Tularov's answer.
extension String{
var integer: Int {
return Int(self) ?? 0
}
var secondFromString : Int{
var components: Array = self.components(separatedBy: ":")
let hours = components[0].integer
let minutes = components[1].integer
let seconds = components[2].integer
return Int((hours * 60 * 60) + (minutes * 60) + seconds)
}
}
Usage
let xyz = "00:44:22".secondFromString
//result : 2662
You can also try like this:
extension String{
//format hh:mm:ss or hh:mm or HH:mm
var secondFromString : Int{
var n = 3600
return self.components(separatedBy: ":").reduce(0) {
defer { n /= 60 }
return $0 + (Int($1) ?? 0) * n
}
}
}
var result = "00:44:22".secondFromString //2662
result = "00:44".secondFromString //2640
result = "01:44".secondFromString //6240
result = "999:10".secondFromString //3597000
result = "02:44".secondFromString //9840
result = "00:01".secondFromString //60
result = "00:01:01".secondFromString //61
result = "01:01:01".secondFromString //3661
result = "12".secondFromString //43200
result = "abcd".secondFromString //0
From what you've,
double totalSeconds = [hour doubleValue] * 60 * 60 + [min doubleValue] * 60 + [sec doubleValue];
NSNumber * seconds = [NSNumber numberWithDouble:totalSeconds];
The following is a String extension for converting a time string (HH:mm:ss) to seconds
extension String {
func secondsFromString (string: String) -> Int {
var components: Array = string.componentsSeparatedByString(":")
var hours = components[0].toInt()!
var minutes = components[1].toInt()!
var seconds = components[2].toInt()!
return Int((hours * 60 * 60) + (minutes * 60) + seconds)
}
}
how to use
var exampleString = String().secondsFromString("00:30:00")
You can use the following extension to do that (Swift 3):
extension String {
func numberOfSeconds() -> Int {
var components: Array = self.components(separatedBy: ":")
let hours = Int(components[0]) ?? 0
let minutes = Int(components[1]) ?? 0
let seconds = Int(components[2]) ?? 0
return (hours * 3600) + (minutes * 60) + seconds
}
}
And for example, use it like:
let seconds = "01:30:10".numberOfSeconds()
print("%# seconds", seconds)
Which will print:
3790 seconds

Resources