F#
JavaScript
Kotlin
Python
Rust
// определение множества let users = set ["Tom"; "Bob"; "Sam"] // перебор множества for user in users do printfn $"{user}" // получаем каждый элемент множества // Консольный вывод: // Bob // Sam // Tom // получаем размер множества printfn "%d" (Set.count users) // 3 // альтернативный вариант // printfn "%d" users.Count // 3 // добавление в множество // множество - неизменяемая структура данных // поэтому получаем новое множество в новую переменную let users1 = Set.add "Alice" users // альтернативный вариант // let users1 = users.Add("Alice") // удаление let users2 = Set.remove "Bob" users1 // альтернативный вариант // let users2 = users1.Remove("Bob") // проверка наличия элемента printfn "%b" (Set.contains "Alice" users2) // true printfn "%b" (Set.contains "Bob" users2) // false // альтернативный вариант // printfn "%b" users.Contains("Alice") // true // printfn "%b" users.Contains("Bob") // false
Основные операции с множествами:
let students = set ["Tom"; "Bob"; "Sam"] let employees = set ["Tom"; "Bob"; "Alex"; "Mike"] // объединение множеств printfn "%A" (Set.union students employees) // set [Alex; Bob; Mike; Sam; Tom] printfn "%A" (students + employees) // set [Alex; Bob; Mike; Sam; Tom] // пересечение множеств printfn "%A" (Set.intersect students employees) // set ["Bob", "Tom"] // симметрическая разность множеств printfn "%A" (Set.difference students employees) // set [Sam] printfn "%A" (students - employees) // set [Sam] // является ли множество подмножеством let workingStudents = Set.intersect students employees // set ["Bob", "Tom"] printfn "%b" (Set.isSubset workingStudents students) // true printfn "%b" (workingStudents.IsSubsetOf(students)) // true printfn "%b" (Set.isSubset employees students) // false printfn "%b" (employees.IsSubsetOf(students)) // false // является ли множество надмножеством let people = Set.union students employees printfn "%b" (Set.isSuperset people students) // true printfn "%b" (people.IsSupersetOf(students)) // true printfn "%b" (Set.isSuperset employees students) // false printfn "%b" (employees.IsSupersetOf(students)) // false
// определение множества const users = new Set(["Tom", "Bob", "Sam"]); // перебор множества for(user of users){ console.log(user); // получаем каждый элемент множества } // Консольный вывод: // Tom // Bob // Sam // получаем размер множества console.log(users.size); // 3 // добавление в множество users.add("Alice"); // удаление users.delete("Bob"); // проверка наличия элемента console.log(users.has("Alice")); // true console.log(users.has("Bob")); // false
Основные операции с множествами (на момент написания статьи поддерживались только в Safari):
const students = new Set(["Tom", "Bob", "Sam"]); const employees = new Set(["Tom", "Bob", "Alex", "Mike"]); // объединение множеств console.log(students.union(employees)); // {"Alex", "Mike", "Bob", "Tom", "Sam"} // пересечение множеств console.log(students.intersection(employees)); // {"Bob", "Tom"} // разность множеств console.log(students.difference(employees)); // {"Sam"} // симметрическая разность множеств console.log(students.symmetricDifference(employees)); // {"Alex", "Mike", "Sam"} // является ли множество подмножеством const workingStudents = students.intersection(employees); console.log(workingStudents.isSubsetOf(students)); // true console.log(employees.isSubsetOf(students)); // false // является ли множество надмножеством const people = students.union(employees); console.log(people.isSupersetOf(students)); // true console.log(employees.isSupersetOf(students)); // false
fun main() { // определение множества val users = mutableSetOf("Tom", "Bob", "Sam") // перебор множества for(user in users) { println(user) // получаем каждый элемент множества } // Консольный вывод: // Tom // Bob // Sam // получаем размер множества println(users.size) // 3 // добавление в множество users.add("Alice") // удаление users.remove("Bob") // проверка наличия элемента println("Alice" in users) // True println("Bob" in users) // False }
Операции с множествами:
fun main() { val students = mutableSetOf("Tom", "Bob", "Sam") val employees = mutableSetOf("Tom", "Bob", "Alex", "Mike") // объединение множеств println(students.union(employees)) //[Tom, Bob, Sam, Alex, Mike] // пересечение множеств println(students.intersect(employees)) // ["Bob", "Tom"] // разность множеств println(students.subtract(employees)) // ["Sam"] }
# определение множества users = {"Tom", "Bob", "Sam"} # перебор множества for user in users: print(user) # получаем каждый элемент множества # Консольный вывод: # Tom # Bob # Sam # получаем размер множества print(len(users)) # 3 # добавление в множество users.add("Alice") # удаление users.remove("Bob") # проверка наличия элемента print("Alice" in users) # True print("Bob" in users) # False
Операции с множествами:
students = {"Tom", "Bob", "Sam"} employees = {"Tom", "Bob", "Alex", "Mike"} # объединение множеств print(students.union(employees)) # {"Alex", "Mike", "Bob", "Tom", "Sam"} print(students | employees) # {"Alex", "Mike", "Bob", "Tom", "Sam"} # пересечение множеств print(students.intersection(employees)) # {"Bob", "Tom"} print(students & employees) # {"Bob", "Tom"} # разность множеств print(students.difference(employees)) # {"Sam"} print(students - employees) # {"Sam"} # симметрическая разность множеств print(students.symmetric_difference(employees)) # {"Alex", "Mike", "Sam"} print(students ^ employees) # {"Alex", "Mike", "Sam"} # является ли множество подмножеством working__students = students & employees print(working__students.issubset(students)) # True print(employees.issubset(students)) # False # является ли множество надмножеством people = students | employees print(people.issuperset(students)) # True print(employees.issuperset(students)) # False
use std::collections::HashSet; fn main() { // определение множества let mut users = HashSet::from(["Tom", "Bob", "Sam"]); // перебор множества for user in &users{ println!("{}",user); // получаем каждый элемент множества } // Консольный вывод: // Tom // Bob // Sam // получаем размер множества println!("{}",users.len()); // 3 // добавление в множество users.insert("Alice"); // удаление users.remove("Bob"); // проверка наличия элемента println!("{}", users.contains("Alice")); // true println!("{}",users.contains("Bob")); // False }
Операции с множествами:
use std::collections::HashSet; fn main() { let students = HashSet::from(["Tom", "Bob", "Sam"]); let employees = HashSet::from(["Tom", "Bob", "Alex", "Mike"]); // объединение множеств let set_union : HashSet<_> = students.union(&employees).collect(); println!("{:?}", set_union) ; // {"Alex", "Mike", "Bob", "Tom", "Sam"} // пересечение множеств let set_intersection : HashSet<_> =students.intersection(&employees).collect(); println!("{:?}", set_intersection); // {"Bob", "Tom"} // разность множеств let set_diff : HashSet<_> = students.difference(&employees).collect(); println!("{:?}", set_diff); // {"Sam"} // симметрическая разность множеств let set_symdiff : HashSet<_> = students.symmetric_difference(&employees).collect(); println!("{:?}", set_symdiff); // {"Alex", "Mike", "Sam"} // является ли множество подмножеством let working_students: HashSet<_> = students.intersection(&employees).copied().collect(); println!("{}", working_students.is_subset(&students)); // true println!("{}", employees.is_subset(&students)); // false // является ли множество надмножеством let people: HashSet<&str> = students.union(&employees).copied().collect(); println!("{}", people.is_superset(&students)); // true println!("{}", employees.is_superset(&students)); // false }