Sobes.tech
Middle

Display unique combinations of user and product ID for all purchases made by users before they were banned. Sort first by username, then by SKU. Find users who made purchases totaling more than 5000 rubles. Display their names in the format user ID | first name | last name | total purchases.

sobes.tech AI

Answer from AI

To solve the first task, you need data on purchases, users, and their ban status. Suppose we have structures and slices with data:

// Example structures
type Purchase struct {
    UserID    int
    SKU       string
    Amount    float64
    Timestamp time.Time
}

type User struct {
    ID        int
    FirstName string
    LastName  string
    IsBanned  bool
    BanDate   *time.Time // nil if not banned
}

1. Unique combinations of user and product ID for purchases before ban

Logic:

  • For each user, if they are banned, take the ban date.
  • Filter the user's purchases made before the ban date.
  • Form unique pairs (UserName, SKU).
  • Sort by user name, then by SKU.

2. Users with total purchases > 5000

Calculate the sum of purchases for each user and display those with a total greater than 5000.


Example code:

package main

import (
    "fmt"
    "sort"
    "time"
)

type Purchase struct {
    UserID    int
    SKU       string
    Amount    float64
    Timestamp time.Time
}

type User struct {
    ID        int
    FirstName string
    LastName  string
    IsBanned  bool
    BanDate   *time.Time
}

func main() {
    // Example data
    banDate := time.Date(2023, 4, 10, 0, 0, 0, 0, time.UTC)
    users := []User{
        {ID: 1, FirstName: "Ivan", LastName: "Ivanov", IsBanned: true, BanDate: &banDate},
        {ID: 2, FirstName: "Petr", LastName: "Petrov", IsBanned: false, BanDate: nil},
    }

    purchases := []Purchase{
        {UserID: 1, SKU: "A123", Amount: 1000, Timestamp: time.Date(2023, 4, 5, 0, 0, 0, 0, time.UTC)},
        {UserID: 1, SKU: "B456", Amount: 2000, Timestamp: time.Date(2023, 4, 11, 0, 0, 0, 0, time.UTC)}, // after ban
        {UserID: 2, SKU: "A123", Amount: 3000, Timestamp: time.Date(2023, 4, 7, 0, 0, 0, 0, time.UTC)},
        {UserID: 2, SKU: "C789", Amount: 2500, Timestamp: time.Date(2023, 4, 8, 0, 0, 0, 0, time.UTC)},
    }

    // 1. Unique user and SKU combinations for purchases before ban
    type userSKU struct {
        userName string
        sku      string
    }

    uniqueMap := make(map[userSKU]struct{})

    for _, u := range users {
        for _, p := range purchases {
            if p.UserID == u.ID {
                if u.IsBanned && u.BanDate != nil {
                    if p.Timestamp.Before(*u.BanDate) {
                        uniqueMap[userSKU{userName: u.FirstName, sku: p.SKU}] = struct{}{}
                    }
                } else {
                    // User not banned, consider all purchases
                    uniqueMap[userSKU{userName: u.FirstName, sku: p.SKU}] = struct{}{}
                }
            }
        }
    }

    // Convert to slice for sorting
    var uniqueList []userSKU
    for k := range uniqueMap {
        uniqueList = append(uniqueList, k)
    }

    sort.Slice(uniqueList, func(i, j int) bool {
        if uniqueList[i].userName == uniqueList[j].userName {
            return uniqueList[i].sku < uniqueList[j].sku
        }
        return uniqueList[i].userName < uniqueList[j].userName
    })

    fmt.Println("Unique user and SKU combinations before ban:")
    for _, us := range uniqueList {
        fmt.Printf("%s | %s\n", us.userName, us.sku)
    }

    // 2. Users with total purchases > 5000
    sumMap := make(map[int]float64)
    for _, p := range purchases {
        sumMap[p.UserID] += p.Amount
    }

    fmt.Println("\nUsers with total purchases > 5000:")
    for _, u := range users {
        if sum, ok := sumMap[u.ID]; ok && sum > 5000 {
            fmt.Printf("%d | %s | %s | %.2f\n", u.ID, u.FirstName, u.LastName, sum)
        }
    }
}

This example demonstrates the logic for processing and outputting data according to the task.

Display unique combinations of user and product ID… - sobes.tech