ewww_its_lina

The exam tasks revolve around a dataset consisting of various intriguing travel destinations around the globe. Each destination has unique properties like its location, return to it and the type of environment it offers. Your mission is to neatly classify these eclectic destinations according to their properties and display them along with other relevant details. The tasks call for the application of filtering and sorting algorithms on collections, and will test your understanding of Java very-well. Your dream travel bucket-list begins here! Create a list of travel destinations from destinations.csv, such that only the destinations with more than 10 characters (excluding spaces) in their LOCATION are added into the main list. The other items should be added to a separate list named "_shortLocationList". Display the main list on the screen, but only those records where the destination was visited after the year 2000. Implement a sorter that will sort the "_shortLocationList" items based on their AVERAGE_COST in ascending order. Print this sorted list under the previously printed list, leaving two empty lines in between. Group all items from the main list into a collection, such that items with the same TYPE are displayed together. However, skip displaying any items with the TYPE "Beach". There should be a gap of two new blank lines between this grouped list and the previous printed list.

Denis9224019

# Display main list with destinations visited after 2000
          	  print("Main list with destinations visited after 2000:")
          	  for destination in destinations:
          	      print(f"{destination.location} - Visit Year: {destination.visit_year}")
          	  
          	  # Sort short locations list based on average cost in ascending order
          	  sort_short_locations_list(short_locations_list)
          	  
          	  # Display grouped main list
          	  display_grouped_destinations(destinations)
Yanıtla

Denis9224019

import csv
          	  from itertools import groupby
          	  
          	  class Destination:
          	      def __init__(self, location, visit_year, average_cost, destination_type):
          	          self.location = location
          	          self.visit_year = visit_year
          	          self.average_cost = average_cost
          	          self.type = destination_type
          	  
          	  def load_destinations(file_path):
          	      destinations = []
          	      with open(file_path, 'r') as file:
          	          reader = csv.DictReader(file)
          	          for row in reader:
          	              location = row['LOCATION'].replace(" ", "")
          	              if len(location) > 10 and int(row['VISIT_YEAR']) > 2000:
          	                  destinations.append(Destination(row['LOCATION'], row['VISIT_YEAR'], row['AVERAGE_COST'], row['TYPE']))
          	      return destinations
          	  
          	  def display_grouped_destinations(destinations):
          	      grouped_destinations = {key: list(group) for key, group in groupby(destinations, key=lambda x: x.type)}
          	      print("\n\nGrouped Main list by TYPE (excluding 'beach'):")
          	      for destination_type, destinations_group in grouped_destinations.items():
          	          print(f"\nType: {destination_type}")
          	          for destination in destinations_group:
          	              print(f"  {destination.location} - Visit Year: {destination.visit_year}")
          	  
          	  def sort_short_locations_list(short_locations_list):
          	      short_locations_list.sort(key=lambda x: float(x.average_cost))
          	      print("\nSorted Short locations list based on average cost (ascending):")
          	      for short_location in short_locations_list:
          	          print(f"{short_location.location} - Average Cost: {short_location.average_cost}")
          	  
          	  destinations = load_destinations('destinations.csv')
          	  short_locations_list = [destination for destination in destinations if destination.type.lower() == 'beach']
          	  destinations = [destination for destination in destinations if destination.type.lower() != 'beach']
          	  
Yanıtla

AniQ12345

3.În funcția principală (metoda main), citește datele din fișierul CSV și utilizează clasa DestinationManager pentru a rezolva sarcinile:
          
          import java.io.BufferedReader;
          import java.io.FileReader;
          import java.io.IOException;
          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Date;
          
          public class Main {
              public static void main(String[] args) {
                  DestinationManager destinationManager = new DestinationManager();
          
                  // Citirea datelor din fișierul CSV și adăugarea destinațiilor în lista
                  try (BufferedReader br = new BufferedReader(new FileReader("destinations.csv"))) {
                      String line;
                      while ((line = br.readLine()) != null) {
                          String[] data = line.split(",");
                          Destination destination = new Destination();
                          destination.setLocation(data[0]);
                          destination.setReturnDate(new SimpleDateFormat("yyyy-MM-dd").parse(data[1]));
                          destination.setType(data[2]);
                          destination.setAverageCost(Double.parseDouble(data[3]));
                          destinationManager.addDestination(destination);
                      }
                  } catch (IOException | ParseException e) {
                      e.printStackTrace();
                  }
          
                  // Rezolvă sarcinile cerute
                  destinationManager.filterAndDisplayDestinations();
                  destinationManager.groupAndDisplayDestinationsByType();
              }
          }

AniQ12345

2.Clasa 'DestinationManager'  care va gestiona operațiile pe listele de destinații:
          
          import java.util.ArrayList;
          import java.util.Collections;
          import java.util.Comparator;
          import java.util.List;
          import java.util.stream.Collectors;
          
          public class DestinationManager {
              private List<Destination> destinations;
              private List<Destination> shortLocationList;
          
              // Constructor, getteri și setteri
          
              // Metodă pentru a filtra destinațiile și a le afișa
              public void filterAndDisplayDestinations() {
                  List<Destination> mainList = destinations.stream()
                          .filter(destination -> destination.getLocation().replaceAll("\\s", "").length() > 10)
                          .filter(Destination::visitedAfter2000)
                          .collect(Collectors.toList());
          
                  shortLocationList = destinations.stream()
                          .filter(destination -> destination.getLocation().replaceAll("\\s", "").length() <= 10)
                          .collect(Collectors.toList());
          
                  // Afișează lista principală
                  System.out.println("Main List:");
                  mainList.forEach(System.out::println);
          
                  // Sortează și afișează lista scurtă
                  System.out.println("\nSorted Short Location List:");
                  Collections.sort(shortLocationList, Comparator.comparingDouble(Destination::getAverageCost));
                  shortLocationList.forEach(System.out::println);
              }
          
              // Metodă pentru a grupa destinațiile după tip și a le afișa
              public void groupAndDisplayDestinationsByType() {
                  System.out.println("\nGrouped Destinations By Type:");
                  destinations.stream()
                          .filter(destination -> !destination.getType().equalsIgnoreCase("Beach"))
                          .collect(Collectors.groupingBy(Destination::getType))
                          .forEach((type, typeDestinations) -> {
                              System.out.println(type + ":");
                              typeDestinations.forEach(System.out::println);
                          });
              }
          }

AniQ12345

1.Clasa 'Destination'
          
          import java.util.Date;
          
          public class Destination {
              private String location;
              private Date returnDate;
              private String type;
              private double averageCost;
          
              // Constructor, getteri și setteri
          
              // Implementează metoda pentru a verifica dacă destinația a fost vizitată după anul 2000
              public boolean visitedAfter2000() {
                  // Implementează condiția în funcție de datele tale
                  return returnDate.after(new Date(100, 0, 1)); // exemplu: considerăm 2000 ca an de referință
              }
          }

ewww_its_lina

The exam tasks revolve around a dataset consisting of various intriguing travel destinations around the globe. Each destination has unique properties like its location, return to it and the type of environment it offers. Your mission is to neatly classify these eclectic destinations according to their properties and display them along with other relevant details. The tasks call for the application of filtering and sorting algorithms on collections, and will test your understanding of Java very-well. Your dream travel bucket-list begins here! Create a list of travel destinations from destinations.csv, such that only the destinations with more than 10 characters (excluding spaces) in their LOCATION are added into the main list. The other items should be added to a separate list named "_shortLocationList". Display the main list on the screen, but only those records where the destination was visited after the year 2000. Implement a sorter that will sort the "_shortLocationList" items based on their AVERAGE_COST in ascending order. Print this sorted list under the previously printed list, leaving two empty lines in between. Group all items from the main list into a collection, such that items with the same TYPE are displayed together. However, skip displaying any items with the TYPE "Beach". There should be a gap of two new blank lines between this grouped list and the previous printed list.

Denis9224019

# Display main list with destinations visited after 2000
            print("Main list with destinations visited after 2000:")
            for destination in destinations:
                print(f"{destination.location} - Visit Year: {destination.visit_year}")
            
            # Sort short locations list based on average cost in ascending order
            sort_short_locations_list(short_locations_list)
            
            # Display grouped main list
            display_grouped_destinations(destinations)
Yanıtla

Denis9224019

import csv
            from itertools import groupby
            
            class Destination:
                def __init__(self, location, visit_year, average_cost, destination_type):
                    self.location = location
                    self.visit_year = visit_year
                    self.average_cost = average_cost
                    self.type = destination_type
            
            def load_destinations(file_path):
                destinations = []
                with open(file_path, 'r') as file:
                    reader = csv.DictReader(file)
                    for row in reader:
                        location = row['LOCATION'].replace(" ", "")
                        if len(location) > 10 and int(row['VISIT_YEAR']) > 2000:
                            destinations.append(Destination(row['LOCATION'], row['VISIT_YEAR'], row['AVERAGE_COST'], row['TYPE']))
                return destinations
            
            def display_grouped_destinations(destinations):
                grouped_destinations = {key: list(group) for key, group in groupby(destinations, key=lambda x: x.type)}
                print("\n\nGrouped Main list by TYPE (excluding 'beach'):")
                for destination_type, destinations_group in grouped_destinations.items():
                    print(f"\nType: {destination_type}")
                    for destination in destinations_group:
                        print(f"  {destination.location} - Visit Year: {destination.visit_year}")
            
            def sort_short_locations_list(short_locations_list):
                short_locations_list.sort(key=lambda x: float(x.average_cost))
                print("\nSorted Short locations list based on average cost (ascending):")
                for short_location in short_locations_list:
                    print(f"{short_location.location} - Average Cost: {short_location.average_cost}")
            
            destinations = load_destinations('destinations.csv')
            short_locations_list = [destination for destination in destinations if destination.type.lower() == 'beach']
            destinations = [destination for destination in destinations if destination.type.lower() != 'beach']
            
Yanıtla