We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 26: Nested Logic
  5. Discussions

Day 26: Nested Logic

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

    You are viewing a single comment's thread. Return to all comments →

  • roshankumar350
    3 months ago+ 0 comments

    swift Solution

    let returnedDate = readLine()!.components(separatedBy: .whitespaces)
    let dueDate = readLine()!.components(separatedBy: .whitespaces)
    
    let dueDateComponent: (day: String, month: String, year: String) = (dueDate[0], dueDate[1], dueDate[2])
    
    let returnedComponent: (day: String, month: String, year: String) = (returnedDate[0], returnedDate[1], returnedDate[2])
    
    let dueDateYear = Int(dueDateComponent.year) ?? Int.min
    let returnedYear = Int(returnedComponent.year) ?? Int.min
    
    let dueDateMonth = Int(dueDateComponent.month) ?? Int.min
    let returnedMonth = Int(returnedComponent.month) ?? Int.min
    
    let dueDateDay = Int(dueDateComponent.day) ?? Int.min
    let returnedDay = Int(returnedComponent.day) ?? Int.min
    
    func calculateFine() {
        guard returnedYear >= dueDateYear else {
            print(0)
            return
        }
        
        guard returnedYear == dueDateYear else {
            print(10000)
            return
        }
        
        guard returnedMonth <= dueDateMonth else {
            let diff = returnedMonth - dueDateMonth
            print(diff*500)
            return
        }
        
        guard returnedDay <= dueDateDay else {
            let diff = returnedDay - dueDateDay
            print(diff*15)
            return
        }
        
        print(0)
    }
    
    calculateFine()
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy