Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.8 kB
2
Indexable
Never
func sumOfPositiveOddAndEvenNumbers(numbers: [Int]) -> (oddSum: Int, evenSum: Int) 
{
    var oddSum = 0
    var evenSum = 0

    for number in numbers {
        if number > 0 {
            if number % 2 == 0 {
                evenSum += number
            } else {
                oddSum += number
            }
        }
    }

    return (oddSum, evenSum)
}

// Example usage:
let integerArray = [1, 2, 3, -4, 5, 6, -7, 8, 9]
let result = sumOfPositiveOddAndEvenNumbers(numbers: integerArray)
print("Sum of positive odd numbers: \(result.oddSum)")
print("Sum of positive even numbers: \(result.evenSum)")


func shortestAndLongestLength(strings: [String]) -> (shortest: Int, longest: Int)? {
    guard !strings.isEmpty else { return nil }

    var shortestLength = Int.max
    var longestLength = Int.min

    for str in strings {
        let length = str.count
        shortestLength = min(shortestLength, length)
        longestLength = max(longestLength, length)
    }

    return (shortestLength, longestLength)
}

// Example usage:
let stringArray = ["apple", "banana", "kiwi", "orange"]
if let result = shortestAndLongestLength(strings: stringArray) {
    print("Length of shortest string: \(result.shortest)")
    print("Length of longest string: \(result.longest)")
}

func sequentialSearch(array: [Int], itemToSearch: Int) -> Bool {
    for element in array {
        if element == itemToSearch {
            return true
        }
    }
    return false
}

// Example usage:
let numbersToSearch = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let itemToSearch = 5
let isItemPresent = sequentialSearch(array: numbersToSearch, itemToSearch: itemToSearch)
print("Is \(itemToSearch) present in the array? \(isItemPresent)")

class CityStatistics {
    var cityName: String
    var population: Int
    var longitude: Double
    var latitude: Double

    init(cityName: String, population: Int, longitude: Double, latitude: Double) {
        self.cityName = cityName
        self.population = population
        self.longitude = longitude
        self.latitude = latitude
    }

    func getPopulation() -> Int {
        return population
    }

    func getLatitude() -> Double {
        return latitude
    }
}

// Example usage:
let cityData = CityStatistics(cityName: "New York", population: 8419600, longitude: -74.006, latitude: 40.7128)

// Accessing data members directly
print("City Name: \(cityData.cityName)")
print("Population: \(cityData.population)")
print("Longitude: \(cityData.longitude)")
print("Latitude: \(cityData.latitude)")

// Using methods to get specific information
print("City Population: \(cityData.getPopulation())")
print("City Latitude: \(cityData.getLatitude())")

var cityDictionary: [String: CityStatistics] = [:]

// Add 5 records to the dictionary
cityDictionary["Paris"] = CityStatistics(cityName: "Paris", population: 2140526, longitude: 2.3522, latitude: 48.8566)
cityDictionary["New York"] = CityStatistics(cityName: "New York", population: 8419600, longitude: -74.006, latitude: 40.7128)
cityDictionary["Tokyo"] = CityStatistics(cityName: "Tokyo", population: 13929286, longitude: 139.6917, latitude: 35.6895)
cityDictionary["Beijing"] = CityStatistics(cityName: "Beijing", population: 21542000, longitude: 116.4074, latitude: 39.9042)
cityDictionary["Sydney"] = CityStatistics(cityName: "Sydney", population: 5312000, longitude: 151.2093, latitude: -33.8688)

var maxPopulationCity: (name: String, population: Int)?

for (city, statistics) in cityDictionary {
    if let maxPopulation = maxPopulationCity {
        if statistics.getPopulation() > maxPopulation.population {
            maxPopulationCity = (name: city, population: statistics.getPopulation())
        }
    } else {
        maxPopulationCity = (name: city, population: statistics.getPopulation())
    }
}

if let maxPopulationCity = maxPopulationCity {
    print("City with the largest population: \(maxPopulationCity.name) (Population: \(maxPopulationCity.population))")
} else {
    print("No city records found.")
}

var northernmostCity: (name: String, latitude: Double)?

for (city, statistics) in cityDictionary {
    if let maxLatitude = northernmostCity {
        if statistics.getLatitude() > maxLatitude.latitude {
            northernmostCity = (name: city, latitude: statistics.getLatitude())
        }
    } else {
        northernmostCity = (name: city, latitude: statistics.getLatitude())
    }
}

if let northernmostCity = northernmostCity {
    print("Northernmost city: \(northernmostCity.name) (Latitude: \(northernmostCity.latitude))")
} else {
    print("No city records found.")
}

var students: [[String: Any]] = [
    ["firstName": "John", "lastName": "Wilson", "gpa": 2.4],
    ["firstName": "Nancy", "lastName": "Smith", "gpa": 3.5],
    ["firstName": "Michael", "lastName": "Liu", "gpa": 3.1]
]

var highestGPAStudent: (firstName: String, lastName: String)?

for student in students {
    if let currentGPA = student["gpa"] as? Double {
        if let maxGPAStudent = highestGPAStudent {
            if currentGPA > maxGPAStudent.gpa {
                highestGPAStudent = (firstName: student["firstName"] as? String ?? "",
                                     lastName: student["lastName"] as? String ?? "")
            }
        } else {
            highestGPAStudent = (firstName: student["firstName"] as? String ?? "",
                                 lastName: student["lastName"] as? String ?? "")
        }
    }
}

if let highestGPAStudent = highestGPAStudent {
    print("Student with the highest GPA: \(highestGPAStudent.firstName) \(highestGPAStudent.lastName)")
} else {
    print("No student records found.")
}
Leave a Comment