Skip to content

Instantly share code, notes, and snippets.

@Viranchee
Last active February 12, 2021 10:19
Show Gist options
  • Save Viranchee/9a2e34c49ab299546dbd0925a2a3851e to your computer and use it in GitHub Desktop.
Save Viranchee/9a2e34c49ab299546dbd0925a2a3851e to your computer and use it in GitHub Desktop.
University DSA Problem Statement

Sample Input

Problem 1

let student_course_pairs_1 : [[String]] = [ ["58", "Linear Algebra"], ["94", "Art History"], ["94", "Operating Systems"], ["17", "Software Design"], ["58", "Mechanics"], ["58", "Economics"], ["17", "Linear Algebra"], ["17", "Political Science"], ["94", "Economics"], ["25", "Economics"], ["58", "Software Design"],

]

Problem 2

let student_course_pairs_2: [[String]] = [ ["42", "Software Design"], ["0", "Advanced Mechanics"], ["9", "Art History"], ]

Expected Output:

Solution 1

["Software Design": [17, 58], "Political Science": [17], "Economics": [58, 94, 25], "Linear Algebra": [58, 17], "Mechanics": [58], "Operating Systems": [94], "Art History": [94]]

Solution 2

["Software Design": [42], "Advanced Mechanics": [0], "Art History": [9]]

let student_course_pairs_1 : [[String]] = [
["58", "Linear Algebra"],
["94", "Art History"],
["94", "Operating Systems"],
["17", "Software Design"],
["58", "Mechanics"],
["58", "Economics"],
["17", "Linear Algebra"],
["17", "Political Science"],
["94", "Economics"],
["25", "Economics"],
["58", "Software Design"],
]
let student_course_pairs_2: [[String]] = [
["42", "Software Design"],
["0", "Advanced Mechanics"],
["9", "Art History"],
]
struct StudCoursePair {
var studentCode: Int
var courseName: String
}
struct Pair {
var students: [Int]
var courseName: String
}
func makePairCombinations(from values: [Int]) -> [(Int, Int)] {
switch values.count {
case 0,1: return []
case 2: return [(values[0], values[1])] // shortcut
default:
return values.enumerated().reduce(into: []) { (result, iteration) in
let diff = values.count - 1 - iteration.offset
guard diff >= 1 else { return }
// print("Diff: \(diff)")
for i in 1...diff {
let offset = iteration.offset
result.append((iteration.element, values[i + offset]))
}
}
}
}
func decode(data: [[String]]) -> [StudCoursePair] {
return data.compactMap { (arrayOfStrings) -> StudCoursePair? in
guard arrayOfStrings.count >= 2, let studentCode = Int(arrayOfStrings[0]) else {
return nil
}
let course = arrayOfStrings[1]
return .init(studentCode: studentCode, courseName: course)
}
}
func find_pairs(_ data: [[String]]) -> () {
let sanitizedData = decode(data: data)
let pairDicts: [String: [Int]] = sanitizedData.reduce(into: [:]) { (result, currentPair) in
switch result[currentPair.courseName] {
case .some(_):
result[currentPair.courseName]?.append(currentPair.studentCode)
case .none:
result[currentPair.courseName] = [currentPair.studentCode]
}
}
let pairs: [String: [(Int, Int)]] = pairDicts.reduce(into: [:]) { (result, pairDict) in
let combinations = makePairCombinations(from: pairDict.value)
combinations.forEach { (pair) in
switch result[pairDict.key] {
case .none:
result[pairDict.key] = [pair]
case .some(_):
result[pairDict.key]?.append(pair)
}
}
}
print(pairs)
}
find_pairs(student_course_pairs_1)
find_pairs(student_course_pairs_2)
func getPairs(_ pairs: [[String]]) -> [String: [String]]{
var map = [String: [String]]()
for pair in pairs {
let rollNumber = pair[0]
let subject = pair[1]
if var existingSubjects = map[rollNumber] {
existingSubjects.append(subject)
map[rollNumber] = existingSubjects
} else {
map[rollNumber] = [subject]
}
}
var result = [String: [String]]()
let rollNumbers = Array(map.keys)
for (index, rollNumber) in rollNumbers.enumerated() {
var anotherIndex = index + 1
while anotherIndex < rollNumbers.count {
let anotherRollNumber = rollNumbers[anotherIndex]
var commonSubjects = [String]()
if let anotherSubjects = map[anotherRollNumber], let currentSubjects = map[rollNumber] {
commonSubjects = anotherSubjects.filter { currentSubjects.contains($0) }
}
// print(rollNumber, anotherRollNumber, commonSubjects)
let key = rollNumber + ", " + anotherRollNumber
result[key] = commonSubjects
anotherIndex += 1
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment